From e2361b66a073c5c3e9072008d983f987e55443b8 Mon Sep 17 00:00:00 2001 From: shmyga Date: Tue, 30 Jun 2015 12:43:07 +0300 Subject: [PATCH] added Locale String --- haxework/gui/GuiBuilder.hx | 12 ++++++++---- haxework/locale/ILocale.hx | 1 + haxework/locale/LString.hx | 38 ++++++++++++++++++++++++++++++++++++++ haxework/locale/Locale.hx | 9 +++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 haxework/locale/LString.hx diff --git a/haxework/gui/GuiBuilder.hx b/haxework/gui/GuiBuilder.hx index 0cf3a85..a62f2d1 100755 --- a/haxework/gui/GuiBuilder.hx +++ b/haxework/gui/GuiBuilder.hx @@ -2,6 +2,7 @@ package haxework.gui; //ToDo: +import haxework.locale.LString; import haxe.Json; import flash.errors.Error; import openfl.Assets; @@ -126,7 +127,7 @@ class GuiF { if (s.charAt(1) == "~") { var a:Array = s.substr(2).split(":"); switch (a[0]) { - case "images": value = Assets.getBitmapData(a[1]); + case "image": value = Assets.getBitmapData(a[1]); case "layout": value = GuiBuilder.buildFromAssets(a[1], null, links); } } else { @@ -172,17 +173,20 @@ class GuiF { var e:Enum = Type.resolveEnum(a[0]); value = Type.createEnum(e, a[1]); } else if (c == "@") { - if (s.charAt(1) == "~") { + var c1 = s.charAt(1); + if (c1 == "~") { var a:Array = s.substr(2).split(":"); switch (a[0]) { - case "image": Reflect.setProperty(object, field, Assets.getBitmapData(a[1])); + case "image": value = Assets.getBitmapData(a[1]); } + } else if (c1 == "^") { + value = new LString(s.substr(2)); } else { var a:Array = s.substr(1).split(":"); //value = Reflect.field(Provider.get(IResources), a[0]).get(a[1]); Reflect.field(Provider.get(IResources), a[0]).bind(a[1], object, field); + continue; } - continue; } else if (~/0x[A-Fa-f\d]{6}/.match(value)) { value = Std.parseInt(value); } diff --git a/haxework/locale/ILocale.hx b/haxework/locale/ILocale.hx index 6c98b27..d17bad5 100755 --- a/haxework/locale/ILocale.hx +++ b/haxework/locale/ILocale.hx @@ -3,6 +3,7 @@ package haxework.locale; interface ILocale { public function getString(key:String):String; public function formatString(key:String, args:Array):String; + public function formatPlurar(key:String, num:Int, args:Array):String; public function getArray(key:String):Array; public function getPlural(key:String, num:Int):String; } \ No newline at end of file diff --git a/haxework/locale/LString.hx b/haxework/locale/LString.hx new file mode 100644 index 0000000..f0c482d --- /dev/null +++ b/haxework/locale/LString.hx @@ -0,0 +1,38 @@ +package haxework.locale; + +import haxework.provider.Provider; + +abstract LString(String) from String to String { + + inline public function new(value:String) { + this = Provider.get(ILocale).getString(value); + } +} + +abstract LAString(String) from String to String { + + inline public function new(value:String, index:Int) { + this = Provider.get(ILocale).getArray(value)[index]; + } +} + +abstract LPString(String) from String to String { + + inline public function new(value:String, num:Int) { + this = Provider.get(ILocale).getPlural(value, num); + } +} + +abstract LFString(String) from String to String { + + inline public function new(value:String, args:Array) { + this = Provider.get(ILocale).formatString(value, args); + } +} + +abstract LPFString(String) from String to String { + + inline public function new(value:String, num:Int, args:Array) { + this = Provider.get(ILocale).formatPlurar(value, num, args); + } +} diff --git a/haxework/locale/Locale.hx b/haxework/locale/Locale.hx index 7088a23..b591d99 100755 --- a/haxework/locale/Locale.hx +++ b/haxework/locale/Locale.hx @@ -19,6 +19,15 @@ class Locale implements ILocale { return string; } + public function formatPlurar(key:String, num:Int, args:Array):String { + var string:String = getPlural(key, num); + for (i in 0...args.length) { + var arg:Dynamic = args[i]; + string = StringTools.replace(string, "{" + i + "}", arg); + } + return string; + } + public function getString(key:String):String { return getObject(key); }