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?
You should check out Poedit, as far as i know it is the best localization software out there, its used extensively in wordpress etc.. you could also store all the different translations in a db and serve them according to geo location.
I use defines, language php files and a session to tell what language to display.
then make your language files like this.
gettext
is used by many industry-standard apps out there. There's nothing fundamentally wrong with it except that I think it doesn't handle very well (as it tends to depend on locales installed on the system and crap like that).A more complex, but also more flexible alternative could be Zend_Translate that can be connected to different dictionary backends - .po/mo files like gettext (which are great because you have an entire universe of editing tools for all platforms), but also databases.
The approach you show (feeding the dictionary into JavaScript in the main document) is fine, as you supposedly are supposedly already using PHP to do dynamic processing at that point and the JavaScript file can remain static, which is great.
It might be a bit heavy on the HTML file if you have a huge dictionary, though, as you're transferring all that data on every request. In that case, you should consider an external JavaScript resource after all. You have two options:
Serve a PHP-generated JavaScript file - one that contains all the dictionary definitions. Use some form of
If-modified-since
caching so you have to re-render the dictionary only when something has actually changed, or send caching headers with looong lifetimes.or: whenever the dictionary gets updated, run a PHP script (automatically or manually) that generates a static JavaScript file containing all the dictionary definitions. That might be the best of both worlds.