diff --git a/jsoni18n/I18n.hx b/jsoni18n/I18n.hx index b859ccd..0a966ed 100644 --- a/jsoni18n/I18n.hx +++ b/jsoni18n/I18n.hx @@ -25,15 +25,16 @@ class I18n public static function tr(id : String, ?vars : Map) : String { if (trads == null) { - return ""; + return id; } - var str : String; + var str : String = id; if (id.indexOf(depthDelimiter) != -1) { - str = fetch(trads, new String(id)); + var o = fetch(trads, new String(id)); + if (o != null) { + str = o; + } } else if (trads.exists(id) == true) { str = trads.get(id); - } else { - str = id; } if (vars != null) { for (key in vars.keys()) { @@ -83,7 +84,7 @@ class I18n var part : String = rest.substr(0, pos); rest = rest.substr(pos + 1); if (el.exists(part) == false) { - return ""; + return null; } return fetch(el.get(part), rest); } diff --git a/tests/TrTestCase.hx b/tests/TrTestCase.hx index d181704..2ff8c1e 100644 --- a/tests/TrTestCase.hx +++ b/tests/TrTestCase.hx @@ -27,21 +27,31 @@ class TrTestCase extends TestCase public function testBasic() : Void { - assertEquals(I18n.tr("title"), "jsoni18n tests"); + assertEquals("jsoni18n tests", I18n.tr("title")); + } + + public function testUnknown() : Void + { + assertEquals("brouzoufs", I18n.tr("brouzoufs")); + } + + public function testWrongDepth() : Void + { + assertEquals("title/lol", I18n.tr("title/lol")); } public function testDepth() : Void { - assertEquals(I18n.tr("welcome/hello"), "Hoy!"); + assertEquals("Hoy!", I18n.tr("welcome/hello")); } public function testVar() : Void { - assertEquals(I18n.tr("welcome/subtitle", [ "name" => "Nekith" ]), "Welcome, Nekith!"); + assertEquals("Welcome, Nekith!", I18n.tr("welcome/subtitle", [ "name" => "Nekith" ])); } public function testUtf() : Void { - assertEquals(I18n.tr("welcome/content/main"), "Le contenu principal devrait être plus long, mais vous saisissez l\'idée."); + assertEquals("Le contenu principal devrait être plus long, mais vous saisissez l\'idée.", I18n.tr("welcome/content/main")); } }