diff --git a/package.json b/package.json index d56391747ad779fdda833d4debcbd4a69e8aaad9..5cf1c1f0a8a1e3bf78d1140b7c969d7398f1e7fa 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "eslint-plugin-promise": "^4.2.1", "eslint-plugin-standard": "^4.0.1", "eslint-plugin-vue": "^6.2.2", + "i18n": "^0.13.3", "jest": "^25.1.0", "jsdom": "^16.2.1", "jsdom-global": "^3.0.2", diff --git a/packages/uni-cli-i18n/__tests__/env.spec.js b/packages/uni-cli-i18n/__tests__/env.spec.js new file mode 100644 index 0000000000000000000000000000000000000000..573e4eda8eeea40da21484f51c95446e50659649 --- /dev/null +++ b/packages/uni-cli-i18n/__tests__/env.spec.js @@ -0,0 +1,7 @@ +describe('env', () => { + it('HBuilderX', () => { + process.env.UNI_HBUILDERX_LANGID = 'fr' + const i18n = require('../lib/index') + expect(i18n.getLocale()).toBe('fr') + }) +}) diff --git a/packages/uni-cli-i18n/__tests__/lang.spec.js b/packages/uni-cli-i18n/__tests__/lang.spec.js new file mode 100644 index 0000000000000000000000000000000000000000..769c94dad1632d7bbe5756c7d876af83430e7627 --- /dev/null +++ b/packages/uni-cli-i18n/__tests__/lang.spec.js @@ -0,0 +1,7 @@ +const { format } = require('../lib/lang') + +describe('format', () => { + it('env.lang', () => { + expect(format('zh_CN.UTF_8')).toBe('zh_CN') + }) +}) diff --git a/packages/uni-cli-i18n/lib/index.js b/packages/uni-cli-i18n/lib/index.js new file mode 100644 index 0000000000000000000000000000000000000000..496e99a0d39289a31ff4a54cfa60825f33c521cd --- /dev/null +++ b/packages/uni-cli-i18n/lib/index.js @@ -0,0 +1,29 @@ +const { I18n } = require('i18n') +const { format } = require('./lang') + +const defaultLocale = format(process.env.UNI_HBUILDERX_LANGID || process.env.LANG) + +const i18n = new I18n() + +const staticCatalog = {} + +// 暂时仅支持 en、zh_CN +const locales = [ + 'en', + // 'es', + // 'fr', + // 'zh_HK', + 'zh_CN' +] + +locales.forEach(item => { + // TODO 合并 HBuilderX 内置内容 + staticCatalog[item] = require(`../locales/${item}.json`) +}) + +i18n.configure({ + staticCatalog, + defaultLocale +}) + +module.exports = i18n diff --git a/packages/uni-cli-i18n/lib/lang.js b/packages/uni-cli-i18n/lib/lang.js new file mode 100644 index 0000000000000000000000000000000000000000..5ad340fb5654c536314c92c5c655a0d0027388ca --- /dev/null +++ b/packages/uni-cli-i18n/lib/lang.js @@ -0,0 +1,13 @@ +function format (lang) { + const array = lang.split(/[\.,]/)[0].split(/[_-]/) + array[0] = array[0].toLowerCase() + if (array[0] === 'zh') { + array[1] = (array[1] || 'CN').toUpperCase() + } + array.length = Math.min(array.length, 2) + return array.join('_') +} + +module.exports = { + format +} \ No newline at end of file diff --git a/packages/uni-cli-i18n/locales/en.json b/packages/uni-cli-i18n/locales/en.json new file mode 100644 index 0000000000000000000000000000000000000000..9e26dfeeb6e641a33dae4961196235bdb965b21b --- /dev/null +++ b/packages/uni-cli-i18n/locales/en.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/packages/uni-cli-i18n/locales/es.json b/packages/uni-cli-i18n/locales/es.json new file mode 100644 index 0000000000000000000000000000000000000000..9e26dfeeb6e641a33dae4961196235bdb965b21b --- /dev/null +++ b/packages/uni-cli-i18n/locales/es.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/packages/uni-cli-i18n/locales/fr.json b/packages/uni-cli-i18n/locales/fr.json new file mode 100644 index 0000000000000000000000000000000000000000..9e26dfeeb6e641a33dae4961196235bdb965b21b --- /dev/null +++ b/packages/uni-cli-i18n/locales/fr.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/packages/uni-cli-i18n/locales/zh_CN.json b/packages/uni-cli-i18n/locales/zh_CN.json new file mode 100644 index 0000000000000000000000000000000000000000..9e26dfeeb6e641a33dae4961196235bdb965b21b --- /dev/null +++ b/packages/uni-cli-i18n/locales/zh_CN.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/packages/uni-cli-i18n/locales/zh_HK.json b/packages/uni-cli-i18n/locales/zh_HK.json new file mode 100644 index 0000000000000000000000000000000000000000..9e26dfeeb6e641a33dae4961196235bdb965b21b --- /dev/null +++ b/packages/uni-cli-i18n/locales/zh_HK.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/packages/uni-cli-i18n/package.json b/packages/uni-cli-i18n/package.json new file mode 100644 index 0000000000000000000000000000000000000000..3864bf5fdf86aa8653b5e08edf367f234ad17d5b --- /dev/null +++ b/packages/uni-cli-i18n/package.json @@ -0,0 +1,19 @@ +{ + "name": "@dcloudio/uni-cli-i18n", + "version": "0.0.1", + "description": "uni-cli-i18n", + "main": "lib/index.js", + "repository": { + "type": "git", + "url": "git+https://github.com/dcloudio/uni-app.git", + "directory": "packages/uni-cli-i18n" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "fxy060608", + "license": "Apache-2.0", + "dependencies": { + "i18n": "^0.13.3" + } +} diff --git a/yarn.lock b/yarn.lock index ec071060830d505252ccbb65aa3816930280a5db..765c484f8b960d43076d9c407456605f4dd16f19 100644 --- a/yarn.lock +++ b/yarn.lock @@ -746,7 +746,6 @@ "@dcloudio/uni-i18n@^2.0.0-31920210514002": version "2.0.0-31920210514002" resolved "https://registry.yarnpkg.com/@dcloudio/uni-i18n/-/uni-i18n-2.0.0-31920210514002.tgz#f63d14c234ea0353fabe2d8c681601c7ba07e488" - integrity sha512-shHf27U0KEs//ieCBG1R1aRKNDn6KwcVUIb5GsPuDOqoN7WQY+nEz9QFdOSQY8j9tX4ABOAc1ra0Opz8xFVngA== "@hapi/address@2.x.x": version "2.1.4" @@ -993,7 +992,6 @@ "@rollup/plugin-json@^4.1.0": version "4.1.0" resolved "https://registry.yarnpkg.com/@rollup/plugin-json/-/plugin-json-4.1.0.tgz#54e09867ae6963c593844d8bd7a9c718294496f3" - integrity sha512-yfLbTdNS6amI/2OpmbiBoW12vngr5NW2jCJVZSBEz+H5KfUJZ2M7sDjk0U6GOOdCWFVScShte29o9NezJ53TPw== dependencies: "@rollup/pluginutils" "^3.0.8" @@ -4642,6 +4640,17 @@ human-signals@^1.1.1: version "1.1.1" resolved "https://registry.npmjs.org/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3" +i18n@^0.13.3: + version "0.13.3" + resolved "https://registry.yarnpkg.com/i18n/-/i18n-0.13.3.tgz#5820f48d87a77cf14e064719bee9bc682ed550eb" + dependencies: + debug "^4.1.1" + make-plural "^6.2.2" + math-interval-parser "^2.0.1" + messageformat "^2.3.0" + mustache "^4.0.1" + sprintf-js "^1.1.2" + iconv-lite@0.4.24, iconv-lite@^0.4.24: version "0.4.24" resolved "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b" @@ -5920,6 +5929,16 @@ make-dir@^3.0.0, make-dir@^3.0.2: dependencies: semver "^6.0.0" +make-plural@^4.3.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/make-plural/-/make-plural-4.3.0.tgz#f23de08efdb0cac2e0c9ba9f315b0dff6b4c2735" + optionalDependencies: + minimist "^1.2.0" + +make-plural@^6.2.2: + version "6.2.2" + resolved "https://registry.yarnpkg.com/make-plural/-/make-plural-6.2.2.tgz#beb5fd751355e72660eeb2218bb98eec92853c6c" + makeerror@1.0.x: version "1.0.11" resolved "https://registry.npmjs.org/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c" @@ -5956,6 +5975,10 @@ matched@^0.4.1: lazy-cache "^2.0.1" resolve-dir "^0.1.0" +math-interval-parser@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/math-interval-parser/-/math-interval-parser-2.0.1.tgz#e22cd6d15a0a7f4c03aec560db76513da615bed4" + md5.js@^1.3.4: version "1.3.5" resolved "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz#b5d07b8e3216e3e27cd728d72f70d1e6a342005f" @@ -6016,6 +6039,22 @@ merge2@^1.2.3, merge2@^1.3.0: version "1.3.0" resolved "https://registry.npmjs.org/merge2/-/merge2-1.3.0.tgz#5b366ee83b2f1582c48f87e47cf1a9352103ca81" +messageformat-formatters@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/messageformat-formatters/-/messageformat-formatters-2.0.1.tgz#0492c1402a48775f751c9b17c0354e92be012b08" + +messageformat-parser@^4.1.2: + version "4.1.3" + resolved "https://registry.yarnpkg.com/messageformat-parser/-/messageformat-parser-4.1.3.tgz#b824787f57fcda7d50769f5b63e8d4fda68f5b9e" + +messageformat@^2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/messageformat/-/messageformat-2.3.0.tgz#de263c49029d5eae65d7ee25e0754f57f425ad91" + dependencies: + make-plural "^4.3.0" + messageformat-formatters "^2.0.1" + messageformat-parser "^4.1.2" + methods@~1.1.2: version "1.1.2" resolved "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee" @@ -6251,6 +6290,10 @@ multicast-dns@^6.0.1: dns-packet "^1.3.1" thunky "^1.0.2" +mustache@^4.0.1: + version "4.2.0" + resolved "https://registry.yarnpkg.com/mustache/-/mustache-4.2.0.tgz#e5892324d60a12ec9c2a73359edca52972bf6f64" + mustache@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/mustache/-/mustache-4.1.0.tgz#8c1b042238a982d2eb2d30efc6c14296ae3f699d" @@ -8210,6 +8253,10 @@ split-string@^3.0.1, split-string@^3.0.2: dependencies: extend-shallow "^3.0.0" +sprintf-js@^1.1.2: + version "1.1.2" + resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.1.2.tgz#da1765262bf8c0f571749f2ad6c26300207ae673" + sprintf-js@~1.0.2: version "1.0.3" resolved "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"