Packagemx.lang
Classpublic class Locale
InheritanceLocale Inheritance Object

Language version: ActionScript 2.0
Player version: Flash Player 7

The mx.lang.Locale class allows you to control how multilanguage text is displayed in a SWF file. The Flash Strings panel allows you to use string IDs instead of string literals in dynamic text fields. This allows you to create a SWF file that displays text loaded from a language-specific XML file. The XML file must use the XML Localization Interchange File Format(XLIFF). There are three ways to display the language-specific strings contained in the XLIFF files:

You can use the properties and methods of this class when you want to replace the string IDs "via ActionScript at runtime."

All of the properties and methods available are static, which means that they are accessed through the mx.lang.Locale class itself rather than through an instance of the class.

Note: 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. Since this class installed in the Flash Authoring classpath it is automatically compiled into your SWF files. Using the Locale class increases the SWF size slightly since the class is compiled into the SWF.



Public Properties
 Property
  autoReplace : Boolean
[static]Determines whether strings are replaced automatically after loading the XML file.
  languageCodeArray : Array
[static][read-only]An array containing language codes for the languages that have been specified or loaded into the FLA file.
  stringIDArray : Array
[static][read-only]An array containing all the string IDs in the FLA file.
 Properties inherited from class Object
 __proto__, __resolve, constructor, prototype
Public Methods
 Method
  
addDelayedInstance(instance:Object, stringID:String):Void
[static]Adds the {instance, string ID} pair into the internal array for later use.
  
addXMLPath(langCode:String, path:String):Void
[static]Adds the {languageCode and languagePath} pair into the internal array for later use.
  
[static]Returns true if the XML file is loaded; false otherwise.
  
[static]The default language code as set in the Strings panel dialog box or by calling the setDefaultLang() method.
  
initialize():Void
[static]Automatically determines the language to use and loads the XML language file.
  
loadLanguageXML(xmlLanguageCode:String, customXmlCompleteCallback:Function):Void
[static]Loads the specified XML language file.
  
[static]Returns the string value associated with the given string ID in the current language.
  
loadStringEx(stringID:String, languageCode:String):String
[static]Returns the string value associated with the given string ID and language code.
  
setDefaultLang(langCode:String):Void
[static]Sets the default language code.
  
setLoadCallback(loadCallback:Function):Void
[static]Sets the callback function that is called after the XML file is loaded.
  
setString(stringID:String, languageCode:String, stringValue:String):Void
[static]Sets the new string value of a given string ID and language code.
 Methods inherited from class Object
 addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch
Property detail
autoReplaceproperty
public static var autoReplace:Boolean

Language version: ActionScript 2.0
Player version: Flash Player 8

Determines whether strings are replaced automatically after loading the XML file. If set to true, the text replacement method is equivalent to the Strings panel setting "automatically at runtime". This means that Flash Player will determine the default language of the host environment and automatically display the text in that language. If set to false, the text replacement method is equivalent to the Strings panel setting "via ActionScript at runtime". This means that you are responsible for loading the appropriate XML file to display the text.

The default value of this property reflects the setting that you select for Replace strings in the Strings panel dialog box: true for "automatically at runtime" (the default setting) and false for "via ActionScript at runtime".


Example
The following example uses the Locale.autoReplace property to populate the dynamically created greeting_txt text field on the Stage with the contents of the IDS_GREETING string in the English XML file. In the Strings panel, click the Settings button to open the Settings dialog box. You can add two active languages using the Settings dialog box: English (en) and French (fr), set the replacement strings radio option to "via ActionScript at runtime", and click OK. Finally, enter a string ID of IDS_GREETING in the Strings panel, and add text for each active language.
import mx.lang.Locale;
this.createTextField("greeting_txt", 10, 40, 40, 200, 20);
greeting_txt.autoSize = "left";
Locale.autoReplace = true;
Locale.addDelayedInstance(greeting_txt, "IDS_GREETING");
Locale.loadLanguageXML("en");

languageCodeArrayproperty 
languageCodeArray:Array  [read-only]

Language version: ActionScript 2.0
Player version: Flash Player 8

An array containing language codes for the languages that have been specified or loaded into the FLA file. The language codes are not sorted alphabetically.

Implementation
    public static function get languageCodeArray():Array

Example
The following example loads a language XML file based on the current value of a ComboBox component. You drag a ComboBox component onto the Stage and give it an instance name of lang_cb. Using the Text tool, you create a dynamic text field and give it an instance name of greeting_txt. In the Strings panel, you add at least two active languages, set the replace strings radio option to "via ActionScript at runtime", and click OK. Next, you add a string ID of IDS_GREETING and enter text for each active language. Finally, you add the following ActionScript code 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);

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.";
    }
}

stringIDArrayproperty 
stringIDArray:Array  [read-only]

Language version: ActionScript 2.0
Player version: Flash Player 8

An array containing all the string IDs in the FLA file. The string IDs are not sorted alphabetically.

Implementation
    public static function get stringIDArray():Array

Example
The following example traces the Locale.stringIDArray property for the currently loaded language XML file. Click the Settings button in the Strings panel to open the Settings dialog box. Next, you add two active languages: English (en) and French (fr), set the replace strings radio control to "via ActionScript at runtime", and click OK. In the Strings panel, you add a string ID of IDS_GREETING, and then add text for each active language.
import mx.lang.Locale;
Locale.setLoadCallback(localeCallback);
Locale.loadLanguageXML("fr");
function localeCallback(success:Boolean) {
    trace(success);
    trace(Locale.stringIDArray); // IDS_GREETING
    trace(Locale.loadStringEx("IDS_GREETING", "fr")); // bonjour
}

Method detail
addDelayedInstance()method
public static function addDelayedInstance(instance:Object, stringID:String):Void

Language version: ActionScript 2.0
Player version: Flash Player 7

Adds the {instance, string ID} pair into the internal array for later use. This is primarily used by Flash when the strings replacement method is "automatically at runtime".

Parameters
instance:Object — Instance name of the text field to populate.
 
stringID:String — Language string ID.

Example
The following example uses the autoReplace property and addDelayedInstance() method to populate a text field on the Stage with the IDS_GREETING string from the English XML language file.
import mx.lang.Locale;
greeting_txt.autoSize = "left";
Locale.autoReplace = true;
Locale.addDelayedInstance(greeting_txt, "IDS_GREETING");
Locale.loadLanguageXML("en");

addXMLPath()method 
public static function addXMLPath(langCode:String, path:String):Void

Language version: ActionScript 2.0
Player version: Flash Player 7

Adds the {languageCode and languagePath} pair into the internal array for later use. This is primarily used by Flash when the strings replacement method is "automatically at runtime" or "via ActionScript at runtime".

Parameters
langCode:String — The language code.
 
path:String — The XML path to add.

Example
The following example uses the setInterval() method to check whether the language XML file has successfully loaded.
import mx.lang.Locale;
Locale.setLoadCallback(localeCallback);
Locale.loadLanguageXML("en");
// create interval to check if language XML file is loaded
var locale_int:Number = setInterval(checkLocaleStatus, 10);
function checkLocaleStatus():Void {
    if (Locale.checkXMLStatus()) {
        clearInterval(locale_int);
        trace("clearing interval @ " + getTimer() + " ms");
    }
}
// callback function for Locale.setLoadCallback()
function localeCallback(success:Boolean):Void {
    greeting_txt.text = Locale.loadString("IDS_GREETING");
}

checkXMLStatus()method 
public static function checkXMLStatus():Boolean

Language version: ActionScript 2.0
Player version: Flash Player 7

Returns true if the XML file is loaded; false otherwise.

Returns
Boolean — Returns true if the XML file is loaded; false otherwise.

Example
The following example uses an interval to check every 10 milliseconds to see if the language file has successfully loaded. Once the XML file has loaded, the greeting_txt text field instance on the Stage is populated with the IDS_GREETING string from the language XML file.
import mx.lang.Locale;
Locale.setLoadCallback(localeCallback);
Locale.loadLanguageXML("en");
// create interval to check if language XML file is loaded
var locale_int:Number = setInterval(checkLocaleStatus, 10);
function checkLocaleStatus():Void {
    if (Locale.checkXMLStatus()) {
        clearInterval(locale_int);
        trace("clearing interval @ " + getTimer() + " ms");
    }
}
// callback function for Locale.setLoadCallback()
function localeCallback(success:Boolean):Void {
    greeting_txt.text = Locale.loadString("IDS_GREETING");
}

getDefaultLang()method 
public static function getDefaultLang():String

Language version: ActionScript 2.0
Player version: Flash Player 8

The default language code as set in the Strings panel dialog box or by calling the setDefaultLang() method.

Returns
String — Returns the default language code.

See also


Example
The following example creates a variable called defLang, which is used to hold the initial default language for the Flash document. You click the Settings button in the Strings panel to launch the Settings dialog box. Then you add two active languages: English (en) and French (fr), set the replace strings radio control to "via ActionScript at runtime", and click OK. In the Strings panel, you add a string ID of IDS_GREETING, and then add text for each active language.
import mx.lang.Locale;
var defLang:String = "fr";
Locale.setDefaultLang(defLang);
Locale.setLoadCallback(localeCallback);
Locale.loadLanguageXML(Locale.getDefaultLang());
function localeCallback(success:Boolean) {
    if (success) {
        trace(Locale.stringIDArray); // IDS_GREETING
        trace(Locale.loadString("IDS_GREETING"));
    } else {
        trace("unable to load XML");
    }
}

initialize()method 
public static function initialize():Void

Language version: ActionScript 2.0
Player version: Flash Player 7

Automatically determines the language to use and loads the XML language file. This is primarily used by Flash when the strings replacement method is "automatically at runtime".


Example
This example shows how to use the initialize() method to automatically populate the greeting_txt text field on the Stage with the user's current OS language. Instead of using the initialize() method directly, use the string replacement method of "automatically at runtime".
import mx.lang.Locale;
trace(System.capabilities.language);
Locale.autoReplace = true;
Locale.addDelayedInstance(greeting_txt, "IDS_GREETING");
Locale.initialize();

loadLanguageXML()method 
public static function loadLanguageXML(xmlLanguageCode:String, customXmlCompleteCallback:Function):Void

Language version: ActionScript 2.0
Player version: Flash Player 8

Loads the specified XML language file.

Parameters
xmlLanguageCode:String — The language code for the XML language file that you want to load.
 
customXmlCompleteCallback:Function — Custom callback function to call when XML language file loads.

Example
The following example uses the loadLanguageXML() method to load the English (en) XML language file. Once the language file loads, the localeCallback() method is called and populates the greeting_txt text field on the Stage with the contents of the IDS_GREETING string in the XML file.
import mx.lang.Locale;
Locale.setLoadCallback(localeCallback);
Locale.loadLanguageXML("en");
// create interval to check if language XML file is loaded
var locale_int:Number = setInterval(checkLocaleStatus, 10);
function checkLocaleStatus():Void {
    if (Locale.checkXMLStatus()) {
        clearInterval(locale_int);
        trace("clearing interval @ " + getTimer() + " ms");
    }
}
// callback function for Locale.setLoadCallback()
function localeCallback(success:Boolean):Void {
    greeting_txt.text = Locale.loadString("IDS_GREETING");
}

loadString()method 
public static function loadString(id:String):String

Language version: ActionScript 2.0
Player version: Flash Player 7

Returns the string value associated with the given string ID in the current language.

Parameters
id:String — The identification (ID) number of the string to load.

Returns
String — The string value associated with the given string ID in the current language.

See also


Example
The following example uses an interval to check every 10 milliseconds to see if the language file has successfully loaded. Once the XML file has loaded, the greeting_txt text field instance on the Stage is populated with the IDS_GREETING string from the XML language file.
import mx.lang.Locale;
Locale.setLoadCallback(localeCallback);
Locale.loadLanguageXML("en");
// create interval to check if language XML file is loaded
var locale_int:Number = setInterval(checkLocaleStatus, 10);
function checkLocaleStatus():Void {
    if (Locale.checkXMLStatus()) {
        clearInterval(locale_int);
        trace("clearing interval @ " + getTimer() + " ms");
    }
}
// callback function for Locale.setLoadCallback()
function localeCallback(success:Boolean):Void {
    greeting_txt.text = Locale.loadString("IDS_GREETING");
}

loadStringEx()method 
public static function loadStringEx(stringID:String, languageCode:String):String

Language version: ActionScript 2.0
Player version: Flash Player 8

Returns the string value associated with the given string ID and language code. To avoid unexpected XML file loading, loadStringEx() does not load the XML language file if the XML file is not already loaded. You should decide on the right time to call the loadLanguageXML() method if you want to load a XML language file.

Parameters
stringID:String — The identification (ID) number of the string to load.
 
languageCode:String — The language code.

Returns
String — The string value associated with the given string ID in the language specified by the languageCode parameter.

See also


Example
The following example uses the loadStringEx() method to trace the value of the IDS_GREETING string for the currently loaded French language XML file.
import mx.lang.Locale;
Locale.setLoadCallback(localeCallback);
Locale.loadLanguageXML("fr");
function localeCallback(success:Boolean) {
    trace(success);
    trace(Locale.stringIDArray); // IDS_GREETING
    trace(Locale.loadStringEx("IDS_GREETING", "fr")); // bonjour
}

setDefaultLang()method 
public static function setDefaultLang(langCode:String):Void

Language version: ActionScript 2.0
Player version: Flash Player 7

Sets the default language code.

Parameters
langCode:String — A string representing a language code.

See also


Example
The following example creates a variable called defLang, which is used to hold the initial default language for the Flash document. You click the Settings button in the Strings panel to open the Settings dialog box. Then you add two active languages: English (en) and French (fr), set the replace strings radio control to "via ActionScript at runtime", and click OK. In the Strings panel, you add a string ID of IDS_GREETING, and then add text for each active language.
import mx.lang.Locale;
var defLang:String = "fr";
Locale.setDefaultLang(defLang);
Locale.setLoadCallback(localeCallback);
Locale.loadLanguageXML(Locale.getDefaultLang());
function localeCallback(success:Boolean) {
    if (success) {
        trace(Locale.stringIDArray); // IDS_GREETING
        trace(Locale.loadString("IDS_GREETING"));
    } else {
        trace("unable to load XML");
    }
}

setLoadCallback()method 
public static function setLoadCallback(loadCallback:Function):Void

Language version: ActionScript 2.0
Player version: Flash Player 7

Sets the callback function that is called after the XML file is loaded.

Parameters
loadCallback:Function — The function to call when the XML language file loads.

Example
The following example uses an interval to check every 10 milliseconds to see if the language file has successfully loaded. Once the XML file has loaded, the greeting_txt text field instance on the Stage is populated with the IDS_GREETING string from the XML language file.
import mx.lang.Locale;
Locale.setLoadCallback(localeCallback);
Locale.loadLanguageXML("en");
// create interval to check if language XML file is loaded
var locale_int:Number = setInterval(checkLocaleStatus, 10);
function checkLocaleStatus():Void {
    if (Locale.checkXMLStatus()) {
        clearInterval(locale_int);
        trace("clearing interval @ " + getTimer() + " ms");
    }
}
// callback function for Locale.setLoadCallback()
function localeCallback(success:Boolean):Void {
    greeting_txt.text = Locale.loadString("IDS_GREETING");
}

setString()method 
public static function setString(stringID:String, languageCode:String, stringValue:String):Void

Language version: ActionScript 2.0
Player version: Flash Player 8

Sets the new string value of a given string ID and language code.

Parameters
stringID:String — The identification (ID) number of the string to set.
 
languageCode:String — The language code.
 
stringValue:String — A string value.

Example
The following example uses the setString() method to set the IDS_WELCOME string for both English (en) and French (fr).
import mx.lang.Locale;
Locale.setString("IDS_WELCOME", "en", "hello");
Locale.setString("IDS_WELCOME", "fr", "bonjour");
trace(Locale.loadStringEx("IDS_WELCOME", "en")); // hello