Previous Tutorial: Developing a Multi-Language Application with ActionScript 2.0
3 of 4
Next

LanguageManager.as

class LanguageManager {
	
   public static var LANG:Array;
   private static var _instance:LanguageManager = null;
   private var _xmlPath:String;
   private var _xmlDoc:XML;
	
   // constructor 
   private function LanguageManager() {
      // empty
   }
	
   // function getLanguageManager()
   public static function getLanguageManager():LanguageManager {
      if (LanguageManager._instance == null) {
         LanguageManager._instance = new LanguageManager();
      }
      return LanguageManager._instance;
   }
	
   // function init()
   public function init(lang:String): Void {
      _xmlPath = "languages/"+lang+".xml";
      loadXMLFile();
   }
	
   // function loadXMLFile()
   private function loadXMLFile(): Void {
      var _this:LanguageManager = this;
      _xmlDoc = new XML();
      _xmlDoc.ignoreWhite = true;
      _xmlDoc.onLoad = function(success:Boolean): Void {
         if (success) {
            _this.xml2Array();
         }
      }
      _xmlDoc.load(_xmlPath);
   }
	
   // function xml2Array()
   private function xml2Array() {
      LanguageManager.LANG = new Array();
      // loop through nodes
      for (var i:Number = 0; i < _xmlDoc.firstChild.childNodes.length; i++) {
         var node:XML = _xmlDoc.firstChild.childNodes[i];
         var nodeName:String = node.nodeName;
         var val:String = node.firstChild.nodeValue;
         LanguageManager.LANG[nodeName] = val;
      }
      // continue with your application 
      Main.getMain().doSomething();
   }
	
}

You will notice that this class is also based on the Singleton design pattern which makes sense since you usually do not need more than one language manager in your application. The most interesting property in this class is this array:

public static var LANG:Array;

This array will contain the language resources. Since it is a public static property we can later access it from any part of our application by referencing it as "LanguageManager.LANG".

But how is the "LanguageManager.LANG" array populated? Well, the ActionScript techniques that we use are pretty standard so we just describe in general what happens in the "init()", "loadXMLFile()", and "xml2Array()" methods.

What "init()" basically does is to build the path of the XML file. It then triggers the loading of the XML file. Once the XML file is loaded it gets parsed and converted into an associative array by the "xml2Array()" method. An associative array uses strings as key values instead of integers. By looping through the XML nodes each tag identifier (or node name) becomes a key:

   for (var i:Number = 0; i < _xmlDoc.firstChild.childNodes.length; i++) {
      var node:XML = _xmlDoc.firstChild.childNodes[i];
      var nodeName:String = node.nodeName;
      var val:String = node.firstChild.nodeValue;
      LanguageManager.LANG[nodeName] = val;
   }

The last line of the "xml2Array()" method is very important:

Main.getMain().doSomething();

At this point the application leaves the "LanguageManager" and gives the control back to the "Main" class. As I mentioned above, the "Main" class is not only responsible for the initialization of the application but also for the application flow. In our example the method "doSomething" is called. So let's return to our "Main" class again and see what "doSomething" actually does.

Previous 3 of 4 Next