New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

i18nice

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18nice

Simple and easy i18n library

  • 1.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by120%
Maintainers
1
Weekly downloads
 
Created
Source

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");

FAQs

Package last updated on 27 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc