I have a project which I want to translate into multiple languages for the PHP Part I use Zend Frameworks Zend_Translate with GetText. Now I want to translate the JS part too.
I am using the jQuery JS Framework and would love to hear your thoughts on translation for JS files
How to convert a string to a number in JavaScript using the parseInt() function. Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.
I have successfully used GetText for translation of JavaScript files in three projects.
Of course GetText natively doesn't support JavaScript and JavaScript also doesn't support GetText. My solution has been the following.
gettext("your text here")
.xgettext
on the generated C files and on any other files in my project to produce POT and PO files.After translating PO files, I have to get the translations back to JavaScript...
For example a locale file for Estonian (e.g. 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 selected locale you either include et_EE.js
or en_US.js
or ...
en_US.js
will probably contain just the following:
function gettext(string) {
return string;
}
A bit trickier for ngettext()
, but you should get the general idea.
The great thing is that I can use all the gettext tools already available. Especially when I have translatable texts in both PHP and JavaScript - I can use the same tool for translating both of them and also ensure that the same string is translated the same way both in JavaScript and PHP.
NOTE: If you aren't dealing with a JavaScript-intensive web-app, you should think twice before having page content created by JavaScript in the first place.
http://24ways.org/2007/javascript-internationalisation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With