blob: 1926c9dc7733d6853d7c365ce18001327228cb0c [file] [log] [blame]
import React from 'react';
import { autobind } from 'core-decorators';
import cookie from 'js-cookie';
export default class Language extends React.Component {
@autobind
onLanguageChange(language) {
const pathname = window.location.pathname;
let oldLang;
if (language === 'zh-cn') {
oldLang = 'en-us';
} else {
oldLang = 'zh-cn';
}
const newPathname = pathname.replace(`${window.rootPath}/${oldLang}`, `${window.rootPath}/${language}`);
cookie.set('docsite_language', language, { expires: 365, path: '' });
window.location = newPathname;
}
getLanguage() {
let urlLang;
if (window.rootPath) {
urlLang = window.location.pathname.split('/')[2];
} else {
urlLang = window.location.pathname.split('/')[1];
}
let language = this.props.lang || urlLang || cookie.get('docsite_language') || siteConfig.defaultLanguage;
// 防止链接被更改导致错误的cookie存储
if (language !== 'en-us' && language !== 'zh-cn') {
language = siteConfig.defaultLanguage;
}
// 同步cookie语言版本
if (language !== cookie.get('docsite_language')) {
cookie.set('docsite_language', language, { expires: 365, path: '' });
}
return language;
}
}