diff --git a/src/components/GlobalHeader/RightContent.js b/src/components/GlobalHeader/RightContent.js index 1c74d47ff4f4932e25259f0484436d723530b998..d856d435d1fc866ecc08de8934ab651adaddbcdc 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 0000000000000000000000000000000000000000..4f1e782b8fe077b96f887c1af4d22652e2233f52 --- /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 b11e50dbb00b9fab1d12a0d7c40e16fdc1e38b8d..c3db6fcbd49508613762f36bbdd818088de1b061 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" + /> + + + ); }