Flash Internationalization
Flash has good support for internationalization but it does have a few restrictions such as you need to recompile your fla if you add a new language.
I've come up with a fairly simple way to load languages from XML files and to support the following:
- Language defaults to the users language
- Language can be set via a URL parameter
- New languages can be added after the swf is deployed
You can check the language to use with the following code:
2
3 // check if language passed in as URL parameter
4 if (_root.lang) {
5 langauge = _root.lang;
6 }
The following code will take XML and convert it to an object called resources.
2 resources[childXML.nodeName] = childXML.childNodes[0].nodeValue;
3 }
If the XML file fails to load (ie the language isn't supported), we default to English (our default language) and load that XML file.
Putting this all together we can place the code in two places; first an external actionscript file called language.as:
2var resources:Object = new Object();
3
4var resourceXML:XML = new XML();
5var defaultlanguage:String = 'en'; // default to english
6var timestried:Number = 0;
7
8function loadResources():Void {
9 var xmlResourceFile:String = '';
10
11 // default to system langauge
12 var language:String = System.capabilities.language;
13
14 // check if language passed in as URL parameter
15 if (_root.lang) {
16 langauge = _root.lang;
17 }
18
19 resourceXML.ignoreWhite = true;
20 resourceXML.onLoad = ResourceXMLLoaded;
21
22 // load the XML resource file
23 resourceXML.load('lang/resources_' + language + '.xml');
24}
25
26// Load XML functions
27function ResourceXMLLoaded(loaded:Boolean):Void {
28 var status:Number = this.status;
29
30 timestried++;
31
32 if (loaded && status == 0) {
33 for varr childXML:XMLNode = resourceXML.firstChild.firstChild; childXML != null; childXML=childXML.nextSibling) {
34 resources[childXML.nodeName] = childXML.childNodes[0].nodeValue;
35 }
36
37 setUIStrings(resources);
38 }
39 else {
40 if (timestried == 1) {
41 // try and use default language
42 resourceXML.load('lang/resources_' + defaultlanguage + '.xml');
43 }
44 else {
45 // if cant find user or default language show an error
46 Alert('Unable to load XML language resource file');
47 }
48 }
49}
and the code we need to place in the first frame of the fla (in the actions layer) to load the XML and set the value of the textfields:
2
3function settext(textfield:TextField, text:String):Void{
4 textfield.embedFonts = true;
5 textfield.text = text;
6}
7
8// function called when language XML is loaded
9function setUIStrings(resources:Object):Void {
10 // set text fields
11 settext(textfield1, resources.field1);
12 settext(textfield2, resources.field2);
13 settext(textfield3, resources.field3);
14 ...
15
16}
17
18
19// load XML and call above function once XML is loaded
20loadResources();
http://www.adobe.com/support/flash/languages.html
Some of the information refers to previous versions of flash but are still useful.