@sumor/i18n

i18n

一个 Sumor Cloud 工具。
更多文档

这是一个适用于 Node.js 和浏览器的轻量级 i18n 库。
您可以轻松地使用它来管理您的 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