# Localizing widget display text

Widget display text can be localized at run time to match with the current system language.

### To localize widget display text

1. Predefine the display text strings in separate JavaScript files that you create for each supported language.

   The name of the JavaScript file is arbitrary, but the name must be identical for all the supported languages so that you need to specify the name only once in the main HTML document.

2. In the JavaScript files, use arrays for holding the localized text strings.

   The name and structure of the array are arbitrarily implemented but they must be identical for all languages (except for the localized text strings). For an example of creating the array, see Localizing STEW.

3. Store the JavaScript files into the appropriate language project directories.

4. Provide a JavaScript file for default text strings (for example, in English) and store it in the root directory of the widget.

   The default text strings are used in case the current system language does not match with any provided local language. The name of the default text strings file must be identical to the name of the localized text strings files.

### Example

The following pieces of code provide an example of localizing display text to English and Finnish:

Specified in the main HTML document:

    <script type="text/javascript" src="localizedTextStrings.js" />

Implemented in a `localizedTextStrings.js` JavaScript file:

English version for the English language directory `en.lproj`:

      var localizedMenu = new Array();
      localizedMenu['setting'] = "Settings";
      localizedMenu['open'] = "Open";
      localizedMenu['close'] = "Close";

      var localizedInfo = new Array();
      localizedInfo['congrat'] = "Congratulations!";

Finnish version for the Finnish language directory `fi.lproj`:

      var localizedMenu = new Array();
      localizedMenu['setting'] = "Asetukset";
      localizedMenu['open'] = "Avaa";
      localizedMenu['close'] = "Sulje";

      var localizedInfo = new Array();
      localizedInfo[0] = "Onneksi olkoon!";

Default version in English to be stored in the widget root directory:

      var localizedMenu = new Array();
      localizedMenu['setting'] = "Settings";
      localizedMenu['open'] = "Open";
      localizedMenu['close'] = "Close";

      var localizedInfo = new Array();
      localizedInfo['congrat'] = "Congratulations!";

Defined in the JavaScript file that implements the logic of the widget:

    function init() {
      var settingMenuItem = new MenuItem(loadLocalizedMenu("setting"), 10);
      var openMenuItem = new MenuItem(loadLocalizedMenu("open"), 11);
      var playMenuItem = new MenuItem(loadLocalizedMenu("close"), 12);
      ...
    	 displayInfo(0);
    }
    function displayInfo(index) {
      alert(localizedInfo[index]);
    }
    function loadLocalizedMenu(index) {
      if (localizedMenu[index])
        return localizedMenu[index];
      else
        return "not found";
    }