From 680bf33dceb69beeac15d5d0cbbe4ffd2c1e9409 Mon Sep 17 00:00:00 2001 From: Valerian Cubero Date: Wed, 15 Feb 2017 19:24:50 +0100 Subject: [PATCH] move from static to instantiation --- README.md | 26 ++++++++++++++++---------- haxelib.json | 4 ++-- jsoni18n/I18n.hx | 29 ++++++++++++++--------------- tests/ConfigTestCase.hx | 19 ++++++++++--------- tests/TrTestCase.hx | 32 +++++++++++++++----------------- 5 files changed, 57 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 0d55934..f5d91a0 100644 --- a/README.md +++ b/README.md @@ -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 @@ -76,13 +82,13 @@ 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 @@ -90,8 +96,8 @@ var hello : String = I18n.tr("welcome/hello"); 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 @@ -99,7 +105,7 @@ I18n.tr("ui/welcome/hello"); // Hoy! 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 @@ -107,16 +113,16 @@ I18n.tr("welcome/subtitle", [ "name" => "Nekith" ]); // Welcome, Nekith! 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 diff --git a/haxelib.json b/haxelib.json index a2c74ec..5b56453 100644 --- a/haxelib.json +++ b/haxelib.json @@ -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": {} } diff --git a/jsoni18n/I18n.hx b/jsoni18n/I18n.hx index 5d6181f..5c1c045 100644 --- a/jsoni18n/I18n.hx +++ b/jsoni18n/I18n.hx @@ -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; + private var trads : DynamicObject; - public static function loadFromString(content : String, ?prefix : String) : Void + public function new() + { + trads = new DynamicObject(); + } + + public function loadFromString(content : String, ?prefix : String) : Void { - if (trads == null) { - trads = new DynamicObject(); - } var data : DynamicObject = Json.parse(content); for (key in data.keys()) { var name : String = key; @@ -25,11 +27,8 @@ class I18n } } - public static function tr(id : String, ?vars : Map) : String + public function tr(id : String, ?vars : Map) : String { - if (trads == null) { - return id; - } var str : String = id; if (id.indexOf(depthDelimiter) != -1) { var o : DynamicObject = fetch(trads, new String(id)); @@ -60,12 +59,12 @@ class I18n return str; } - public static function clear() : Void + public function clear() : Void { trads = new DynamicObject(); } - private static function update(el : DynamicObject, rest : String, data : DynamicObject) : Void + private function update(el : DynamicObject, rest : String, data : DynamicObject) : Void { var pos : Int = rest.indexOf(depthDelimiter); if (pos == -1) { @@ -91,7 +90,7 @@ class I18n } } - private static function fetch(el : DynamicObject, rest : String) : DynamicObject + private function fetch(el : DynamicObject, rest : String) : DynamicObject { var pos : Int = rest.indexOf(depthDelimiter); if (pos == -1) { diff --git a/tests/ConfigTestCase.hx b/tests/ConfigTestCase.hx index 6eba082..167c831 100644 --- a/tests/ConfigTestCase.hx +++ b/tests/ConfigTestCase.hx @@ -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!", @@ -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 ])); } } diff --git a/tests/TrTestCase.hx b/tests/TrTestCase.hx index da112c6..970f904 100644 --- a/tests/TrTestCase.hx +++ b/tests/TrTestCase.hx @@ -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!", @@ -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")); } }