Git Product home page Git Product logo

jsoni18n's Introduction

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": "Woman", "m": "Man", "$": "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" ]); // Woman
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.5 - HashLink 1.11.0 and JS
  • Haxe 4.2.5 - hxcpp
  • Haxe 4.3.3 - hxcpp

jsoni18n's People

Contributors

nekith avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jsoni18n's Issues

Pluralization

{
  "items": { "0": "No item", "1": "One item", "_": ":_ items" }
}
I18n.pluralizationWildcard = "n"; // default: "_"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.