Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Working with Text and Strings > About strings and the String class > 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: You'll notice that Flash converts the greeting string into IDS_GREETING.
You use these strings when you use the lang_cb combo box to change the language on the Stage.
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.
|
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.
|
|
|
|