PDA

View Full Version : Multilingual and JavaScript



Gunmen
3 Sep 2008, 9:31 AM
Okay, more a JavaScript question but also good to know for ExtJs developers.

What is the best way to make multilingual web applications with JavaScript? For example, I have an ExtJs form, how can I easliy change the labels to an other language? Do I need some kind of string array? Are there other ways?

Thanks you!

santosh.rajan
7 Sep 2008, 8:14 PM
Well here is a way you can do that. Lets assume you have a namespace called MyApp. And let us say you have an app with following labels, "Add", "Edit", "Delete" in your English language version. Create a file called MyApp_language_en.js and put the following code in that file.

MyApp.lang = {
add: "Add",
edit: "Edit",
delete: "Delete"
};

In you code you can access these variables as

....
label: MyApp.lang.add,
....
label: MyApp.lang.edit,
....
label: MyApp.lang.delete,
....

Now if you want a different language version of you application, lets say french, just copy MyApp_language_en.js to MyApp_language_fr.js and translate the values in quotes ie add, edit, delete to french. Your MyApp_language_fr.js will look like this.

MyApp.lang = {
add: "Ajoutez",
edit: "Editez",
delete: "Suppression"
};
(My French is not very good this is babelfish translation for the words.)
And just load MyApp.language_fr instead of MyApp_language_en for your french version of the application.

I hope i this is the answer you were looking for.