I wonder what are the different and which is the best method to set up some multilingual javascript application. i want to have all used strings in one file to easily change strings or add more languages later.
thnx!
I wonder what are the different and which is the best method to set up some multilingual javascript application. i want to have all used strings in one file to easily change strings or add more languages later.
thnx!
Whatever you do, the most important thing is to separate between your code and the texts.
If the code and the texts are mixed, maintenance will be impossible and you'll soon abandon it.
The translatable texts must be easily scanned, so that translators can translate just texts. Then, you should be able to insert the translations conveniently.
We use a JS file that includes a map of strings. We have a simple Python script that extracts the translatable strings from it. The same script also builds the output JS file that includes the same labels with the translated strings.
The result is:
Here's a function I put together to handle language translations based on the accepted answer in this question:
Then to initialise it in common page code, where
${language.language}
is jsp code to set the language from a server side configuration.Then whenever you need a message use
Language.getString('hello-world', 'Hi World');
I like using a "language dictionary array", which you can do using JSON or a simple array.
This is easy to implement:
You can simply make a big object tree:
In your app:
The benefit of this system is that you can make sub objects to group some values together.