1.4.1 • Published 3 years ago

i18nice v1.4.1

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

Overview

"i18nice" is an internationalization library written in JavaScript. It provides a simple and easy way to handle i18n (interpolation,plural)."i18nice" has zero dependencies.

It is licensed under MIT license.

Quick start

view demo on the web

https://riversun.github.io/i18nice/index.html

run demo on node.js

clone this project and type as follows

npm start

download standalone js file

You can import standalone js into your front-end app on the browser with <script> tag.

https://raw.githubusercontent.com/riversun/i18nice/master/dist/i18nice.js

install via npm

npm install i18nice

Usage

Instantiation

First, create an instance of the i18nice class, which you will use for translation.

var i18n = new i18nice();
 

Translation

i18nice.t() provides translation.

i18n.init({
    en: {
        hello: "Hello.",
    },
    ja: {
        hello: "こんにちは。",
    }
});

i18n.setLocale("en");
i18n.t("hello");
 => "Hello."

i18n.setLocale("ja");
i18n.t("hello");
 => "こんにちは。"
 

Interpolation

i18nice.t() also provides interpolation.

i18n.init({
    en: {
        hello: "Hi,#{user1}.",
    },
    ja: {
        hello: "#{user1}さん、こんにちは。",
    }
});

i18n.setLocale("en");

i18n.t("hello", {user1:"Tom"});
 =>"Hi,Tom!"

Ordinal Number

  • Ordinal Number in template
i18n.init({
    en: {
        hello: "Hi,#{user1}.Come into the #{ordinal} room.",
        ordinal:["first","second","third"],
    },
    ja: {
        hello: "#{user1}さん、こんにちは。#{ordinal}のお部屋にお入りください",
        ordinal:["1つめ","2つめ","3つめ"],
    }
});

i18n.setLocale("en");

i18n.t("hello", {user1:"Tom",ordinal_index:1});
 =>"Hi,Tom.Come into the second room."
  • Ordial Number as itself
i18n.init({
    en: {
        hello: "Hi,#{user1}.Come into the #{ordinal} room.",
        ordinal:["first","second","third"],
    },
    ja: {
        hello: "#{user1}さん、こんにちは。#{ordinal}のお部屋にお入りください",
        ordinal:["1つめ","2つめ","3つめ"],
    }
});

i18n.setLocale("en");

i18n.t("ordinal",{ordinal_index:1});
 =>"first"
  • Ordial Number as itself with no option
i18n.init({
    en: {
        hello: "Hi,#{user1}.Come into the #{ordinal} room.",
        ordinal:["first","second","third"],
    },
    ja: {
        hello: "#{user1}さん、こんにちは。#{ordinal}のお部屋にお入りください",
        ordinal:["1つめ","2つめ","3つめ"],
    }
});

i18n.setLocale("en");

i18n.t("ordinal");
 =>["first","second","third"] // returns array itself

Pluralization

You can use ((case of single | case of multiple)) for pluralization.

i18n.init({
    en: {
        cars: "You have ((#{numCar} car|#{numCar} cars)).",
    }
});

i18n.t("cars", {numCar:0});
 =>"You have 0 cars."

i18n.t("cars", {numCar:1});
 =>"You have 1 car."
 
i18n.t("cars", {numCar:2});
 =>"You have 2 cars."
 
 

Also you can use ((case of zero | case of single | case of multiple)) for pluralization.

i18n.init({
    en: {
        cars: "You have ((no car|a car|#{numCar} cars)).",
    }
});

i18n.t("cars", {numCar:0});
 =>"You have no car."

i18n.t("cars", {numCar:1});
 =>"You have a car."
 
i18n.t("cars", {numCar:2});
 =>"You have 2 cars."
 
 

Fallback locale

i18n.setFallback("en");
1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

6 years ago