1.0.2 • Published 7 years ago

ilocale v1.0.2

Weekly downloads
2
License
-
Repository
github
Last release
7 years ago

ilocale (I.js)

Tiny utility to implement localization in JavaScript (L10N), supporting only scoped translations; and simple templates.

Features

  • Supports scoped translations
  • Supports simple templates with mustaches {{ }}
  • Enables the implementation of text localization
  • Supports NodeJs and amd
  • Minified it weighs only 1.8 KB

More complex solutions

I.js is meant to be as simple and short as possible, and to support only scoped translations. Users who need more complex solutions (e.g. with support for pluralization; currencies; dates management), should look instead at other libraries (internationalization frameworks):

For a comparison with i18n-js library, please refer to the dedicated wiki page;

How to use

Either install using npm:

npm install ilocale

Or download one of these two files:

ES6 Example

If I.js is installed using npm package:

import I from "ilocale"

I.regional = {
  "en": {
    "voc": {
      "Hi": "Hi"
    }
  }
}

I.setLocale("en");
I.t("voc.Hi"); // --> "Hi"

Examples

The following example shows how to work with scoped translations, using I.js:

(function () {

  //set the locale information:
  I.regional = {
    "en": {
      "voc": {
        "Hi": "Hi"
      }
    },
    "it": {
      "voc": {
        "Hi": "Ciao"
      }
    },
    "pl": {
      "voc": {
        "Hi": "Cześć"
      }
    }
  };

  I.setLocale("en");
  I.t("voc.Hi"); // --> "Hi"

  I.setLocale("it");
  I.t("voc.Hi"); // --> "Ciao"

  I.setLocale("pl");
  I.t("voc.Hi"); // --> "Cześć"

})();

The following example shows how to use simple templates:

(function () {

  //set the locale information:
  I.regional = {
    "en": {
      "voc": {
        "HelloName": "Hello, {{name}}!"
      }
    },
    "it": {
       "voc": {
         "HelloName": "Ciao, {{name}}!"
       }
    }
  };

  I.setLocale("en");
  I.t("voc.HelloName"); // --> "Hello, {{name}}!"
  I.t("voc.HelloName", {
    name: "Edyta" // e.g. user.name
  }); // --> "Hello, Edyta!"

  I.setLocale("it");
  I.t("voc.HelloName"); // --> "Ciao, {{name}}!"
  I.t("voc.HelloName", {
    name: "Edyta" // e.g. user.name
  }); // --> "Ciao, Edyta!"

})();

How to organize the locale files

Please refer to the dedicated wiki page.

Examples in other repositories

The following repositories contain examples of internationalization implemented using I.js:

Live demos:

Notes

When setting a locale using the function setLocale; a class to the document.body is also added accordingly.