AS3: Dealing with multi-language data in your application
posted in /home/ActionScript on 2011-06-16 |
I'm a big fen of the MVC pattern. I like the idea to store my data in one place and be able to get it fast. In most of my projects I used a class called Storage for similar purpose. It supports a multi-language data saving, so I decided to share it with you.
I prefer to use the Storage class as a static one, so I can access it from anywhere. It has only four methods and one property:
You can store as many values as you want. And once you set the language the class will return the correct value. For example:
Normally I'm using the Storage class for different types of information. Not only for the text data, but for urls, application settings and so on. It is obvious that such kind of information is the same for all the languages. For example:The code above will print out:That's because I didn't specify a language in addValue method. I.e. the value http://www.site.com is associated with key site-url for language = "". The Storage firstly checks if there is a value for the specific key, which matches the current language. If there is no such kind of record then simply returns the value associated with the key but with empty language.
The class is very useful when you have a multi-language application and language switcher. What you have to do is to add the texts for all the languages and simply change the language property.
You can download the source code of that example and the Storage.as class from here.
Storage.as:
I prefer to use the Storage class as a static one, so I can access it from anywhere. It has only four methods and one property:
- addValue(key:String, value:*, language:String = "") - stores value associate with key for language
- getValue(key:String) - gets the value associate with key for the current language
- editValue(key:String, value:*, language:String = "") - edits value associate with key for language
- clear() - clears all the stored values
- language (getter/setter) - get/set the current language
You can store as many values as you want. And once you set the language the class will return the correct value. For example:
Normally I'm using the Storage class for different types of information. Not only for the text data, but for urls, application settings and so on. It is obvious that such kind of information is the same for all the languages. For example:The code above will print out:That's because I didn't specify a language in addValue method. I.e. the value http://www.site.com is associated with key site-url for language = "". The Storage firstly checks if there is a value for the specific key, which matches the current language. If there is no such kind of record then simply returns the value associated with the key but with empty language.
The class is very useful when you have a multi-language application and language switcher. What you have to do is to add the texts for all the languages and simply change the language property.
You can download the source code of that example and the Storage.as class from here.
Storage.as:
Delicious