Localisation with xml.Proxy

Here an example too show how to make a simple localisation file base on xml.Proxy.
The example is meant for the neko target, you'll have to adapt the code for another target.

the xml source used in this example : localisation.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <item id="film01">
        <en>Code of Silence</en>
        <fr>Sale temps pour un flic</fr>
    </item>
    <item id="film02">
        <en>Fire Down Below</en>
    </item>
</data>
Note : ids can not begin with a digit [0-9]

CLocals.hx

class CLocalsProxy extends haxe.xml.Proxy<"assets/localisation.xml", String> { }

class CLocals {
    public static var all  = new Hash<String>();
    public static var text  = new CLocalsProxy(all.get);
    
    public static function set( ?_lang = "en" ) : Void {
        var l_xmlString = neko.io.File.getContent( Glb.PROJECT_PATH+"Src/assets/localisation.xml" );
        var l_xmlFast   = new haxe.xml.Fast( Xml.parse( l_xmlString ).firstElement() );
        for ( i_node in l_xmlFast.nodes.item ) {
            var l_text : String = switch ( _lang ) {
                case "en" : i_node.node.en.innerData;
                // if there's no french, use english instead
                case "fr" : ( i_node.hasNode.fr ) ? i_node.node.fr.innerData : i_node.node.en.innerData;
            }
            all.set( i_node.att.id, l_text );
        }
    }
}

You'll have to set your language :

CLocals.set( "fr" );

After that you'll be able to access your texts using for instance :
trace( CLocals.text.film01 );  // Sale temps pour un flic
trace( CLocals.text.film02 );  // Fire Down Below

related links :
haxe.xml.Fast
neko.io.File

version #10805, modified 2011-08-07 22:26:23 by bubblebenj
0 comment