From bdecb146dd2cdb476215cd97f6b8201fd20d6d3b Mon Sep 17 00:00:00 2001 From: shanghaitao <359310444@qq.com> Date: Tue, 7 May 2019 09:44:26 +0800 Subject: [PATCH] feat: use antd LocaleProvider --- src/containers/shared/App/IntlWrapper.tsx | 64 ++++++++--------------- src/locales/{en-US.json => en_US.json} | 0 src/locales/loader.ts | 41 +++++++++++++++ src/locales/{zh-CN.json => zh_CN.json} | 0 4 files changed, 63 insertions(+), 42 deletions(-) rename src/locales/{en-US.json => en_US.json} (100%) create mode 100644 src/locales/loader.ts rename src/locales/{zh-CN.json => zh_CN.json} (100%) diff --git a/src/containers/shared/App/IntlWrapper.tsx b/src/containers/shared/App/IntlWrapper.tsx index 8dd7ccd..624b5f2 100644 --- a/src/containers/shared/App/IntlWrapper.tsx +++ b/src/containers/shared/App/IntlWrapper.tsx @@ -1,55 +1,33 @@ import * as React from 'react' import intl from 'react-intl-universal' import { find } from 'lodash' -import { Select } from 'antd' +import { Select, LocaleProvider } from 'antd' import * as styles from './index.scss' import { setCookie } from '@utils/index' import { COOKIE_KEYS } from '@constants/index' import PageLoading from '@components/PageLoading' +import { SUPPOER_LOCALES, LOCALES_KEYS, getLocaleLoader } from '@locales/loader' -enum LOCALES { - EN_US = 'en-US', - ZH_CN = 'zh-CN' +interface IS { + currentLocale: string + antdLocaleData: PlainObject } -const SUPPOER_LOCALES = [ - { - name: 'English', - value: LOCALES.EN_US - }, - { - name: '简体中文', - value: LOCALES.ZH_CN - } -] - -export default class IntlWrapper extends React.Component { - state = { currentLocale: '' } - - getLangLoader(locale: string): Promise { - switch (locale) { - case LOCALES.ZH_CN: - return import(/* webpackChunkName: "zh-CN" */ '@locales/zh-CN.json').then(m => m.default) - default: - return import(/* webpackChunkName: "en-US" */ '@locales/en-US.json').then(m => m.default) - } - } +export default class IntlWrapper extends React.Component<{}, IS> { + state = { currentLocale: '', antdLocaleData: null } loadLocales() { let currentLocale = intl.determineLocale({ cookieLocaleKey: COOKIE_KEYS.LANG }) // default is English if (!find(SUPPOER_LOCALES, { value: currentLocale })) { - currentLocale = LOCALES.EN_US + currentLocale = LOCALES_KEYS.EN_US } - this.getLangLoader(currentLocale) - .then(data => { - return intl.init({ currentLocale, locales: { [currentLocale]: data } }) - }) - .then(() => { - // After loading CLDR locale data, start to render - this.setState({ currentLocale }) + getLocaleLoader(currentLocale).then(({ localeData, antdLocaleData }) => { + intl.init({ currentLocale, locales: { [currentLocale]: localeData } }).then(() => { + this.setState({ antdLocaleData, currentLocale }) }) + }) } onSelectLocale = (val: string) => { @@ -62,24 +40,26 @@ export default class IntlWrapper extends React.Component { } render() { - const { currentLocale } = this.state + const { currentLocale, antdLocaleData } = this.state if (!currentLocale) { return } const selectLanguage = ( ) return ( - - {selectLanguage} - {this.props.children} - + + + {selectLanguage} + {this.props.children} + + ) } } diff --git a/src/locales/en-US.json b/src/locales/en_US.json similarity index 100% rename from src/locales/en-US.json rename to src/locales/en_US.json diff --git a/src/locales/loader.ts b/src/locales/loader.ts new file mode 100644 index 0000000..57762aa --- /dev/null +++ b/src/locales/loader.ts @@ -0,0 +1,41 @@ +export enum LOCALES_KEYS { + EN_US = 'en-US', + ZH_CN = 'zh-CN' +} + +export const SUPPOER_LOCALES = [ + { + name: 'English', + value: LOCALES_KEYS.EN_US + }, + { + name: '简体中文', + value: LOCALES_KEYS.ZH_CN + } +] + +export interface LocaleResponse { + localeData: StringObject + antdLocaleData: PlainObject +} + +export function getLocaleLoader(locale: string): Promise { + switch (locale) { + case LOCALES_KEYS.ZH_CN: + return new Promise(async resolve => { + const loc = await import(/* webpackChunkName: "zh-CN" */ './zh_CN.json').then(m => m.default) + const antdLoc = await import(/* webpackChunkName: "antd-zh-CN" */ 'antd/lib/locale-provider/zh_CN').then( + m => m.default + ) + resolve({ localeData: loc, antdLocaleData: antdLoc }) + }) + default: + return new Promise(async resolve => { + const loc = await import(/* webpackChunkName: "en-US" */ './en_US.json').then(m => m.default) + const antdLoc = await import(/* webpackChunkName: "antd-en-US" */ 'antd/lib/locale-provider/en_US').then( + m => m.default + ) + resolve({ localeData: loc, antdLocaleData: antdLoc }) + }) + } +} diff --git a/src/locales/zh-CN.json b/src/locales/zh_CN.json similarity index 100% rename from src/locales/zh-CN.json rename to src/locales/zh_CN.json