Skip to content

Commit

Permalink
chore: optimize site language detect. close #972
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoidjk committed Mar 9, 2017
1 parent 4bf3347 commit e003001
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
21 changes: 20 additions & 1 deletion site/desktop/src/static/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
/* eslint-enable */
}
(function() {
function checkIfMobile() {
var ua = window.navigator.userAgent.toLowerCase();
if (ua.indexOf('android') !== -1 || ua.indexOf('iphone') !== -1) {
return true;
}
return false;
}
function checkIfCn() {
return window.navigator.language.toLowerCase() === 'zh-cn'; // wtf safari is 'zh-CN', while chrome and other is 'zh-CN'
}
if (checkIfMobile()) {
var url = location.port ? 'http://127.0.0.1:8002/' : window.location.origin + '/kitchen-sink/';
if (checkIfCn()) {
url = url + '?lang=zh-CN';
} else {
url = url + '?lang=en-US';
}
return location.href = url;
}
function isLocalStorageNameSupported() {
const testKey = 'test';
const storage = window.localStorage;
Expand Down Expand Up @@ -58,7 +77,7 @@
}
// 首页无视链接里面的语言设置 https://github.com/ant-design/ant-design/issues/4552
if (isLocalStorageNameSupported() && (pathname === '/' || pathname === '/index-cn')) {
var lang = (window.localStorage && localStorage.getItem('locale')) || navigator.language;
var lang = (window.localStorage && localStorage.getItem('locale')) || (window.navigator.language.toLowerCase() === 'zh-cn' ? 'zh-CN' : 'en-US');
if ((lang === 'zh-CN') !== isZhCN(pathname)) {
location.pathname = getLocalizedPathname(pathname, lang === 'zh-CN');
}
Expand Down
2 changes: 1 addition & 1 deletion site/desktop/src/template/Content/ComponentDoc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default class ComponentDoc extends React.Component {
const { title, subtitle, chinese, english } = meta;
const hash = `#${path}-demo-${currentIndex}`;
const mainPath = isLocalMode ? 'components' : 'kitchen-sink/components';
const search = this.context.intl.locale === 'zh-CN' ? '?lang=zh-CN' : '';
const search = this.context.intl.locale === 'zh-CN' ? '?lang=zh-CN' : '?lang=en-US';
const iframeUrl = `${protocol}//${host}/${mainPath}/${path}${search}${hash}`;

return (
Expand Down
2 changes: 2 additions & 0 deletions site/desktop/src/template/Home/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Home extends React.Component {
let iframeUrl = location.port ? 'http://localhost:8002/' : `${location.origin}/kitchen-sink/`;
if (isZhCN) {
iframeUrl = `${iframeUrl}?lang=zh-CN`;
} else {
iframeUrl = `${iframeUrl}?lang=en-US`;
}
return (
<DocumentTitle title={`Ant Design Mobile - ${this.props.intl.formatMessage({ id: 'app.home.slogan' })}`}>
Expand Down
12 changes: 0 additions & 12 deletions site/desktop/src/template/Layout/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ export default class Layout extends React.Component {
};
}

checkIfMobile = () => {
const ua = window.navigator.userAgent.toLowerCase();
if (ua.indexOf('android') !== -1 || ua.indexOf('iphone') !== -1) {
return true;
}
return false;
}
componentWillMount() {
if (this.checkIfMobile()) {
location.href = location.port ? 'http://127.0.0.1:8002/' : '/kitchen-sink/';
}
}
componentDidMount() {
if (typeof window.ga !== 'undefined') {
this.context.router.listen((loc) => {
Expand Down
4 changes: 2 additions & 2 deletions site/kitchen/src/template/KitchenSink/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const sort = (a, b) => {
export default class App extends React.Component {
constructor(props) {
super(props);
const lang = getQuery('lang');
const appLocale = lang === 'zh-CN' ? cnLocale : enLocale;
const lang = getQuery('lang') || window.navigator.language;
const appLocale = lang.toLowerCase() === 'zh-cn' ? cnLocale : enLocale;
addLocaleData(appLocale.data);
this.state = {
open: false,
Expand Down

0 comments on commit e003001

Please sign in to comment.