added Locale String
This commit is contained in:
@@ -2,6 +2,7 @@ package haxework.gui;
|
|||||||
|
|
||||||
|
|
||||||
//ToDo:
|
//ToDo:
|
||||||
|
import haxework.locale.LString;
|
||||||
import haxe.Json;
|
import haxe.Json;
|
||||||
import flash.errors.Error;
|
import flash.errors.Error;
|
||||||
import openfl.Assets;
|
import openfl.Assets;
|
||||||
@@ -126,7 +127,7 @@ class GuiF {
|
|||||||
if (s.charAt(1) == "~") {
|
if (s.charAt(1) == "~") {
|
||||||
var a:Array<String> = s.substr(2).split(":");
|
var a:Array<String> = s.substr(2).split(":");
|
||||||
switch (a[0]) {
|
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);
|
case "layout": value = GuiBuilder.buildFromAssets(a[1], null, links);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -172,17 +173,20 @@ class GuiF {
|
|||||||
var e:Enum<Dynamic> = Type.resolveEnum(a[0]);
|
var e:Enum<Dynamic> = Type.resolveEnum(a[0]);
|
||||||
value = Type.createEnum(e, a[1]);
|
value = Type.createEnum(e, a[1]);
|
||||||
} else if (c == "@") {
|
} else if (c == "@") {
|
||||||
if (s.charAt(1) == "~") {
|
var c1 = s.charAt(1);
|
||||||
|
if (c1 == "~") {
|
||||||
var a:Array<String> = s.substr(2).split(":");
|
var a:Array<String> = s.substr(2).split(":");
|
||||||
switch (a[0]) {
|
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 {
|
} else {
|
||||||
var a:Array<String> = s.substr(1).split(":");
|
var a:Array<String> = s.substr(1).split(":");
|
||||||
//value = Reflect.field(Provider.get(IResources), a[0]).get(a[1]);
|
//value = Reflect.field(Provider.get(IResources), a[0]).get(a[1]);
|
||||||
Reflect.field(Provider.get(IResources), a[0]).bind(a[1], object, field);
|
Reflect.field(Provider.get(IResources), a[0]).bind(a[1], object, field);
|
||||||
}
|
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
} else if (~/0x[A-Fa-f\d]{6}/.match(value)) {
|
} else if (~/0x[A-Fa-f\d]{6}/.match(value)) {
|
||||||
value = Std.parseInt(value);
|
value = Std.parseInt(value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package haxework.locale;
|
|||||||
interface ILocale {
|
interface ILocale {
|
||||||
public function getString(key:String):String;
|
public function getString(key:String):String;
|
||||||
public function formatString(key:String, args:Array<Dynamic>):String;
|
public function formatString(key:String, args:Array<Dynamic>):String;
|
||||||
|
public function formatPlurar(key:String, num:Int, args:Array<Dynamic>):String;
|
||||||
public function getArray(key:String):Array<String>;
|
public function getArray(key:String):Array<String>;
|
||||||
public function getPlural(key:String, num:Int):String;
|
public function getPlural(key:String, num:Int):String;
|
||||||
}
|
}
|
||||||
38
haxework/locale/LString.hx
Normal file
38
haxework/locale/LString.hx
Normal file
@@ -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<Dynamic>) {
|
||||||
|
this = Provider.get(ILocale).formatString(value, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract LPFString(String) from String to String {
|
||||||
|
|
||||||
|
inline public function new(value:String, num:Int, args:Array<Dynamic>) {
|
||||||
|
this = Provider.get(ILocale).formatPlurar(value, num, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,15 @@ class Locale implements ILocale {
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function formatPlurar(key:String, num:Int, args:Array<Dynamic>):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 {
|
public function getString(key:String):String {
|
||||||
return getObject(key);
|
return getObject(key);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user