From de4a5c5849bacf4bc3ac60bc2601ca16bbbd7c68 Mon Sep 17 00:00:00 2001 From: suanmei Date: Fri, 8 May 2020 21:11:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20i18n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 9 +++++ package.json | 1 + packages/json-schema-editor/src/i18n/index.ts | 2 ++ .../src/i18n/translations/en.json | 4 ++- .../src/i18n/translations/zh.json | 4 ++- packages/json-schema-editor/src/index.tsx | 34 +++++++++++++++---- rollup.config.js | 2 ++ 7 files changed, 47 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 89eaaf7..b163679 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2936,6 +2936,15 @@ } } }, + "@rollup/plugin-json": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@rollup/plugin-json/-/plugin-json-4.0.3.tgz", + "integrity": "sha512-QMUT0HZNf4CX17LMdwaslzlYHUKTYGuuk34yYIgZrNdu+pMEfqMS55gck7HEeHBKXHM4cz5Dg1OVwythDdbbuQ==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^3.0.8" + } + }, "@rollup/plugin-node-resolve": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@rollup/plugin-node-resolve/-/plugin-node-resolve-7.1.3.tgz", diff --git a/package.json b/package.json index 42c6953..c16c360 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@babel/preset-react": "^7.9.4", "@babel/preset-typescript": "^7.9.0", "@rollup/plugin-commonjs": "^11.0.2", + "@rollup/plugin-json": "^4.0.3", "@rollup/plugin-node-resolve": "^7.1.3", "@rollup/plugin-strip": "^1.3.2", "@rollup/plugin-url": "^4.0.2", diff --git a/packages/json-schema-editor/src/i18n/index.ts b/packages/json-schema-editor/src/i18n/index.ts index c1f896e..d8637b1 100644 --- a/packages/json-schema-editor/src/i18n/index.ts +++ b/packages/json-schema-editor/src/i18n/index.ts @@ -10,6 +10,8 @@ i18n resources: { en, zh, + 'en-US': en, + 'zh-CN': zh, }, keySeparator: false, // we do not use keys in form messages.welcome interpolation: { diff --git a/packages/json-schema-editor/src/i18n/translations/en.json b/packages/json-schema-editor/src/i18n/translations/en.json index 399a52d..323faf1 100644 --- a/packages/json-schema-editor/src/i18n/translations/en.json +++ b/packages/json-schema-editor/src/i18n/translations/en.json @@ -1,3 +1,5 @@ { - "home": "home" + "translation": { + "import_json": "Import JSON" + } } diff --git a/packages/json-schema-editor/src/i18n/translations/zh.json b/packages/json-schema-editor/src/i18n/translations/zh.json index c218cee..33820fa 100644 --- a/packages/json-schema-editor/src/i18n/translations/zh.json +++ b/packages/json-schema-editor/src/i18n/translations/zh.json @@ -1,3 +1,5 @@ { - "home": "首页" + "translation": { + "import_json": "导入 JSON" + } } diff --git a/packages/json-schema-editor/src/index.tsx b/packages/json-schema-editor/src/index.tsx index 524a3cb..eed07cf 100644 --- a/packages/json-schema-editor/src/index.tsx +++ b/packages/json-schema-editor/src/index.tsx @@ -1,16 +1,36 @@ /** @jsx jsx */ -import { jsx } from '@emotion/core'; +import { useEffect } from 'react'; +import { Global, jsx, css } from '@emotion/core'; import { Button } from 'antd'; import { useTranslation } from 'react-i18next'; -import i18n from './i18n'; +import './i18n'; -i18n.changeLanguage('en'); +interface EditorProps { + lang?: string; +} + +function JsonSchemaEditor({ lang }: EditorProps): JSX.Element { + const { t, i18n } = useTranslation(); + + useEffect(() => { + if (!lang) return; + i18n.changeLanguage(lang); + }, [i18n, lang]); -function JsonSchemaEditor(): JSX.Element { - const { t } = useTranslation(); return ( -
- +
+ +
); } diff --git a/rollup.config.js b/rollup.config.js index 601fef9..3b370df 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,6 +2,7 @@ import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import strip from '@rollup/plugin-strip'; import url from '@rollup/plugin-url'; +import json from '@rollup/plugin-json'; import babel from 'rollup-plugin-babel'; import svgr from '@svgr/rollup'; import { sizeSnapshot } from 'rollup-plugin-size-snapshot'; @@ -14,6 +15,7 @@ export default { plugins: [ resolve({ extensions, preferBuiltins: true }), commonjs(), + json(), babel({ extensions, exclude: 'node_modules/**',