From 4a0ce8e3316669481b0d4eaeec09956b50bca7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BD=A6=E6=B0=91?= Date: Thu, 29 Nov 2018 18:18:28 +0800 Subject: [PATCH] fix: Language switch --- .../resources/static/console-fe/src/index.js | 4 +- .../static/console-fe/src/layouts/Header.js | 56 +++++++++---------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/console/src/main/resources/static/console-fe/src/index.js b/console/src/main/resources/static/console-fe/src/index.js index 2bd393558..ef51f5eb0 100644 --- a/console/src/main/resources/static/console-fe/src/index.js +++ b/console/src/main/resources/static/console-fe/src/index.js @@ -85,7 +85,7 @@ class App extends React.Component { this.props.changeLanguage(language); } - generateRouter() { + static generateRouter() { return ( @@ -120,7 +120,7 @@ class App extends React.Component { fullScreen {...this.state.nacosLoading} > - {this.generateRouter()} + {App.generateRouter()} ); } diff --git a/console/src/main/resources/static/console-fe/src/layouts/Header.js b/console/src/main/resources/static/console-fe/src/layouts/Header.js index 867f9ce90..916eab61c 100644 --- a/console/src/main/resources/static/console-fe/src/layouts/Header.js +++ b/console/src/main/resources/static/console-fe/src/layouts/Header.js @@ -12,10 +12,13 @@ */ import React from 'react'; -import PropTypes from 'prop-types'; +import { connect } from 'react-redux'; +import { ConfigProvider } from '@alifd/next'; import classnames from 'classnames'; import siteConfig from '../config'; import { getLink } from '../utils/nacosutil'; +import { changeLanguage } from '../reducers/locale'; + import './index.scss'; const languageSwitch = [ @@ -30,26 +33,18 @@ const languageSwitch = [ ]; const noop = () => {}; -const defaultProps = { - type: 'primary', - language: 'en-us', - onLanguageChange: noop, -}; - +@connect( + state => ({ ...state.locale }), + { changeLanguage } +) +@ConfigProvider.config class Header extends React.Component { - static propTypes = { - language: PropTypes.string, - type: PropTypes.string, - logo: PropTypes.string, - currentKey: PropTypes.string, - onLanguageChange: PropTypes.func, - }; + static displayName = 'Header'; constructor(props) { super(props); this.state = { menuBodyVisible: false, - language: props.language, }; this.switchLang = this.switchLang.bind(this); @@ -62,9 +57,8 @@ class Header extends React.Component { } switchLang() { - const language = this.state.language === 'zh-cn' ? 'en-us' : 'zh-cn'; - this.setState({ language }); - this.props.onLanguageChange(language); + const { language = 'en-US', changeLanguage } = this.props; + changeLanguage(language === 'en-US' ? 'zh-CN' : 'en-US'); } UNSAFE_componentWillReceiveProps(nextProps) { @@ -74,8 +68,10 @@ class Header extends React.Component { } render() { + const { locale = {}, language = 'en-US' } = this.props; + const { home, docs, blog, community, languageSwitchButton } = locale; const { type, logo, onLanguageChange, currentKey } = this.props; - const { menuBodyVisible, language } = this.state; + const { menuBodyVisible } = this.state; return (
- + {siteConfig.name} - {onLanguageChange !== noop ? ( + {onLanguageChange !== noop && ( {languageSwitch.find(lang => lang.value === language).text} - ) : null} + )}
    - {siteConfig[language].pageMenu.map(item => ( + {[ + [home, 'https://nacos.io/en-us/index.html'], + [docs, 'https://nacos.io/en-us/docs/quick-start.html'], + [blog, 'https://nacos.io/en-us/blog'], + [community, 'https://nacos.io/en-us/community'], + ].map(([text, link]) => (
  • - - {item.text} + + {text}
  • ))} @@ -127,5 +128,4 @@ class Header extends React.Component { } } -Header.defaultProps = defaultProps; export default Header;