web oriented universal language

About haxe.xml.Proxy

Posted on 2007-09-13 by Nicolas Cannasse in General

In haXe 1.14 was introduced a new class called haxe.xml.Proxy.

At Motion-Twin, we are using it when we need to have some data structures stored in external XML files and that we want to reference in a program and still be sure that they exists.

Let's take a practical example. When some action occurs on our website, we can display either a confirm or an error text message. In order to ease translation of our website, we don't put any text in the sourcecode itself, so all these messages are stored in an XML file and referenced by their 'id'.

<texts>
   <t id="works">Great ! It works !</t>
</texts>

Before haxe.xml.Proxy, we were then building an Hash'table of these texts and simply having in our code :

class Texts {
   public static var all = new Hash<String>();
   // on initialization, fill 'all' with the XML content
}
Texts.all.get("works");

It works well of course, but we found several times that we mistake the spelling of the identifier, or that we duplicate messages that should be reused, and that we never remember which identifier to use to display a particular message (so having to look at the XML file everytime).

So I came up with the idea of haxe.xml.Proxy. Basically, you can declare a Proxy this way :

class AllTexts extends haxe.xml.Proxy<"texts.xml",String> {
}

The first type argument in the name of the XML file (must be in the classpath, you can add one -cp if it's in specific path) and the second one is the data structure content, String in our case, but this can be used with more complex data structures.

The AllTexts can then be instanciated with a method that will take an identifier and return a String. Our hashtable 'get' methods does just that. This way, our Texts class becomes :

class Texts {
    public static var all = new Hash<String>();
    public static var list = new AllTexts(all.get);
}

And all texts can then be replaced by using dot access :

Texts.list.works

This will do in practice exactly the same has before since at runtime it will call the hashtable 'get' method, but in will ensure at compile time that there is an node in the XML with the identifier 'works'.

The best is that that completion works very well so as soon as you write 'Texts.list.' you will get the list of all available identifiers.

Since then, we've been using this technique for many different kind of resources.

Comments

"ease translation of our website" too bad none of the newer Motion-Twin games have been translated )=

You guys are still awesome! ;D
Posted by Brian , 2007-09-28 21:34:04
btw... is there any way that I could contact you?
Posted by Brian , 2007-09-28 21:35:10

Post a comment

Name:
Email:
Url:
Security: Please enter 1518 here.
remember me
Comment:
 
 
Haxe Powered Rss flux Valid XHTML 1.0 Valid CSS