Skip to content
Permalink
Browse files

英转中,url替换(带hash、query)

英转中URL问题,上一版使用了location.origin + location.pathname,会丢失query和hash。此版本原理还是用location.href做replace,鉴于首页pathname可能是/会在替换中匹配到https:后的第一个/,所以先把href的protocol部分提取出来,再做replace,最后再拼回去做跳转。
  • Loading branch information...
YuyingWu committed Feb 26, 2017
1 parent 0c6da21 commit 9c9c164617ec35d9c30e5518f94661601bcd736b
Showing with 4 additions and 1 deletion.
  1. +4 −1 site/theme/template/Layout/Header.jsx
@@ -84,10 +84,13 @@ export default class Header extends React.Component {

handleLangChange = () => {
const pathname = this.props.location.pathname;
const currentProtocol = location.protocol + '//';
const currentHref = location.href.substr(currentProtocol.length);

if (utils.isLocalStorageNameSupported()) {
localStorage.setItem('locale', utils.isZhCN(pathname) ? 'en-US' : 'zh-CN');
}
location.href = location.origin + location.pathname.replace(
location.href = currentProtocol + currentHref.replace(
location.pathname,
utils.getLocalizedPathname(pathname, !utils.isZhCN(pathname)),
);

0 comments on commit 9c9c164

Please sign in to comment.
You can’t perform that action at this time.