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:

view plain print about
1var language:String = System.capabilities.language;
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.

view plain print about
1for varr childXML:XMLNode = resourceXML.firstChild.firstChild; childXML != null; childXML=childXML.nextSibling) {
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:

view plain print about
1// Load UI strings from XML based on Locale
2
var resources:Object = new Object();
3
4var resourceXML:XML = new XML();
5var defaultlanguage:String = 'en'; // default to english
6
var 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
27
function 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:

view plain print about
1#include "code/language.as"
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
9
function 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
20
loadResources();

Related Blog Entries

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
I like your scripting very nice but the approach of using 'System.capabilities.language' isn't the best as that will only detect regional/installed lang packs on the local machine, without the use of 'HTTP_ACCEPT_LANGUAGE' it would pickup browser language preferance. I have build a nice script with the use of PHP/flash everythign is workign perfectly but. when i use it in flash/ie doesn't load in the correct variable. if i load just php displays correct vars. then when i reload the flash displays correct var/lang code. have you come across anything liek this?
# Posted By 1nf3c710n | 6/30/08 2:40 AM
If you look the code just below System.capabilities.language you see that you can also set the language by passing it in with a URL parameter called lang. This could come from HTTP_ACCEPT_LANGUAGE if the page was a dynamic page (ie generated by php, ColdFuion, asp etc).
# Posted By Justin Mclean | 6/30/08 7:27 AM
Hi, you mention at the top of this blog entry that "Flash has good support for internationalization", however I'm struggling to find any offical documentation from Adobe about how they recommend building i18n in your Flash app. Do you know of any good references from Adobe on this? Thanks, Ollie
# Posted By Oliver Powell | 12/9/08 2:18 PM
Here's a good starting place:
http://www.adobe.com/support/flash/languages.html

Some of the information refers to previous versions of flash but are still useful.
# Posted By Justin Mclean | 5/18/09 5:22 PM