Using the Locale class

The Locale class (mx.lang.Locale) allows you to control how multilanguage text is displayed in a Flash application at runtime. With the Strings panel, you can use string IDs instead of string literals in dynamic text fields, which allows you to create a SWF file that displays text loaded from a language-specific XML file. You can use the following methods to display the language-specific strings contained in the XML Localization Interchange File Format (XLIFF) files.

Automatically at runtime Flash Player replaces string IDs with strings from the XML file, which matches the default system language code that is returned by language (capabilities.language property).

Manually using stage language String IDs are replaced by strings when the SWF file compiles, and cannot be changed by Flash Player.

Using ActionScript at runtime You can control string ID replacement by using ActionScript, which is controlled at runtime. This option gives you control over both the timing and the language of string ID replacement.

You can use the properties and methods of the Locale class when you want to replace the string IDs by using ActionScript to control the application when it plays in Flash Player. For a demonstration of how to use the Locale class, see the following procedure.

To use the Locale class to create multilanguage sites:

  1. Create a new Flash document and save it as locale.fla.
  2. Open the Strings panel (Window > Other Panels > Strings) and click Settings.
  3. Select two languages, en (English) and fr (French), and click Add to add the languages to the Active languages pane.
  4. Select the Via ActionScript at Runtime option, set the default runtime language to French, and click OK.
  5. Drag a ComboBox component from the User Interface folder of the Components panel (Window > Components) onto the Stage and give it the instance name lang_cb.
  6. Create a dynamic text field on the Stage using the Text tool and give the text field the instance name greeting_txt.
  7. With the text field selected on the Stage, type a string identifier of greeting in the ID text box of the Strings panel and click Apply.

    You'll notice that Flash converts the greeting string into IDS_GREETING.

  8. In the String panel's grid, type the string hello in the en column.
  9. Type the string bonjour in the fr column.

    You use these strings when you use the lang_cb combo box to change the language on the Stage.

  10. Add the following ActionScript to Frame 1 of the main Timeline:
    import mx.lang.Locale;
    Locale.setLoadCallback(localeListener);
    lang_cb.dataProvider = Locale.languageCodeArray.sort();
    lang_cb.addEventListener("change", langListener);
    greeting_txt.autoSize = "left";
    Locale.loadLanguageXML(lang_cb.value);
    
    function langListener(eventObj:Object):Void {
        Locale.loadLanguageXML(eventObj.target.value);
    }
    function localeListener(success:Boolean):Void {
        if (success) {
            greeting_txt.text = Locale.loadString("IDS_GREETING");
        } else {
            greeting_txt.text = "unable to load language XML file.";
        }
    }
    

    The preceding ActionScript is split into two sections. The first section of code imports the Locale class and specifies a callback listener that is called whenever a language XML file is finished loading. Next, the lang_cb combo box is populated with a sorted array of available languages. Whenever the lang_cb value changes, Flash's event dispatcher triggers the langListener() function, which loads the specified-language XML file. The second section of code defines two functions, langListener(), and localeListener(). The first function, langListener(), is called whenever the lang_cb combo box's value is changed by the user. The second function, localeListener(), is called whenever a language XML file is finished loading. The function checks if the load was successful, and if so, sets the text property of the greeting_txt instance to the greeting for the selected language.

  11. Select Control > Test Movie to test the Flash document.

TIP

The XML file that you use must use the XML Localization Interchange File Format (XLIFF).

CAUTION

The Locale class is different from the other classes in the ActionScript 2.0 Language Reference, since it is not part of the Flash Player. Because this class installed in the Flash Authoring classpath, it is automatically compiled into your SWF files. Using the Locale class increases the SWF file size slightly, because the class must be compiled into the SWF file.

For more information, see Locale (mx.lang.Locale) in the ActionScript 2.0 Language Reference.