I'm trying to develop a website with php and javascript (jquery) that supports multiple languages. I'm trying to obtain something that is:
- Efficient: I need to reduce as much as possible the backend processing for this operation.
- Expandable: it must be easy and practical adding new strings to the translations
The problem is: my javascript code is generating dynamically various elements in the DOM and needs to print some strings that are in the language files.
My actual solution is, using gettext in php, to print in the document the strings that have to be used by javascript:
<?php
... some kind of gettext initialization ...
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo _("My page Title"); ?></title>
...
</head>
<body>
...
<script type="text/javascript">
var LANG = {
"string1" : "<?php echo _("string1"); ?>",
"string2" : "<?php echo _("string2"); ?>",
"string3" : "<?php echo _("string3"); ?>",
...
};
</script>
</body>
</html>
Then I'm accessing to the LANG
in my js sources, but I don't think that this is the best solution...
My questions are:
- What is the correct approach to apply a translations to a javascript file, using the same "database" of php?
- How efficient is
gettext
library? It could be useful to implement some form of caching mechanism on the generated pages?