-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #315 from ice-lab/fix/i18n-for-component-builder
fix: web i18n
- Loading branch information
Showing
3 changed files
with
32 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,56 @@ | ||
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'; | ||
import { ConfigProvider, Loading } from '@alifd/next'; | ||
import enUSNextMessages from '@alifd/next/lib/locale/en-us'; | ||
import zhCNNextMessages from '@alifd/next/lib/locale/zh-cn'; | ||
|
||
// 引入 locale 配置文件 | ||
import enUSLocale from './locales/en-US.json'; | ||
import zhCNLocale from './locales/zh-CN.json'; | ||
import callService from './callService'; | ||
import enUSMessages from './locales/en-US.json'; | ||
import zhCNMessages from './locales/zh-CN.json'; | ||
|
||
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, | ||
}, | ||
}; | ||
|
||
// 找到当前语言即使用的包 | ||
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 = (locale: string) =>{ | ||
let localeMessage = localeMessages[locale]; | ||
if (!localeMessage) { | ||
locale = DEFAULT_LOCALE; | ||
localeMessage = localeMessages[locale]; | ||
} | ||
return createIntl({locale, messages: localeMessage.messages}); | ||
} | ||
export const LocaleProvider = (props)=>{ | ||
const [i18n,setI18n] = useState(defaultIntl); | ||
const [loading,setLoading] = useState(false) | ||
const [i18n, setI18n] = useState(() => getIntl(DEFAULT_LOCALE)); | ||
const [loading, setLoading] = useState(true) | ||
useEffect(()=>{ | ||
async function initI18n(){ | ||
try{ | ||
setLoading(true); | ||
const lang = await callService('common','getLanguage'); | ||
setI18n(changeLanguage(lang)); | ||
}catch(e){ | ||
Notification.error({ content: e.message }); | ||
} | ||
finally{ | ||
try { | ||
const lang = await callService('common', 'getLanguage'); | ||
setI18n(getIntl(lang)); | ||
} catch(e) { | ||
// ignore i18n error, just using default language | ||
} finally { | ||
setLoading(false); | ||
} | ||
} | ||
initI18n(); | ||
|
||
},[]); | ||
|
||
// const i18n = createIntl({locale:lang,messages:localeMessages[lang].messages}); | ||
return ( | ||
<RawIntlProvider value={i18n}> | ||
<ConfigProvider> | ||
{loading?<Loading visible={loading} style={{width: '100%', height:'80vh'}} /> :React.Children.only(props.children)} | ||
<ConfigProvider locale={localeMessages[i18n.locale].nextMessages}> | ||
{loading ? <Loading visible={loading} style={{width: '100%', height:'80vh'}} /> : React.Children.only(props.children)} | ||
</ConfigProvider> | ||
</RawIntlProvider> | ||
) | ||
} | ||
|
||
export function checkI18nReady(intl: IntlShape){ | ||
return intl.locale !== 'default' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters