views:

428

answers:

4

I am using gettext in my PHP code, but i have a big problem. All my Javascript files are not affected by the translation, can somebody tell me an easy way to get the translations in the chosen language into JavaScript as well.

+2  A: 

Try, jQuery i18n or jQuery localisation

An example for jQuery i18n, and of course you need to generate JSON based dictionary from language file from php

var my_dictionary = { 
    "some text"  : "a translation",
    "some more text"  : "another translation"
}
$.i18n.setDictionary(my_dictionary);


$('div#example').text($.i18n._('some text'));
S.Mark
i have nevere tryt that, and its comming in combinasion whit json, alerts and more.
NeoNmaN
+5  A: 

The easiest way is having a PHP file write the translations from gettext into JavaScript variables.

js_lang.php:

word_hello = "<?php echo gettext("hello"); ?>"
word_world = "<?php echo gettext("world"); ?>"
word_how_are_you = "<?php echo gettext("how_are_you"); ?>"

and then include it:

<script type="text/javascript" src="js_lang.php"></script>

I would also recommend this method in conjunction with the translation plugins S.Mark mentions (which are very interesting!).

You can define the dictionary in the current page's header, too, without including an external file, but that way, you would have to look up and send the data on every page load - quite unnecessary, as a dictionary tends to change very rarely.

Pekka
can you maby try to get a eg. and paste it on http://pastebin.org? :)
NeoNmaN
@neonman example for what exactly?
Pekka
i think i get a idé now :) tanks for help, i will use you respons ;)
NeoNmaN
Pekka, how should you manage this for a very large website with many translations? would the generated language file be too large? (Let's say Facebook size just for fun) Or should we just stick to generating the HTML and text server side and use our current PHP language framework? Thanks
KennyCason
@Kenny Re the "very large" aspect, unless you're talking about megabytes of data needed for one translation, I think having one external JavaScript translation file per language - be it manually set up, cached, or generated by PHP - should work fine. It'll be loaded only once (make sure caching is configured correctly) and will provide all necessary words for all contexts of the site.
Pekka
Thanks :) I'll give it a spin.
KennyCason
A: 

I generally export the translations in a JavaScript structure:

var app = {}
var app.translations = {
  en:  { hello: "Hello, World!"
       , bye:   "Goodbye!"
       }
, nl:  { hello: "Hallo, Wereld!"
       , bye:   "Tot ziens!"
       }
};

The current language of the page texts can be defined using: <html xml:lang="en" lang="nl">

This can be read in JavaScript:

var curentLanguage = document.documentElement.lang || "en";
app.lang = app.translations[ currentLanguage ] || app.translations.en;

And then you can write code like this:

alert( app.lang.hello );

Optionally, a i18n() or gettext() function can bring some intelligence, to return the default text if the key does not exist). For example:

function gettext( key )
{
  return app.lang[ key ] || app.translations.en[ key ] || "{translation key not found: " + key + "}";
}
vdboor
A: 

You can make your life much easier if you get rid of bad habit to use string literals in your code. That is, instead of

 alert("Some message")

use

alert($("#some_message_id").text())

where "#some_message_id" is a hidden div or span generated on the server side.

stereofrog