From f07927f813f6b3fe6b0fb23dde99689498efd3dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Sun, 1 Jul 2018 20:06:13 +0800 Subject: [PATCH] Increase dynamic switching local --- src/components/GlobalHeader/RightContent.js | 20 ++++++-- src/locale/localeContext.js | 53 +++++++++++++++++++++ src/router.js | 46 +++++++----------- 3 files changed, 85 insertions(+), 34 deletions(-) create mode 100644 src/locale/localeContext.js diff --git a/src/components/GlobalHeader/RightContent.js b/src/components/GlobalHeader/RightContent.js index 1c74d47f..d856d435 100644 --- a/src/components/GlobalHeader/RightContent.js +++ b/src/components/GlobalHeader/RightContent.js @@ -3,6 +3,7 @@ import { FormattedMessage } from 'react-intl'; import { Spin, Tag, Menu, Icon, Dropdown, Avatar, Tooltip, Button } from 'antd'; import moment from 'moment'; import groupBy from 'lodash/groupBy'; +import LocaleContext from '../../locale/localeContext'; import NoticeIcon from '../NoticeIcon'; import HeaderSearch from '../HeaderSearch'; import styles from './index.less'; @@ -47,7 +48,6 @@ export default class GlobalHeaderRight extends PureComponent { } else { localStorage.setItem('locale', 'zh-CN'); } - location.reload(); }; render() { @@ -153,9 +153,21 @@ export default class GlobalHeaderRight extends PureComponent { ) : ( )} - + + {context => { + return ( + + ); + }} + ); } diff --git a/src/locale/localeContext.js b/src/locale/localeContext.js new file mode 100644 index 00000000..4f1e782b --- /dev/null +++ b/src/locale/localeContext.js @@ -0,0 +1,53 @@ +import React from 'react'; +import { addLocaleData, IntlProvider } from 'react-intl'; +import { LocaleProvider } from 'antd'; +import enLocale from './en-US'; +import cnLocale from './zh-CN'; + +const Context = React.createContext(); + +function getLang() { + if (window.localStorage && localStorage.getItem('locale')) { + return localStorage.getItem('locale'); + } + return (navigator.language || navigator.browserLanguage).toLowerCase() === 'en-us' + ? 'en-US' + : 'zh-CN'; +} + +export class LocalComponent extends React.PureComponent { + state = { + locale: getLang(), + }; + + changeLocal = () => { + this.setState({ + locale: getLang(), + }); + }; + + render() { + const { children } = this.props; + const { locale } = this.state; + return ( + + + {({ appLocale }) => { + addLocaleData(appLocale.data); + return ( + + {children} + + ); + }} + + + ); + } +} +export default Context; diff --git a/src/router.js b/src/router.js index b11e50db..c3db6fcb 100644 --- a/src/router.js +++ b/src/router.js @@ -1,14 +1,12 @@ import React from 'react'; import { routerRedux, Route, Switch } from 'dva/router'; -import { LocaleProvider, Spin } from 'antd'; +import { Spin } from 'antd'; import dynamic from 'dva/dynamic'; -import { addLocaleData, IntlProvider } from 'react-intl'; +import { LocalComponent } from './locale/localeContext'; import { getRouterData } from './common/router'; import Authorized from './utils/Authorized'; import { getQueryPath } from './utils/utils'; import styles from './index.less'; -import enLocale from './locale/en-US'; -import cnLocale from './locale/zh-CN'; const { ConnectedRouter } = routerRedux; const { AuthorizedRoute } = Authorized; @@ -17,37 +15,25 @@ dynamic.setDefaultLoadingComponent(() => { return ; }); -function getLang() { - if (window.localStorage && localStorage.getItem('locale')) { - return localStorage.getItem('locale'); - } - return (navigator.language || navigator.browserLanguage).toLowerCase() === 'en-us' - ? 'en-US' - : 'zh-CN'; -} - function RouterConfig({ history, app }) { const routerData = getRouterData(app); const UserLayout = routerData['/user'].component; const BasicLayout = routerData['/'].component; - const appLocale = getLang() === 'zh-CN' ? cnLocale : enLocale; - addLocaleData(appLocale.data); + return ( - - - - - - } - authority={['admin', 'user']} - redirectPath="/user/login" - /> - - - - + + + + + } + authority={['admin', 'user']} + redirectPath="/user/login" + /> + + + ); } -- GitLab