@sumor/i18n

i18n

一个 Sumor Cloud 工具。
更多文档

这是一个轻量级的用于 Node.js 和浏览器的国际化(i18n)库。 您可以轻松使用它来管理您的国际化资源。 并将其应用到您的项目中。

CI Test Coverage Audit

安装

npm i @sumor/i18n --save

先决条件

Node.JS 版本

需要 Node.JS 版本 16.x 或以上。

需要 Node.JS ES 模块

由于此包以 ES 模块编写, 请在您的 package.json 文件中更改以下代码:

{
  "type": "module"
}

用法

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)

// 匹配 zh-TW
console.log(i18n('demo.hello')) // 妳好
console.log(i18n('demo.greeting', { name: 'John' })) // 妳好, John

// 匹配 zh
console.log(i18n('demo.welcome')) // 欢迎

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