@sumor/i18n
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "@sumor/i18n", | ||
"description": "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.", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "repository": "sumor-cloud/i18n", |
@@ -41,38 +41,38 @@ # i18n | ||
```javascript | ||
import getI18n from '@sumor/i18n'; | ||
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}' | ||
} | ||
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); | ||
const i18n = getI18n('zh-TW', i18nConfig) | ||
// match zh-TW | ||
console.log(i18n('demo.hello')); // 妳好 | ||
console.log(i18n('demo.greeting', { name: 'John' })); // 妳好, John | ||
console.log(i18n('demo.hello')) // 妳好 | ||
console.log(i18n('demo.greeting', { name: 'John' })) // 妳好, John | ||
// match zh | ||
console.log(i18n('demo.welcome')); // 欢迎 | ||
console.log(i18n('demo.welcome')) // 欢迎 | ||
// match en | ||
console.log(i18n('demo.test')); // Test | ||
``` | ||
console.log(i18n('demo.test')) // Test | ||
``` |
@@ -5,2 +5,5 @@ import pick from './pick.js' | ||
return (key, parameters = {}) => { | ||
if (!key) { | ||
throw new Error('i18n key is required') | ||
} | ||
const originConfig = | ||
@@ -17,4 +20,8 @@ config.origin || config.en || config['en-US'] || config.zh || config['zh-CN'] | ||
if (matched === undefined) { | ||
throw new Error(`i18n text is not found for ${key}`) | ||
} | ||
return stringify(matched, parameters) | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5838
37
78