I have successfully used GetText to translate JavaScript files into three projects.
Of course, GetText does not natively support JavaScript, and JavaScript also does not support GetText. My solution was as follows.
- In JavaScript, I surround broadcast strings with
gettext("your text here") . - Then I have a script that pulls strings from JavaScript files and creates a C file because C is supported by gettext.
- Then I use
xgettext in the generated C files and any other files in my project to create the POT and PO files.
After translating the PO files, I have to return the translations to JavaScript ...
- I parse the JavaScript files again, remembering the list of translatable strings, then for each locale:
- Using a language with GetText support (in my case, PHP) I translated all the lines and output the JavaScript file.
For example, a locale file for the Estonian language (for example, et_EE.js ) might look like this:
var LOCALE = { "Hello": "Tere", "My name is %s": "Minu nimi on %s", "Enter your credit card number": "Sisesta oma krediitkaardi number" }; function gettext(string) { return LOCALE[string] ? LOCALE[string] : string; }
Depending on the locale you et_EE.js you include et_EE.js or en_US.js or ...
en_US.js will probably only contain the following:
function gettext(string) { return string; }
A bit trickier for ngettext() , but you should get a general idea.
The great thing is that I can use all the gettext tools available. Especially when I have translatable texts in both PHP and JavaScript - I can use the same tool to translate both of them, and also ensure that the same line is translated the same way in both JavaScript and in PHP.
NOTE. . If you are not using a web application that uses JavaScript heavily, you should think twice before creating a web site created by JavaScript first.
source share