Skip to content

Instantly share code, notes, and snippets.

@jdonaldson
Created December 20, 2010 21:40
Show Gist options
  • Save jdonaldson/749043 to your computer and use it in GitHub Desktop.
Save jdonaldson/749043 to your computer and use it in GitHub Desktop.
quick and dirty localization example
import haxe.macro.Context;
import haxe.xml.Fast;
import haxe.macro.Expr;
class Localization
{
#if macro
static var id_hash = new Hash<String>();
#end
public static function main(): Void
{
init('assets/norwegian.xml');
trace(localize('welcome_text'));
}
@:macro public static function init(e:Expr) : Expr {
var xml_loc:String;
switch(e.expr){
case EConst(c): switch(c){
case CString(s): xml_loc = s;
default:{}
}
default:{}
}
var xml_s = neko.io.File.getContent(xml_loc);
var xml = Xml.parse(xml_s);
var fast = new Fast(xml);
for (x in fast.node.strings.nodes.string){
if (x.has.id) id_hash.set(x.att.id,x.innerData);
}
return e;
}
@:macro public static function localize(e:Expr) : Expr {
var field:String;
switch(e.expr){
case EConst(c): switch(c){
case CString(s): field = s;
default:{}
}
default:{}
}
if (!id_hash.exists(field)){Context.error('This field does not exist',e.pos);}
var result = id_hash.get(field);
return {expr:EConst(CString(result)),pos:e.pos};
}
}
Copy link

ghost commented Oct 13, 2011

Do you have a sample of the xml file you pass in?

@jdonaldson
Copy link
Author

jdonaldson commented Oct 13, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment