jsoni18n 0.3.1

A flexible internationalization library working with JSON files in Haxe.

Released 2017-02-15.

To install, run:

haxelib install jsoni18n 0.3.1

See using Haxelib in Haxelib documentation for more information.

This is not the latest stable version of jsoni18n. See version 1.0.0 for the latest version.

MaintainerNekith
Websitehttps://github.com/Nekith/jsoni18n
Current version1.0.0
StatisticsInstalled 23 times
LicenseBSD
Tags cross, i18n, internationalization, localization, translation

README.md

jsoni18n

A flexible internationalization library working with JSON files in Haxe.

Build Status

Installation

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 ...

Usage

JSON files

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?"
    }
}

Basics

There's only one import:

import jsoni18n.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");

Prefix

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

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

Variables

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

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

Pluralization

It also handles pluralization for your convenience.

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

Configuration

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

License

3-clause BSD. See LICENSE file.

Development

Tests

haxelib dev jsoni18n .
cd tests
haxe -main Main -lib jsoni18n -cpp build
cd build
./Main