Check out "Do you speak JavaScript?" - my latest video course on advanced JavaScript.
Language APIs, Popular Concepts, Design Patterns, Advanced Techniques In the Browser

AS3: Dealing with multi-language data in your application

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:

  • • 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:

// adding values
    Storage.addValue("hello-text", "Hello, dear customers", "english");
    Storage.addValue("hello-text", "Здравейте, скъпи клиенти", "bulgarian");
    Storage.addValue("hello-text", "Hallo, liebe Kunden", "german");
    Storage.addValue("hello-text", "Hola, queridos clientes", "spanish");
    // setting the language
    Storage.language = "spanish";
    // getting the value 
    trace(Storage.getValue("hello-text"));
    // prints "Hola, queridos clientes"

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:

Storage.addValue("site-url", "http://www.site.com");
    Storage.language = "english";
    trace(Storage.getValue("site-url"));
    Storage.language = "german";
    trace(Storage.getValue("site-url"));

The code above will print out:

http://www.site.comhttp://www.site.com

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.Storage.as:

package {
    public class Storage {
      private static
      var _data: Array;
      private static
      var _language: String = "";
      public static
      function addValue(key: String, value: * , language: String = ""): void {
        if (!_data) {
          _data = [];
        }
        _data.push({
          key: key,
          value: value,
          language: language
        });
      }
      public static
      function getValue(key: String): * {
        if (_data) {
          var numOfValues: int = _data.length;
          for (var i: int = 0; i < numOfValues; i++) {
            if (_data[i].key == key && _data[i].language == _language) {
              return _data[i].value;
            } else if (_data[i].key == key && _data[i].language == "") {
              return _data[i].value;
            }
          }
        }
        return null;
      }
      public static
      function editValue(key: String, value: * , language: String = ""): void {
        if (_data) {
          var numOfValues: int = _data.length;
          for (var i: int = 0; i < numOfValues; i++) {
            if (_data[i].key == key && _data[i].language == language) {
              _data[i].value = value;
              return;
            }
          }
          Storage.addValue(key, value, language);
        } else {
          trace("Storage missing _objects !");
        }
      }
      public static
      function get language(): String {
        return _language;
      }
      public static
      function set language(l: String): void {
        _language = l;
      }
      public static
      function clear(): void {
        _data = [];
      }
    }
  }
If you enjoy this post, share it on Twitter, Facebook or LinkedIn.