Skip to content

Commit

Permalink
add some additional error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekith committed Feb 8, 2017
1 parent 04a09f1 commit c1f8a74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
13 changes: 7 additions & 6 deletions jsoni18n/I18n.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ class I18n
public static function tr(id : String, ?vars : Map<String, String>) : 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()) {
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 14 additions & 4 deletions tests/TrTestCase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit c1f8a74

Please sign in to comment.