@sumor/i18n

i18n

A Sumor Cloud Tool.
More Documentation

This is a lightweight i18n library for Node.js and the browser. You can easily use it to manage your i18n resources. And apply it to your project.

CI Test Coverage Audit

Installation

npm i @sumor/i18n --save

Prerequisites

Node.JS version

Require Node.JS version 16.x or above

require Node.JS ES module

As this package is written in ES module, please change the following code in your package.json file:

{
  "type": "module"
}

Usage

import getI18n from '@sumor/i18n'

const i18nConfig = {
  en: {
    demo: {
      hello: 'Hello',
      welcome: 'Welcome',
      greeting: 'Hello, {name}',
      test: 'Test'
    }
  },
  zh: {
    demo: {
      hello: 'Привет',
      welcome: 'Добро пожаловать'
    }
  },
  'zh-TW': {
    demo: {
      hello: 'Привет',
      greeting: 'Привет, {name}'
    }
  }
}

const i18n = getI18n('zh-TW', i18nConfig)

// match zh-TW
console.log(i18n('demo.hello')) // Привет
console.log(i18n('demo.greeting', { name: 'Джон' })) // Привет, Джон

// match zh
console.log(i18n('demo.welcome')) // Добро пожаловать

// match en
console.log(i18n('demo.test')) // Test