How do I feed terms from a gettext dictionary into JavaScript?

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:

... some kind of gettext initialization ...
<!DOCTYPE html>
    <title><?php echo _("My page Title"); ?></title>


    <script type="text/javascript">
        var LANG = {
            "string1" : "<?php echo _("string1"); ?>",
            "string2" : "<?php echo _("string2"); ?>",
            "string3" : "<?php echo _("string3"); ?>",

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?


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.

I use defines, language php files and a session to tell what language to display.


    if (isset($_SESSION['lang'])){
        require('language/' . $_SESSION['lang'] . '.php');
    <?= HELLO_WORLD ; ?>

then make your language files like this.


    define("HELLO_WORLD", "Hello world!");

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.

Need Your Help

Sync Framework between SQL Server 2008 and SQL Server CE

compact-framework sql-server-ce sql-server-2008-r2 microsoft-sync-framework windows-mobile-6.5

I'm working with a moderately sized database of about 60,000 records. I am working on building a mobile application which will be able to check out a single table into a compact .sdf on for viewing...

Pulling git submodules with TortoiseGit

git tortoisegit

I'm using TortoiseGit to maintain git repository. We have one repo with multiple submodules in each. Everything works fine but when I'm trying to pull main repo, submodules aren't updating. I must ...