Archived:Flash lite localisation
The following is one way of localising texts at runtime. In order for it to work, the SWF needs to be packaged, since the localisation text files will be separate.
For a more advanced example with a helper class go to Advanced example and helper class section at the bottom of the page!
Set variables to Dynamic text fields
To be able to switch the texts at runtime, you need to define your texts as dynamic text fields and you need to set a variable for each of them.
- Add a dynamic text field (or change the text field to dynamic)
- In the field's Properties panel set the Variable field in the Options section to the desired value
Save the localisation data to an external text file
To be able to localise the text properly, the translator need some information. Please make sure you provide enough data for a correct translation to be possible. Also, it is very important to let the translator know how much room the text has on the screen.
You could format the string in the following manner, following the Symbian i18n guidelines.
//d: A descriptive text, describing the use of the text string and it's
//d: dependencies. Should be more than a few words and can extend to several
//d: lines. This must always preceed the actual string definition
//l: Information about the layout use one of the predefined values
&logical_name=The localisation string&
NOTE: all characters, also linebreaks and whitespace between the "equals" sign (=) and the next ampersand (&) will be returned as the value
Detecting the device language and loading the floc file
The following code uses first the fscommand "GetLanguage" to set the value of variable isoLang to the devices language. The returned value may vary a bit, but at least in the S60 5th edition devices the value is <ISO639‑1 language alpha-2>-<ISO3166‑1 country alpha-2>, for example en-GB or es-ES.
status = fscommand2("GetLanguage", "isoLang");
The second line uses the language information to load the variable data to the dynamic text fields. In this example, the file is given an extension .loc but it is not an official file extension...
The following example (Adobe Flash Professional CS4) uses the method described above.
Advanced example and helper class
This package contains a similar Flash CS3 FLA file as the basic example, but it includes a very handy helper class to do most of the work for you automatically. It had been developed as a part of an application already in Nokia Store after which it was generalised and open sourced for use in any other project!
What does Language.as class do for you?
- gets the system language
- intelligently tries to match it with the list of available languages
- first trying a full match
- if that fails trying to match only the language
- and finally as a last resort loading the default one you can specify
- loads the text labels from the external files automatically
- does the loading asynchronously and gives you a hook to a callback function
- makes all the labels available as static variables, so no need to pass an instance of the Language class around, you can access text labels from anywhere
- works perfectly as part of a more advanced, class based project, in fact it`s MTASC strict compliant
- copy in Language.as and Delegate.as in your project folder
- paste in to and edit code in your project from FLA example
- edit FLOC files
- update variables in Language.as based on your FLOC files (FLOC variable name = Language.as variable name in uppercase, Language.as loader handles this automatically)
- package your FLOC files together / next to your SWF
- automatically load the right language runtime before drawing your GUI