A flexible internationalization library working with JSON files in Haxe.
Haxelib:
haxelib install jsoni18n
OpenFL project XML file:
<haxelib name="jsoni18n" />
OpenFL project HXP file:
haxelibs.push(new Haxelib("jsoni18n"));
Haxe command line arguments:
haxe -lib jsoni18n ...
It's JSON, objects and strings:
{
"welcome": {
"hello": "Hoy!",
"subtitle": "Welcome, :name!",
"content": {
"main": "Main content should be longer but you get the idea.",
"side": "Some useful side notes to shine in society."
}
},
"news": {
"list": { "0": "Nothing to display.", "1": "Only one new item.", "_": ":_ new items." }
},
"secret": {
"intro": "It's a secret page! Do you have authorization?"
}
}
There's only one import:
import jsoni18n.I18n;
Initialization:
var i18n : I18n = new I18n();
For the following examples, we assume you do something like this:
// it could be Reg.lang, context.userLang, App.instance.settings["currentLanguage"] or ...
var lang : String = myGetCurrentLanguage();
Then you load data:
var jsonFileContent : String = myLangFileLoader();
// or if you use OpenFL:
// var jsonFileContent : String = Assets.getText(filename);
i18n.loadFromString(jsonFileContent);
Now, to translate something:
var hello : String = i18n.tr("welcome/hello");
You can add prefixes to keys from all data fetched by loadFromString() like this:
i18n.loadFromString(data, "ui/");
i18n.tr("ui/welcome/hello"); // Hoy!
You can pass variables to strings returned by tr() like this:
i18n.tr("welcome/subtitle", [ "name" => "Nekith" ]); // Welcome, Nekith!
It also handles pluralization for your convenience.
i18n.tr("news/list", [ "_" => 0 ]); // Nothing to display.
i18n.tr("news/list", [ "_" => 12 ]); // 12 new items.
i18n.depthDelimiter = "."; // default: "/"
i18n.varPrefix = "@"; // default: ":"
i18n.pluralizationVar = "*"; // default: "_"
3-clause BSD. See LICENSE file.
Github issues are open if you have suggestions or bugs to report.
haxelib dev jsoni18n .
cd tests
haxe -main Main -lib jsoni18n -cpp build
cd build
./Main
Tested against:
- 3.4.7
- 3.4.6