refactor: Language switching

This commit is contained in:
王彦民 2018-12-03 17:36:07 +08:00 committed by zhichen
parent 91d0052563
commit 40d333d529
5 changed files with 22 additions and 14 deletions

View File

@ -11,7 +11,7 @@
* limitations under the License.
*/
export const LANGUAGE_KEY = 'site_language';
export const LANGUAGE_KEY = 'docsite_language';
export const LANGUAGE_SWITCH = 'LANGUAGE_SWITCH';
// TODO: 后端暂时没有统一成功失败标记

View File

@ -50,7 +50,7 @@ import './index.scss';
module.hot && module.hot.accept();
if (!CookieHelp.getValue(LANGUAGE_KEY)) {
CookieHelp.setValue(LANGUAGE_KEY, navigator.language === 'zh-CN' ? 'zh-CN' : 'en-US');
CookieHelp.setValue(LANGUAGE_KEY, navigator.language === 'zh-CN' ? 'zh-cn' : 'en-us');
}
const reducer = combineReducers({

View File

@ -15,7 +15,8 @@ import React from 'react';
import { connect } from 'react-redux';
import { ConfigProvider } from '@alifd/next';
import siteConfig from '../config';
import { changeLanguage } from '../reducers/locale';
import { changeLanguage } from '@/reducers/locale';
import { aliwareIntl } from '@/globalLib';
import './index.scss';
@ -28,14 +29,19 @@ class Header extends React.Component {
static displayName = 'Header';
switchLang = () => {
const { language = 'en-US', changeLanguage } = this.props;
changeLanguage(language === 'en-US' ? 'zh-CN' : 'en-US');
const { language = 'en-us', changeLanguage } = this.props;
const currentLanguage = language === 'en-us' ? 'zh-cn' : 'en-us';
changeLanguage(currentLanguage);
aliwareIntl.changeLanguage(currentLanguage);
document.cookie = `docsite_language=${currentLanguage}`;
console.log(currentLanguage);
// window.location.reload();
};
render() {
const { locale = {}, language = 'en-US' } = this.props;
const { locale = {}, language = 'en-us' } = this.props;
const { home, docs, blog, community, languageSwitchButton } = locale;
const BASE_URL = `https://nacos.io/${language.toLocaleLowerCase()}/`;
const BASE_URL = `https://nacos.io/${language}/`;
const NAV_MENU = [
{
id: 1,

View File

@ -361,10 +361,12 @@ class MainLayout extends React.Component {
});
}
componentDidMount() {
const nav = this.props.navList || [];
const navRow = this.nacosGetNav(nav);
this.setState({ navRow }, () => this.renderNav());
componentWillReceiveProps() {
setTimeout(() => {
const nav = this.props.navList || [];
const navRow = this.nacosGetNav(nav);
this.setState({ navRow }, () => this.renderNav());
});
}
render() {

View File

@ -21,14 +21,14 @@ const enUS = Object.assign({}, fusionEnUS, I18N.enUS);
const zhCN = Object.assign({}, fusionZhCN, I18N.zhCN);
const initialState = {
language: 'en-US',
language: 'en-us',
locale: enUS,
};
const changeLanguage = lang => dispatch => {
const language = lang === 'zh-CN' ? 'zh-CN' : 'en-US';
const language = lang === 'zh-cn' ? 'zh-cn' : 'en-us';
CookieHelp.setValue(LANGUAGE_KEY, language);
dispatch({ type: LANGUAGE_SWITCH, language, locale: language === 'zh-CN' ? zhCN : enUS });
dispatch({ type: LANGUAGE_SWITCH, language, locale: language === 'zh-cn' ? zhCN : enUS });
};
export default (state = initialState, action) => {