From 2cb34253802097fe9392935a2f6c11a394c87be3 Mon Sep 17 00:00:00 2001 From: sspku-yqLiu Date: Tue, 21 Jul 2020 11:48:32 +0800 Subject: [PATCH 01/10] feat: i18n for Iceworks-component-generater --- .../src/extension.ts | 3 +- .../iceworks-component-builder/src/i18n.ts | 16 +++ .../src/locales/en-US.json | 3 + .../src/locales/zh-CN.json | 3 + .../iceworks-component-builder/tsconfig.json | 3 +- .../web/package.json | 3 +- .../web/src/i18n.tsx | 57 +++++++++ .../web/src/locales/en-US.json | 11 ++ .../web/src/locales/zh-CN.json | 11 ++ .../web/src/pages/Home/index.tsx | 114 ++++++++++-------- .../web/tsconfig.json | 3 +- packages/common-service/package.json | 2 +- 12 files changed, 174 insertions(+), 55 deletions(-) create mode 100644 extensions/iceworks-component-builder/src/i18n.ts create mode 100644 extensions/iceworks-component-builder/src/locales/en-US.json create mode 100644 extensions/iceworks-component-builder/src/locales/zh-CN.json create mode 100644 extensions/iceworks-component-builder/web/src/i18n.tsx create mode 100644 extensions/iceworks-component-builder/web/src/locales/en-US.json create mode 100644 extensions/iceworks-component-builder/web/src/locales/zh-CN.json diff --git a/extensions/iceworks-component-builder/src/extension.ts b/extensions/iceworks-component-builder/src/extension.ts index ede58369a..46c3f9802 100644 --- a/extensions/iceworks-component-builder/src/extension.ts +++ b/extensions/iceworks-component-builder/src/extension.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import { connectService, getHtmlForWebview } from '@iceworks/vscode-webview/lib/vscode'; import { initExtension, Logger } from '@iceworks/common-service'; import services from './services/index'; +import i18n from './i18n'; // eslint-disable-next-line const { name, version } = require('../package.json'); @@ -22,7 +23,7 @@ export function activate(context: vscode.ExtensionContext) { initExtension(context); function activeWebview() { - const webviewPanel: vscode.WebviewPanel = window.createWebviewPanel('iceworks', '生成组件 - Iceworks', ViewColumn.One, { + const webviewPanel: vscode.WebviewPanel = window.createWebviewPanel('iceworks', i18n.format('extension.iceworksComponentBuilder.extension.webviewTitle'), ViewColumn.One, { enableScripts: true, retainContextWhenHidden: true, }); diff --git a/extensions/iceworks-component-builder/src/i18n.ts b/extensions/iceworks-component-builder/src/i18n.ts new file mode 100644 index 000000000..2bbee6203 --- /dev/null +++ b/extensions/iceworks-component-builder/src/i18n.ts @@ -0,0 +1,16 @@ +import * as vscode from 'vscode'; +import I18nService from '@iceworks/i18n'; +import * as zhCNTextMap from './locales/zh-CN.json'; +import * as enUSTextMap from './locales/en-US.json'; + + +// 注册语言表 +const i18n = new I18nService(); + +i18n.registry('zh-cn', zhCNTextMap); +i18n.registry('en', enUSTextMap); + +// 设置使用的语言 +i18n.setLocal(vscode.env.language); + +export default i18n; \ No newline at end of file diff --git a/extensions/iceworks-component-builder/src/locales/en-US.json b/extensions/iceworks-component-builder/src/locales/en-US.json new file mode 100644 index 000000000..aeb9b76e1 --- /dev/null +++ b/extensions/iceworks-component-builder/src/locales/en-US.json @@ -0,0 +1,3 @@ +{ + "extension.iceworksComponentBuilder.extension.webviewTitle":"Generate Components - Iceworks" +} \ No newline at end of file diff --git a/extensions/iceworks-component-builder/src/locales/zh-CN.json b/extensions/iceworks-component-builder/src/locales/zh-CN.json new file mode 100644 index 000000000..152187e57 --- /dev/null +++ b/extensions/iceworks-component-builder/src/locales/zh-CN.json @@ -0,0 +1,3 @@ +{ + "extension.iceworksComponentBuilder.extension.webviewTitle":"生成组件 - Iceworks" +} \ No newline at end of file diff --git a/extensions/iceworks-component-builder/tsconfig.json b/extensions/iceworks-component-builder/tsconfig.json index 2ddd1bbab..0871a64b2 100644 --- a/extensions/iceworks-component-builder/tsconfig.json +++ b/extensions/iceworks-component-builder/tsconfig.json @@ -2,7 +2,8 @@ "extends": "../../tsconfig.extension.json", "compilerOptions": { "rootDir": "./", - "outDir": "build" + "outDir": "build", + "resolveJsonModule": true }, "include": [ "src" diff --git a/extensions/iceworks-component-builder/web/package.json b/extensions/iceworks-component-builder/web/package.json index 2b14eba5f..887b9619d 100644 --- a/extensions/iceworks-component-builder/web/package.json +++ b/extensions/iceworks-component-builder/web/package.json @@ -20,7 +20,8 @@ "@iceworks/vscode-webview": "^0.1.1", "moment": "^2.24.0", "react": "^16.8.0", - "react-dom": "^16.8.0" + "react-dom": "^16.8.0", + "react-intl": "^5.3.2" }, "scripts": { "start": "icejs start --disable-reload", diff --git a/extensions/iceworks-component-builder/web/src/i18n.tsx b/extensions/iceworks-component-builder/web/src/i18n.tsx new file mode 100644 index 000000000..c24f6e61f --- /dev/null +++ b/extensions/iceworks-component-builder/web/src/i18n.tsx @@ -0,0 +1,57 @@ +import {createIntl,RawIntlProvider} from 'react-intl'; +import React, { useEffect, useState } from 'react' +import { ConfigProvider, Loading, Notification } from '@alifd/next'; + +// 引入 locale 配置文件 +import enUSLocale from './locales/en-US.json'; +import zhCNLocale from './locales/zh-CN.json'; +import callService from './callService'; + +// 创建语言包 +export const localeMessages = { + 'en': { + messages:enUSLocale, + nextLocale:'zhCN', + reactLocale:'zh-CN' + }, + 'zh-cn':{ + messages:zhCNLocale, + nextLocal:'enUS', + reactLocale:'en' + } +} + +// 找到当前语言即使用的包 +export const defaultIntl = createIntl({locale:'zh-CN',messages:localeMessages['zh-cn'].messages}) +const changeLanguage = (newLang: string) =>{ + return createIntl({locale:localeMessages[newLang].reactLocale,messages:localeMessages[newLang].messages}); +} +export const LocaleProvider = (props)=>{ + const [i18n,setI18n] = useState(defaultIntl); + const [loading,setLoading] = useState(false) + useEffect(()=>{ + async function initI18n(){ + try{ + setLoading(true); + const lang = await callService('common','getLanguage'); + setI18n(changeLanguage(lang)); + }catch(e){ + Notification.error({ content: e.message }); + } + finally{ + setLoading(false); + } + } + initI18n(); + + },[]); + + // const i18n = createIntl({locale:lang,messages:localeMessages[lang].messages}); + return ( + + + {loading? :React.Children.only(props.children)} + + + ) +} \ No newline at end of file diff --git a/extensions/iceworks-component-builder/web/src/locales/en-US.json b/extensions/iceworks-component-builder/web/src/locales/en-US.json new file mode 100644 index 000000000..970e00619 --- /dev/null +++ b/extensions/iceworks-component-builder/web/src/locales/en-US.json @@ -0,0 +1,11 @@ +{ + "web.ComponentBuiilder.Home.getMaterialError":"Fail to obtain materials. Please try again later.", + "web.ComponentBuiilder.Home.getDataError":"Fail to obtain material data. Please try again later.", + "web.ComponentBuiilder.Home.noComponentName":"Please Enter Component Name.", + "web.ComponentBuiilder.Home.didNotSeletBlock":"Please choose a block before generate.", + "web.ComponentBuiilder.Home.generateSuccess":"Generate component successfully.", + "web.ComponentBuiilder.Home.inputComponentName":"1. Enter Component Name.", + "web.ComponentBuiilder.Home.inputComponentNamePlaceHolder":"Component name must start with A-Z and only contains letters and numbers. Special characters are not allowed.", + "web.ComponentBuiilder.Home.selectBlock":"2. Choose A Block", + "web.ComponentBuiilder.Home.generate":"Generate Component" +} \ No newline at end of file diff --git a/extensions/iceworks-component-builder/web/src/locales/zh-CN.json b/extensions/iceworks-component-builder/web/src/locales/zh-CN.json new file mode 100644 index 000000000..7db2a3103 --- /dev/null +++ b/extensions/iceworks-component-builder/web/src/locales/zh-CN.json @@ -0,0 +1,11 @@ +{ + "web.ComponentBuiilder.Home.getMaterialError":"获取物料源信息失败,请稍后再试。", + "web.ComponentBuiilder.Home.getDataError":"获取物料集合信息失败,请稍后再试。", + "web.ComponentBuiilder.Home.noComponentName":"请填写组件名。", + "web.ComponentBuiilder.Home.didNotSeletBlock":"请选择使用的区块。", + "web.ComponentBuiilder.Home.generateSuccess":"组件生成成功!", + "web.ComponentBuiilder.Home.inputComponentName":"1. 填写组件名:", + "web.ComponentBuiilder.Home.inputComponentNamePlaceHolder":"名称必须英文字母 A-Z 开头,只包含英文和数字,不允许有特殊字符", + "web.ComponentBuiilder.Home.selectBlock":"2. 选择使用的区块:", + "web.ComponentBuiilder.Home.generate":"生成组件" +} \ No newline at end of file diff --git a/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx b/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx index 762f8f4a9..f8c711f86 100644 --- a/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx +++ b/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx @@ -1,57 +1,64 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { Notification, Button, Input } from '@alifd/next'; import Material from '@iceworks/material-ui'; +import { LocaleProvider } from '@/i18n'; +import { useIntl, FormattedMessage } from 'react-intl'; import callService from '../../callService'; import styles from './index.module.scss'; -async function onSettingsClick() { - try { - await callService('common', 'executeCommand', 'iceworksApp.configHelper.start'); - } catch (e) { - Notification.error({ content: e.message }); - } -} - -async function getSources() { - let sources = []; - try { - sources = await callService('material', 'getSourcesByProjectType'); - } catch (e) { - Notification.error({ content: '获取物料源信息失败,请稍后再试。' }); - } - - console.log('getSources', sources); - return sources; -} - -async function getData(source: string) { - let data = {}; - try { - data = await callService('material', 'getData', source); - } catch (e) { - Notification.error({ content: '获取物料集合信息失败,请稍后再试。' }); - } - console.log('getData', data); - return data; -} - -function validateData({ block, componentName }) { - if (!componentName) { - return '请填写组件名。'; - } - - if (!block) { - return '请选择使用的区块。'; - } - - return ''; -} const Home = () => { + const intl = useIntl(); const [selectedBlock, setSelectedBlock] = useState(); const [componentName, setComponentName] = useState(''); const [isCreating, setIsCreating] = useState(false); + async function onSettingsClick() { + try { + await callService('common', 'getLanguage'); + await callService('common', 'executeCommand', 'iceworksApp.configHelper.start'); + } catch (e) { + Notification.error({ content: e.message }); + } + } + + async function getSources() { + let sources = []; + try { + await callService('common', 'getLanguage'); + sources = await callService('material', 'getSourcesByProjectType'); + } catch (e) { + Notification.error({ content: intl.formatMessage({id:'web.ComponentBuiilder.Home.getMaterialError'})}); + } + + console.log('getSources', sources); + return sources; + } + + async function getData(source: string) { + let data = {}; + try { + await callService('common', 'getLanguage'); + data = await callService('material', 'getData', source); + } catch (e) { + Notification.error({ content: intl.formatMessage({id:'web.ComponentBuiilder.Home.getDataError'})}); + } + console.log('getData', data); + return data; + } + + function validateData({ block, componentName }) { + if (!componentName) { + return intl.formatMessage({id:'web.ComponentBuiilder.Home.noComponentName'}); + } + + if (!block) { + return intl.formatMessage({id:'web.ComponentBuiilder.Home.didNotSeletBlock'}); + } + + return ''; + } + function onSelect(block) { setSelectedBlock(block); } @@ -87,20 +94,19 @@ const Home = () => { } setIsCreating(false); - Notification.success({ content: '组件生成成功!' }); + Notification.success({ content: intl.formatMessage({id:'web.ComponentBuiilder.Home.generateSuccess'})}); resetData(); } - return (
- 1. 填写组件名: +
setComponentName(value)} @@ -110,7 +116,7 @@ const Home = () => {
- 2. 选择使用的区块: +
{
); }; -export default Home; +const IntlHome = ()=>{ + return ( + + + + ) +} + +export default IntlHome; diff --git a/extensions/iceworks-component-builder/web/tsconfig.json b/extensions/iceworks-component-builder/web/tsconfig.json index 4fd0a6fba..7c2d90eea 100644 --- a/extensions/iceworks-component-builder/web/tsconfig.json +++ b/extensions/iceworks-component-builder/web/tsconfig.json @@ -14,7 +14,8 @@ "ice/*": [ ".ice/pages/*" ] - } + }, + "resolveJsonModule": true }, "include": [ "src/*", diff --git a/packages/common-service/package.json b/packages/common-service/package.json index 7a9fd041e..7a61c91f4 100644 --- a/packages/common-service/package.json +++ b/packages/common-service/package.json @@ -24,4 +24,4 @@ "access": "public" }, "gitHead": "fc5b35f95ab4cc24898845916acf598c2f34d576" -} \ No newline at end of file +} From 0fd22beac94af5bf6aebdb3584a901e59f0030bd Mon Sep 17 00:00:00 2001 From: sspku-yqLiu Date: Tue, 21 Jul 2020 22:55:33 +0800 Subject: [PATCH 02/10] fix: fix double alerts bug and add iceworks/i18n to dep --- extensions/iceworks-component-builder/package.json | 3 ++- .../iceworks-component-builder/web/src/i18n.tsx | 8 ++++++-- .../web/src/pages/Home/index.tsx | 12 +++++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/extensions/iceworks-component-builder/package.json b/extensions/iceworks-component-builder/package.json index 62e547b38..83cd23a2b 100644 --- a/extensions/iceworks-component-builder/package.json +++ b/extensions/iceworks-component-builder/package.json @@ -108,7 +108,8 @@ "@iceworks/common-service": "^0.1.3", "@iceworks/material-service": "^0.1.3", "@iceworks/project-service": "^0.1.4", - "@iceworks/vscode-webview": "^0.1.1" + "@iceworks/vscode-webview": "^0.1.1", + "@iceworks/i18n":"^0.0.1" }, "homepage": "https://github.com/ice-lab/iceworks/blob/master/extensions/iceworks-component-builder/README.md", "bugs": { diff --git a/extensions/iceworks-component-builder/web/src/i18n.tsx b/extensions/iceworks-component-builder/web/src/i18n.tsx index c24f6e61f..8386cc3a9 100644 --- a/extensions/iceworks-component-builder/web/src/i18n.tsx +++ b/extensions/iceworks-component-builder/web/src/i18n.tsx @@ -1,4 +1,4 @@ -import {createIntl,RawIntlProvider} from 'react-intl'; +import {createIntl,RawIntlProvider, IntlShape} from 'react-intl'; import React, { useEffect, useState } from 'react' import { ConfigProvider, Loading, Notification } from '@alifd/next'; @@ -22,7 +22,7 @@ export const localeMessages = { } // 找到当前语言即使用的包 -export const defaultIntl = createIntl({locale:'zh-CN',messages:localeMessages['zh-cn'].messages}) +export const defaultIntl = createIntl({locale:'default',messages:localeMessages['zh-cn'].messages}) const changeLanguage = (newLang: string) =>{ return createIntl({locale:localeMessages[newLang].reactLocale,messages:localeMessages[newLang].messages}); } @@ -54,4 +54,8 @@ export const LocaleProvider = (props)=>{ ) +} + +export function checkI18nReady(intl: IntlShape){ + return intl.locale !== 'default' } \ No newline at end of file diff --git a/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx b/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx index f8c711f86..054748368 100644 --- a/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx +++ b/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx @@ -1,12 +1,11 @@ import React, { useState, useEffect } from 'react'; import { Notification, Button, Input } from '@alifd/next'; import Material from '@iceworks/material-ui'; -import { LocaleProvider } from '@/i18n'; +import { LocaleProvider, checkI18nReady } from '@/i18n'; import { useIntl, FormattedMessage } from 'react-intl'; import callService from '../../callService'; import styles from './index.module.scss'; - const Home = () => { const intl = useIntl(); const [selectedBlock, setSelectedBlock] = useState(); @@ -23,9 +22,11 @@ const Home = () => { } async function getSources() { + if(!checkI18nReady(intl)){ + return; + } let sources = []; try { - await callService('common', 'getLanguage'); sources = await callService('material', 'getSourcesByProjectType'); } catch (e) { Notification.error({ content: intl.formatMessage({id:'web.ComponentBuiilder.Home.getMaterialError'})}); @@ -36,9 +37,11 @@ const Home = () => { } async function getData(source: string) { + if(!checkI18nReady(intl)){ + return; + } let data = {}; try { - await callService('common', 'getLanguage'); data = await callService('material', 'getData', source); } catch (e) { Notification.error({ content: intl.formatMessage({id:'web.ComponentBuiilder.Home.getDataError'})}); @@ -51,7 +54,6 @@ const Home = () => { if (!componentName) { return intl.formatMessage({id:'web.ComponentBuiilder.Home.noComponentName'}); } - if (!block) { return intl.formatMessage({id:'web.ComponentBuiilder.Home.didNotSeletBlock'}); } From 1a7b0e712905566f85201b8d8c76ee4e097cedb1 Mon Sep 17 00:00:00 2001 From: alvinhui Date: Wed, 22 Jul 2020 11:37:29 +0800 Subject: [PATCH 03/10] fix: web i18n --- .../iceworks-component-builder/src/i18n.ts | 1 - .../web/src/i18n.tsx | 38 +++++++++---------- .../web/src/pages/Home/index.tsx | 8 +--- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/extensions/iceworks-component-builder/src/i18n.ts b/extensions/iceworks-component-builder/src/i18n.ts index 2bbee6203..1c87e8efe 100644 --- a/extensions/iceworks-component-builder/src/i18n.ts +++ b/extensions/iceworks-component-builder/src/i18n.ts @@ -3,7 +3,6 @@ import I18nService from '@iceworks/i18n'; import * as zhCNTextMap from './locales/zh-CN.json'; import * as enUSTextMap from './locales/en-US.json'; - // 注册语言表 const i18n = new I18nService(); diff --git a/extensions/iceworks-component-builder/web/src/i18n.tsx b/extensions/iceworks-component-builder/web/src/i18n.tsx index 8386cc3a9..2c33d8e15 100644 --- a/extensions/iceworks-component-builder/web/src/i18n.tsx +++ b/extensions/iceworks-component-builder/web/src/i18n.tsx @@ -1,4 +1,4 @@ -import {createIntl,RawIntlProvider, IntlShape} from 'react-intl'; +import { createIntl, RawIntlProvider } from 'react-intl'; import React, { useEffect, useState } from 'react' import { ConfigProvider, Loading, Notification } from '@alifd/next'; @@ -7,7 +7,8 @@ import enUSLocale from './locales/en-US.json'; import zhCNLocale from './locales/zh-CN.json'; import callService from './callService'; -// 创建语言包 +const DEFAULT_LANG = 'zh-cn'; + export const localeMessages = { 'en': { messages:enUSLocale, @@ -21,24 +22,24 @@ export const localeMessages = { } } -// 找到当前语言即使用的包 -export const defaultIntl = createIntl({locale:'default',messages:localeMessages['zh-cn'].messages}) -const changeLanguage = (newLang: string) =>{ - return createIntl({locale:localeMessages[newLang].reactLocale,messages:localeMessages[newLang].messages}); +const getIntl = (lang: string) =>{ + let localeMessage = localeMessages[lang]; + if (!localeMessage) { + localeMessage = localeMessages[DEFAULT_LANG]; + } + return createIntl({locale: localeMessage.reactLocale, messages: localeMessage.messages}); } export const LocaleProvider = (props)=>{ - const [i18n,setI18n] = useState(defaultIntl); - const [loading,setLoading] = useState(false) + const [i18n, setI18n] = useState(() => getIntl(DEFAULT_LANG)); + const [loading, setLoading] = useState(true) useEffect(()=>{ async function initI18n(){ - try{ - setLoading(true); - const lang = await callService('common','getLanguage'); - setI18n(changeLanguage(lang)); - }catch(e){ + try { + const lang = await callService('common', 'getLanguage'); + setI18n(getIntl(lang)); + } catch(e) { Notification.error({ content: e.message }); - } - finally{ + } finally { setLoading(false); } } @@ -46,16 +47,11 @@ export const LocaleProvider = (props)=>{ },[]); - // const i18n = createIntl({locale:lang,messages:localeMessages[lang].messages}); return ( - {loading? :React.Children.only(props.children)} + {loading ? : React.Children.only(props.children)} ) -} - -export function checkI18nReady(intl: IntlShape){ - return intl.locale !== 'default' } \ No newline at end of file diff --git a/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx b/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx index 054748368..3a46cfba1 100644 --- a/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx +++ b/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx @@ -1,7 +1,7 @@ import React, { useState, useEffect } from 'react'; import { Notification, Button, Input } from '@alifd/next'; import Material from '@iceworks/material-ui'; -import { LocaleProvider, checkI18nReady } from '@/i18n'; +import { LocaleProvider } from '@/i18n'; import { useIntl, FormattedMessage } from 'react-intl'; import callService from '../../callService'; import styles from './index.module.scss'; @@ -22,9 +22,6 @@ const Home = () => { } async function getSources() { - if(!checkI18nReady(intl)){ - return; - } let sources = []; try { sources = await callService('material', 'getSourcesByProjectType'); @@ -37,9 +34,6 @@ const Home = () => { } async function getData(source: string) { - if(!checkI18nReady(intl)){ - return; - } let data = {}; try { data = await callService('material', 'getData', source); From e9d2679eb94ac4f32e37ec3cbccd18dab649ec44 Mon Sep 17 00:00:00 2001 From: alvinhui Date: Wed, 22 Jul 2020 11:40:31 +0800 Subject: [PATCH 04/10] style: remove empty line --- extensions/iceworks-component-builder/web/src/i18n.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/extensions/iceworks-component-builder/web/src/i18n.tsx b/extensions/iceworks-component-builder/web/src/i18n.tsx index 2c33d8e15..6ac56ecc6 100644 --- a/extensions/iceworks-component-builder/web/src/i18n.tsx +++ b/extensions/iceworks-component-builder/web/src/i18n.tsx @@ -44,7 +44,6 @@ export const LocaleProvider = (props)=>{ } } initI18n(); - },[]); return ( From 106992a7cf565e4292804341cf81d5a20cdda0cf Mon Sep 17 00:00:00 2001 From: alvinhui Date: Wed, 22 Jul 2020 11:41:59 +0800 Subject: [PATCH 05/10] fix: ignore i18n error --- extensions/iceworks-component-builder/web/src/i18n.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/iceworks-component-builder/web/src/i18n.tsx b/extensions/iceworks-component-builder/web/src/i18n.tsx index 6ac56ecc6..c3ecd2a6f 100644 --- a/extensions/iceworks-component-builder/web/src/i18n.tsx +++ b/extensions/iceworks-component-builder/web/src/i18n.tsx @@ -38,7 +38,7 @@ export const LocaleProvider = (props)=>{ const lang = await callService('common', 'getLanguage'); setI18n(getIntl(lang)); } catch(e) { - Notification.error({ content: e.message }); + // ignore i18n error, just using default language } finally { setLoading(false); } From ac258eb63529ac5854c31e0fbb4847e9c993aa5a Mon Sep 17 00:00:00 2001 From: alvinhui Date: Wed, 22 Jul 2020 11:43:01 +0800 Subject: [PATCH 06/10] chore: remove unnecessary code --- extensions/iceworks-component-builder/web/src/i18n.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/iceworks-component-builder/web/src/i18n.tsx b/extensions/iceworks-component-builder/web/src/i18n.tsx index c3ecd2a6f..76cd8b4d6 100644 --- a/extensions/iceworks-component-builder/web/src/i18n.tsx +++ b/extensions/iceworks-component-builder/web/src/i18n.tsx @@ -1,6 +1,6 @@ import { createIntl, RawIntlProvider } from 'react-intl'; import React, { useEffect, useState } from 'react' -import { ConfigProvider, Loading, Notification } from '@alifd/next'; +import { ConfigProvider, Loading } from '@alifd/next'; // 引入 locale 配置文件 import enUSLocale from './locales/en-US.json'; From 17c9ae200949a35e736496a84bd328c0dc901527 Mon Sep 17 00:00:00 2001 From: alvinhui Date: Wed, 22 Jul 2020 11:43:34 +0800 Subject: [PATCH 07/10] chore: remove unnecessary note --- extensions/iceworks-component-builder/web/src/i18n.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/extensions/iceworks-component-builder/web/src/i18n.tsx b/extensions/iceworks-component-builder/web/src/i18n.tsx index 76cd8b4d6..9cbf06c83 100644 --- a/extensions/iceworks-component-builder/web/src/i18n.tsx +++ b/extensions/iceworks-component-builder/web/src/i18n.tsx @@ -1,11 +1,9 @@ import { createIntl, RawIntlProvider } from 'react-intl'; import React, { useEffect, useState } from 'react' import { ConfigProvider, Loading } from '@alifd/next'; - -// 引入 locale 配置文件 +import callService from './callService'; import enUSLocale from './locales/en-US.json'; import zhCNLocale from './locales/zh-CN.json'; -import callService from './callService'; const DEFAULT_LANG = 'zh-cn'; From 80d45f8a2903a31b6861c4881b214e6a4312d60d Mon Sep 17 00:00:00 2001 From: alvinhui Date: Wed, 22 Jul 2020 12:00:51 +0800 Subject: [PATCH 08/10] feat: support next locale --- .../web/src/i18n.tsx | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/extensions/iceworks-component-builder/web/src/i18n.tsx b/extensions/iceworks-component-builder/web/src/i18n.tsx index 9cbf06c83..da1216d29 100644 --- a/extensions/iceworks-component-builder/web/src/i18n.tsx +++ b/extensions/iceworks-component-builder/web/src/i18n.tsx @@ -1,34 +1,36 @@ import { createIntl, RawIntlProvider } from 'react-intl'; import React, { useEffect, useState } from 'react' import { ConfigProvider, Loading } from '@alifd/next'; +import enUSNextMessages from '@alifd/next/lib/locale/en-us'; +import zhCNNextMessages from '@alifd/next/lib/locale/zh-cn'; + import callService from './callService'; -import enUSLocale from './locales/en-US.json'; -import zhCNLocale from './locales/zh-CN.json'; +import enUSMessages from './locales/en-US.json'; +import zhCNMessages from './locales/zh-CN.json'; -const DEFAULT_LANG = 'zh-cn'; +const DEFAULT_LOCALE = 'zh-cn'; export const localeMessages = { 'en': { - messages:enUSLocale, - nextLocale:'zhCN', - reactLocale:'zh-CN' + messages: enUSMessages, + nextMessages: enUSNextMessages, }, 'zh-cn':{ - messages:zhCNLocale, - nextLocal:'enUS', - reactLocale:'en' - } -} + messages: zhCNMessages, + nextMessages: zhCNNextMessages, + }, +}; -const getIntl = (lang: string) =>{ - let localeMessage = localeMessages[lang]; +const getIntl = (locale: string) =>{ + let localeMessage = localeMessages[locale]; if (!localeMessage) { - localeMessage = localeMessages[DEFAULT_LANG]; + locale = DEFAULT_LOCALE; + localeMessage = localeMessages[locale]; } - return createIntl({locale: localeMessage.reactLocale, messages: localeMessage.messages}); + return createIntl({locale, messages: localeMessage.messages}); } export const LocaleProvider = (props)=>{ - const [i18n, setI18n] = useState(() => getIntl(DEFAULT_LANG)); + const [i18n, setI18n] = useState(() => getIntl(DEFAULT_LOCALE)); const [loading, setLoading] = useState(true) useEffect(()=>{ async function initI18n(){ @@ -46,7 +48,7 @@ export const LocaleProvider = (props)=>{ return ( - + {loading ? : React.Children.only(props.children)} From 660b40d98a7acff3a617a5110b5a0f997c4421ec Mon Sep 17 00:00:00 2001 From: alvinhui Date: Wed, 22 Jul 2020 12:13:29 +0800 Subject: [PATCH 09/10] style: indent --- extensions/iceworks-component-builder/web/src/i18n.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/iceworks-component-builder/web/src/i18n.tsx b/extensions/iceworks-component-builder/web/src/i18n.tsx index da1216d29..bf92a74cf 100644 --- a/extensions/iceworks-component-builder/web/src/i18n.tsx +++ b/extensions/iceworks-component-builder/web/src/i18n.tsx @@ -48,7 +48,7 @@ export const LocaleProvider = (props)=>{ return ( - + {loading ? : React.Children.only(props.children)} From f647b23ea0f521b3c70cc3ed68b0e61ef130420f Mon Sep 17 00:00:00 2001 From: sspku-yqLiu Date: Wed, 22 Jul 2020 15:30:55 +0800 Subject: [PATCH 10/10] fix: delete redundant codes --- .../iceworks-component-builder/web/src/pages/Home/index.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx b/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx index 3a46cfba1..c39651dfe 100644 --- a/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx +++ b/extensions/iceworks-component-builder/web/src/pages/Home/index.tsx @@ -14,7 +14,6 @@ const Home = () => { async function onSettingsClick() { try { - await callService('common', 'getLanguage'); await callService('common', 'executeCommand', 'iceworksApp.configHelper.start'); } catch (e) { Notification.error({ content: e.message }); @@ -51,7 +50,6 @@ const Home = () => { if (!block) { return intl.formatMessage({id:'web.ComponentBuiilder.Home.didNotSeletBlock'}); } - return ''; }