jsoni18n

A flexible internationalization library working with JSON files in Haxe.
https://github.com/Nekith/jsoni18n

To install, run:

haxelib install jsoni18n 1.1.0 

See using Haxelib in Haxelib documentation for more information.

README.md

jsoni18n

A flexible internationalization library working with JSON files in Haxe.

  • Does not assume the task of handling the origin/source of the translations (e.g. opening files).
  • Has nothing about date localization.
  • Has been made with coded strings (welcome/hello) as translation key in mind, not sentences in a source language (Hello!).
  • No additional dependency.

Installation

Install with Haxelib:

haxelib install jsoni18n

Then, Haxe command line arguments or in a HXML file:

-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." }
    },
    "person": {
        "f": "Female", "m": "Male", "$": "Person"
    },
    "secret": {
        "intro": "It's a secret page! Do you have authorization?"
    }
}

Basics

Import:

import jsoni18n.I18n;

Initialization:

var i18n : I18n = new I18n();

Load data:

var jsonFileContent : String = myLangFileLoader();
i18n.loadFromString(jsonFileContent);

Now, to translate something:

var hello : String = i18n.tr("welcome/hello"); // Hoy!

Prefix

You can add a prefix 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.

Concord

It also handles concordance for your convenience.

i18n.tr("person", [ "$" => "f" ]); // Female
i18n.tr("person", [ "$" => "o" ]); // Person

Configuration

i18n.depthDelimiter    =  ".";  // default: "/"
i18n.varPrefix         =  "@";  // default: ":"
i18n.pluralizationVar  =  "*";  // default: "_"
i18n.concordVar        =  "^";  // default: "$"

License

3-clause BSD. See LICENSE file.

Development

Github issues are open if you have suggestions or bugs to report.

Tests

haxelib install utest
haxelib dev jsoni18n .
cd tests
haxe -main Main -lib jsoni18n -lib utest -hl build.hl
hl build.hl

Tested against:

  • Haxe 4.2.3 - HashLink 1.11.0 and JS
  • Haxe 4.1.5 - hxcpp
  • Haxe 4.2.3 - hxcpp
Contributors
Nekith
Version
1.1.0
Published
3 years ago
License
BSD

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub