Using JavaScript and storing all the texts locally

One common way to internationalize your web application is to remove all strings from your code and have them all in an object with key-value pairs. All the code exists locally and nothings get stored on the server side.

Now let’s assume you keep the chosen language somewhere in a variable called language. You can then access the right string for the user language, like with the cancel button text for example:

Strings[language].BUTTON_CANCEL

That will give you either ‘Cancel’ or ‘Annuleer’ depending on whether the language variable contains ‘en’ or ‘nl’.

Using JavaScript and storing all the texts on a remote server

Another way of internationalizing your application in BlackBerry WebWorks platform is to use the server side code. Unfortunately, JavaScript does not have the concept of a resource file, and strings that will be generated by JavaScript must be defined in the code.

The easiest approach to deal with this issue is to define your JavaScript strings dynamically in server-side code (Java/JSP, ASPX, PHP, etc.). The following example defines some string resources in a JavaScript script block at the top of a JSP page:

Assuming the currentLocale object is set to English (US), the resulting block should look like this:

When currentLocale is set to German (Germany) it should change to this:

There are a couple things to keep in mind with this approach. First, any strings that are embedded in the files, whether JSP/ASPX/PHP/etc. or JavaScript .js files, must be externalized, i.e. the strings should be moved into the string resource block as demonstrated above, and replaced in the code with their variable names. Second, the JavaScript string resource block should be defined before any other embedded blocks or .js file includes that make use of these externalized strings. For example, the resource block should be defined before the following function is called:

Using JavaScript Object Notation (JSON)

Finally, another way to add multi-language support to your BlackBerry WebWorks application is to use JSON objects. Here is a sample which illustrates the use of JSON objects and changes the text dynamically based on the user’s language selection: