3 Replies Latest reply on Aug 7, 2007 2:03 AM by Thomas Friedl

    Bindable + XML Language File

    Thomas Friedl Level 1
      Edit: The binding mechanism works now, but I can't figure out how to bind to a function in actionscript. I want to bind to: Lang.getInstance().getText('name')

      ----------------

      I'm making a flex application for multiple languages. I want to put the labels/text into a XML file, load it at the beginning and bind my labels to this XML file. The problem is that often the XML hasn't finished loading and the binding mechanism doesn't work - this results into empty labels. I don't know what I'm doing wrong. I load the XML file and after loading I dispatch an event for my bindable function which provides the text. I allready tried extending the UIComponent Class (do I need this do recieve events?)

      Here the code:

      package MainPackage
      {
      import flash.events.Event;
      import flash.events.EventDispatcher;
      import flash.net.URLLoader;
      import flash.net.URLRequest;
      import mx.core.Application;
      import mx.controls.Alert;
      import mx.core.UIComponent;

      public class Lang extends UIComponent
      {
      private static var instance_:Lang;
      private var xml_file:XML;
      private var application:Application;

      //------------------------------------------------------------------
      public static function getInstance():Lang
      {
      if(instance_ == null)
      instance_ = new Lang();

      return instance_;
      }

      //------------------------------------------------------------------
      public function Lang():void
      {
      }

      //------------------------------------------------------------------
      public function loadLangFile(application:Application):void
      {
      this.application = application;
      var loader:URLLoader = new URLLoader();
      var request:URLRequest = new URLRequest("x_lang.xml");
      loader.addEventListener(Event.COMPLETE, handleLoadLangFile);
      loader.load(request);
      }

      //------------------------------------------------------------------
      private function handleLoadLangFile(event:Event):void
      {
      var loader:URLLoader = event.target as URLLoader;
      if(loader != null)
      {
      xml_file = new XML(loader.data);
      var e:Event = new Event("xmlLoaded");
      this.dispatchEvent(e);
      //application.executeBindings(true);
      //application.executeChildBindings(true);
      trace("file loaded");
      //Alert.show("file loaded");
      }
      else
      {
      trace("loader error");
      }
      }

      //------------------------------------------------------------------
      [Bindable(event="xmlLoaded")]
      public function getText(name:String):String
      {
      //Alert.show("getText called with " + name);
      trace("getText called with " + name);

      if(xml_file == null)
      return "";

      var label:XMLList = xml_file.entry.(@name == name);
      return label[0].@value;
      }
      }
      }


      The binding looks like this:
      <mx:Label text="{Lang.getInstance().getText('product_search')}"/>