I recently created the multi-language version of a flash game that I had developed. Practically I had to change all the texts from static to dynamic so you can change their text. In addition, all messages sent by the program, had to be changed from fixed strings to variables. Most important of all, I had to implement a system that would make it easier to edit a text, add a new language, make sure that the user could click a button and change the language throughout the site. Logically, I decided to put all the text of the site in external files. I could have used XML file for this but I did not like their verbosity. I wanted something more like ini files that are normally used for this function. Searching the net I found an open source library that, among many other things, has a class that does just what I needed. The library is called Spring ActionScript and is the porting from Java to actionscript of an open source project. The class in question, then, is Properties. The documentation says in this respect: “The Properties class Represents a collection of properties in the form of key-value pairs.” Perfect! It was just what I was looking for. The class has only three methods, but just what I needed:

getProperty (key: String): String
Gets the value of property That corresponds to the given key.

load (url: String): void
Loads a collection of properties from an external file.
setProperty (key: String, value: String): void
Sets a property.

To be honest, I not even need the third method for this purpose.

At this point, I created my ini files. They are simple key and value pairs. Like this

! License free.
! Note : All ini files need to be saved as UTF-8
TITLE                    Title
MESSAGE                  Message
MSG_1                    Click "Close" or try to change the language
MSG_2                    You clicked "Close"
SELECT_LANGUAGES         Select the language
CLOSE                    CLose

I also created another ini to tell the program which languages ​​are present and how are named their files, here it is:

it        it-IT.language.ini
gb        en-EN.language.ini
es        es-ES.language.ini

Well, now I had to create a class that would manage everything. I created a singleton class (a design pattern that I hope all of you already know) so that was easily recalled by all classes of the program. Uhm, maybe it’s better than, if you are interested, you downloaded the zip with all the sample files so I can explain what I did without having to put all the code in the article.
Ok, first, have a look at what is inside the zip. There is the fla and swf of the example, the two external library i used in the code, the flashDevelop project, the 4 ini file used by the swf, a folder with all the flag for any type of language you could use in your project and a classes folder with all the source code. Do you think it’s enough? :)
Let’s see what the main class Languages does.
When Languages ​​is instantiated the first time, it creates a Properties, which will contain the names of the languages ​​that you have set into languages.ini and load it. Once loaded, set the current language to the first language found in languages.ini otherwise, in my case, set as default Italian. Then load the ini files for that language and, once done, does the dispatch of an event to warn that the language is loaded. You can now use the method getSentence to receive the string corresponding to a key for the current language. Another useful method of this class is changeLanguage that allows you to change the current language. Besides this main class I have also provided a class that allows you to automatically display the flags for the languages ​​present so you can easily change the language. The code, although it does not contain comments, it should be clear enough. However, if you have questions, contact me.