From 5f8a298ece456cb1e893dc55d703be83e728b644 Mon Sep 17 00:00:00 2001 From: zuiidea Date: Sat, 15 Sep 2018 18:06:24 +0800 Subject: [PATCH] fix:change language after switching language login. --- src/locales/en/messages.json | 2 +- src/pages/login/index.js | 15 ++++++++++++--- src/pages/login/model.js | 4 ++-- src/utils/index.js | 19 ++++++++++--------- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/locales/en/messages.json b/src/locales/en/messages.json index 8721746..d9f7efa 100644 --- a/src/locales/en/messages.json +++ b/src/locales/en/messages.json @@ -7,4 +7,4 @@ "Sign in": "Sign in", "Switch Theme": "Switch Theme", "Username": "Username" -} \ No newline at end of file +} diff --git a/src/pages/login/index.js b/src/pages/login/index.js index 3f6c453..6964132 100644 --- a/src/pages/login/index.js +++ b/src/pages/login/index.js @@ -5,6 +5,7 @@ import { Button, Row, Form, Icon, Input } from 'antd' import router from 'umi/router' import { GlobalFooter } from 'ant-design-pro' import { Trans, withI18n } from '@lingui/react' +import { stringify } from 'qs' import config from 'utils/config' import styles from './index.less' @@ -26,7 +27,7 @@ class Login extends PureComponent { } render() { - const { loading, form, i18n } = this.props + const { loading, form, i18n, location } = this.props const { getFieldDecorator } = form const footerLinks = [ @@ -38,11 +39,19 @@ class Login extends PureComponent { }, { key: 'English', - title: router.push('/en/login')}>English, + title: ( + router.push(`/en/login${location.search}`)}> + English + + ), }, { key: 'Chinese', - title: router.push('/zh/login')}>中文, + title: ( + router.push(`/zh/login${location.search}`)}> + 中文 + + ), }, ] diff --git a/src/pages/login/model.js b/src/pages/login/model.js index 79f07a6..e7b7a54 100644 --- a/src/pages/login/model.js +++ b/src/pages/login/model.js @@ -1,4 +1,4 @@ -import { router } from 'utils' +import { router, pathMatchRegexp } from 'utils' import { loginUser } from 'api' export default { @@ -13,7 +13,7 @@ export default { if (data.success) { const { from } = locationQuery yield put({ type: 'app/query' }) - if (from && from !== '/login') { + if (!pathMatchRegexp('/login', from)) { router.push(from) } else { router.push('/dashboard') diff --git a/src/utils/index.js b/src/utils/index.js index 5ad74d1..1f9bf5d 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -71,6 +71,7 @@ export const langFromPath = curry( * @return {string} Return the queryed language. */ (languages, defaultLanguage, pathname) => { + console.log(pathname) for (const item of languages) { if (pathname.startsWith(`/${item}/`)) { return item @@ -101,15 +102,15 @@ export const deLangPrefix = curry( } )(languages) -export const addLangPrefix = curry( - /** - * Add the language prefix in pathname. - * @param {array} prefix Specify the language you need to add. - * @param {string} pathname Add the language prefix in the pathname. - * @return {string} Return the pathname after adding the language prefix. - */ - (prefix, pathname) => `/${prefix}${deLangPrefix(pathname)}` -)(langFromPath(window.location.pathname)) +/** + * Add the language prefix in pathname. + * @param {string} pathname Add the language prefix in the pathname. + * @return {string} Return the pathname after adding the language prefix. + */ +export const addLangPrefix = pathname => { + const prefix = langFromPath(window.location.pathname) + return `/${prefix}${deLangPrefix(pathname)}` +} const routerAddLangPrefix = params => { if (isString(params)) {