Skip to content

Commit

Permalink
move from static to instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekith committed Feb 15, 2017
1 parent 484d341 commit 680bf33
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 53 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ There's only one import:
import jsoni18n.I18n;
```

Initialization:

```haxe
var i18n : I18n = new I18n();
```

For the following examples, we assume you do something like this:

```haxe
Expand All @@ -76,47 +82,47 @@ Then you load data:
var jsonFileContent : String = myLangFileLoader();
// or if you use OpenFL:
// var jsonFileContent : String = Assets.getText(filename);
I18n.loadFromString(jsonFileContent);
i18n.loadFromString(jsonFileContent);
```

Now, to translate something:

```haxe
var hello : String = I18n.tr("welcome/hello");
var hello : String = i18n.tr("welcome/hello");
```

### Prefix

You can add prefixes to keys from all data fetched by loadFromString() like this:

```haxe
I18n.loadFromString(data, "ui/");
I18n.tr("ui/welcome/hello"); // Hoy!
i18n.loadFromString(data, "ui/");
i18n.tr("ui/welcome/hello"); // Hoy!
```

### Variables

You can pass variables to strings returned by tr() like this:

```haxe
I18n.tr("welcome/subtitle", [ "name" => "Nekith" ]); // Welcome, Nekith!
i18n.tr("welcome/subtitle", [ "name" => "Nekith" ]); // Welcome, Nekith!
```

### Pluralization

It also handles pluralization for your convenience.

```haxe
I18n.tr("news/list", [ "_" => 0 ]); // Nothing to display.
I18n.tr("news/list", [ "_" => 12 ]); // 12 new items.
i18n.tr("news/list", [ "_" => 0 ]); // Nothing to display.
i18n.tr("news/list", [ "_" => 12 ]); // 12 new items.
```

### Configuration

```haxe
I18n.depthDelimiter = "."; // default: "/"
I18n.varPrefix = "@"; // default: ":"
I18n.pluralizationVar = "n"; // default: "_"
i18n.depthDelimiter = "."; // default: "/"
i18n.varPrefix = "@"; // default: ":"
i18n.pluralizationVar = "n"; // default: "_"
```

## License
Expand Down
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"license": "BSD",
"tags": ["cross", "i18n", "translation", "internationalization", "localization"],
"description": "A flexible internationalization library working with JSON files in Haxe.",
"version": "0.3.1",
"releasenote": "Add pluralization and tests.",
"version": "1.0.0",
"releasenote": "Move from static to instantiation.",
"contributors": ["Nekith"],
"dependencies": {}
}
29 changes: 14 additions & 15 deletions jsoni18n/I18n.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import haxe.Json;

class I18n
{
public static var depthDelimiter : String = "/";
public static var varPrefix : String = ":";
public static var pluralizationVar : String = "_";
public var depthDelimiter : String = "/";
public var varPrefix : String = ":";
public var pluralizationVar : String = "_";

private static var trads : DynamicObject<Dynamic>;
private var trads : DynamicObject<Dynamic>;

public static function loadFromString(content : String, ?prefix : String) : Void
public function new()
{
trads = new DynamicObject<Dynamic>();
}

public function loadFromString(content : String, ?prefix : String) : Void
{
if (trads == null) {
trads = new DynamicObject<Dynamic>();
}
var data : DynamicObject<Dynamic> = Json.parse(content);
for (key in data.keys()) {
var name : String = key;
Expand All @@ -25,11 +27,8 @@ class I18n
}
}

public static function tr(id : String, ?vars : Map<String, Dynamic>) : String
public function tr(id : String, ?vars : Map<String, Dynamic>) : String
{
if (trads == null) {
return id;
}
var str : String = id;
if (id.indexOf(depthDelimiter) != -1) {
var o : DynamicObject<Dynamic> = fetch(trads, new String(id));
Expand Down Expand Up @@ -60,12 +59,12 @@ class I18n
return str;
}

public static function clear() : Void
public function clear() : Void
{
trads = new DynamicObject<Dynamic>();
}

private static function update(el : DynamicObject<Dynamic>, rest : String, data : DynamicObject<Dynamic>) : Void
private function update(el : DynamicObject<Dynamic>, rest : String, data : DynamicObject<Dynamic>) : Void
{
var pos : Int = rest.indexOf(depthDelimiter);
if (pos == -1) {
Expand All @@ -91,7 +90,7 @@ class I18n
}
}

private static function fetch(el : DynamicObject<Dynamic>, rest : String) : DynamicObject<Dynamic>
private function fetch(el : DynamicObject<Dynamic>, rest : String) : DynamicObject<Dynamic>
{
var pos : Int = rest.indexOf(depthDelimiter);
if (pos == -1) {
Expand Down
19 changes: 10 additions & 9 deletions tests/ConfigTestCase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import jsoni18n.I18n;

class ConfigTestCase extends TestCase
{
var json : String;
var i18n : I18n;

override public function setup() : Void
{
json = '{
var json : String = '{
"welcome": {
"hello": "Hoy!",
"subtitle": "Welcome, @name!",
Expand All @@ -26,24 +26,25 @@ class ConfigTestCase extends TestCase
},
"title": "jsoni18n tests"
}';
I18n.depthDelimiter = "|";
I18n.varPrefix = "@";
I18n.pluralizationVar = "n";
I18n.loadFromString(json);
i18n = new I18n();
i18n.depthDelimiter = "|";
i18n.varPrefix = "@";
i18n.pluralizationVar = "n";
i18n.loadFromString(json);
}

public function testDepthDelimiter() : Void
{
assertEquals("Hoy!", I18n.tr("welcome|hello"));
assertEquals("Hoy!", i18n.tr("welcome|hello"));
}

public function testVarPrefix() : Void
{
assertEquals("Welcome, Nekith!", I18n.tr("welcome|subtitle", [ "name" => "Nekith" ]));
assertEquals("Welcome, Nekith!", i18n.tr("welcome|subtitle", [ "name" => "Nekith" ]));
}

public function testPluralizationVar() : Void
{
assertEquals("7 new items.", I18n.tr("tweets|list", [ "n" => 7 ]));
assertEquals("7 new items.", i18n.tr("tweets|list", [ "n" => 7 ]));
}
}
32 changes: 15 additions & 17 deletions tests/TrTestCase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import jsoni18n.I18n;

class TrTestCase extends TestCase
{
var json : String;
var i18n : I18n;

override public function setup() : Void
{
json = '{
var json : String = '{
"welcome": {
"hello": "Hoy!",
"subtitle": "Welcome, :name!",
Expand All @@ -23,56 +23,54 @@ class TrTestCase extends TestCase
},
"title": "jsoni18n tests"
}';
I18n.depthDelimiter = "/";
I18n.varPrefix = ":";
I18n.pluralizationVar = "_";
I18n.loadFromString(json);
i18n = new I18n();
i18n.loadFromString(json);
}

public function testBasic() : Void
{
assertEquals("jsoni18n tests", I18n.tr("title"));
assertEquals("jsoni18n tests", i18n.tr("title"));
}

public function testUnknown() : Void
{
assertEquals("brouzoufs", I18n.tr("brouzoufs"));
assertEquals("brouzoufs", i18n.tr("brouzoufs"));
}

public function testDepth() : Void
{
assertEquals("Hoy!", I18n.tr("welcome/hello"));
assertEquals("Hoy!", i18n.tr("welcome/hello"));
}

public function testWrongDepth() : Void
{
assertEquals("title/lol", I18n.tr("title/lol"));
assertEquals("title/lol", i18n.tr("title/lol"));
}

public function testWrongType() : Void
{
assertEquals("welcome/content", I18n.tr("welcome/content"));
assertEquals("welcome/content", i18n.tr("welcome/content"));
}

public function testVar() : Void
{
assertEquals("Welcome, Nekith!", I18n.tr("welcome/subtitle", [ "name" => "Nekith" ]));
assertEquals("Welcome, Nekith!", i18n.tr("welcome/subtitle", [ "name" => "Nekith" ]));
}

public function testPlur() : Void
{
assertEquals("27 new items.", I18n.tr("news/list", [ "_" => 27 ]));
assertEquals("Nothing to display.", I18n.tr("news/list", [ "_" => 0 ]));
assertEquals("Only one new item.", I18n.tr("news/list", [ "_" => 1 ]));
assertEquals("27 new items.", i18n.tr("news/list", [ "_" => 27 ]));
assertEquals("Nothing to display.", i18n.tr("news/list", [ "_" => 0 ]));
assertEquals("Only one new item.", i18n.tr("news/list", [ "_" => 1 ]));
}

public function testWrongTypePlur() : Void
{
assertEquals("news/list", I18n.tr("news/list"));
assertEquals("news/list", i18n.tr("news/list"));
}

public function testUtf() : Void
{
assertEquals("Le contenu principal devrait être plus long, mais vous saisissez l\'idée.", I18n.tr("welcome/content/main"));
assertEquals("Le contenu principal devrait être plus long, mais vous saisissez l\'idée.", i18n.tr("welcome/content/main"));
}
}

0 comments on commit 680bf33

Please sign in to comment.