How to pass terms from gettext dictionary to JavaScript?

I am trying to create a website with php and javascript (jquery) that supports multiple languages. I am trying to get something that is:

  • Effective: I need to reduce the amount of processing for this operation as much as possible.
  • Extensible: it should be simple and practical, adding new lines to translations

The problem is that my javascript code generates dynamically different elements in the DOM and should print some lines that are in the language files.

My actual solution is to use gettext in php to print the lines javascript should use in the document:

<?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 turn to LANGin my js sources, but I don't think this is the best solution ...

My questions:

  • javascript " " php?
  • gettext? - ?
+4
3

gettext . , , , ( , , ).

, Zend_Translate, -.po/mo , gettext ( , ), .

, ( JavaScript ), , PHP , JavaScript , .

HTML , , . JavaScript . :

+2

, php , , .

    index.php

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

.

    language/english.php

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

You should check Poedit, as far as I know, this is the best localization software, it is widely used in wordpress, etc., you can also store all the different translations in db and serve them according to geographic location.

0
source

All Articles