How to translate strings in js

I have a project that I want to translate into several languages ​​for PHP. Part I use Zend Frameworks Zend_Translate with GetText. Now I also want to translate part of JS.

I am using jQuery JS Framework and would like to hear your thoughts on translating to JS files.

+4
source share
7 answers

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.

+12
source

This may also be of interest to you: gettext plugin for jQuery .

+2
source

Well, you can expand strings to give them a translated function that looks for the recipient in the locale.

You would use it as follows:

alert ("Your favorite language is English!". translation ());

And you will be given a warning with "tu idiom prefiero es esspanol" or something

Niko

+1
source

http://plugins.jquery.com/project/jquery-localize found this which is used and letmegooglethatforyou.com also found this http://plugins.jquery.com/project/l10n which is better?

0
source

I have a PHP webpage that also exists in several languages. I wrote my own language class in PHP, and when it came to localizing JavaScript files, I just configured the server to execute .js files as in PHP, and I used my PHP class to translate JavaScript strings.

Something like that:

 alert ( '<?php echo $l->Get ( 'MyString' ); ?>' ); 

I simply include a file that initializes "$ l" at the beginning of each JavaScript file. I have no problem with this.

0
source
 // just for some idea //var language ="fr_FR"; var language ="sp_SP"; var FRLOCALE = { "Hello": "bonjure ", "My name is %s": "mo nome oc ", "what": "some french thing" }; var SPLOCALE = { "Hello": "Spanish for hello", "My name is %s": "spanish for my name is ", "what": "qu" }; function translated(language, string){ if (language.indexOf("fr") > -1) { return FRLOCALE[string] ? FRLOCALE[string] : string; } if (language.indexOf("sp") > -1) { return SPLOCALE[string] ? SPLOCALE[string] : string; } return string; } alert(translated(language,"Hello")); alert(translated(language,"some thing not translated ")); 
0
source

All Articles