Created
December 20, 2010 21:40
-
-
Save jdonaldson/749043 to your computer and use it in GitHub Desktop.
quick and dirty localization example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | |
} | |
} |
Let's see if I can just send this working project zip.
…-Justin
On Wed, Oct 12, 2011 at 5:39 PM, Vic C < ***@***.***>wrote:
Do you have a sample of the xml file you pass in?
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/749043
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have a sample of the xml file you pass in?