mirror of
https://github.com/youzan/vant.git
synced 2025-10-20 10:44:59 +00:00
[Build] revert site to webpack config (#3747)
This commit is contained in:
45
docs/site/utils/lang.js
Normal file
45
docs/site/utils/lang.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import Locale from '../../../src/locale';
|
||||
import zhCN from '../../../src/locale/lang/zh-CN';
|
||||
import enUS from '../../../src/locale/lang/en-US';
|
||||
|
||||
const langMap = {
|
||||
'en-US': {
|
||||
title: 'Vant - Mobile UI Components built on Vue',
|
||||
messages: enUS
|
||||
},
|
||||
'zh-CN': {
|
||||
title: 'Vant - 轻量、可靠的移动端 Vue 组件库',
|
||||
messages: zhCN
|
||||
}
|
||||
};
|
||||
|
||||
let currentLang = '';
|
||||
|
||||
function getDefaultLang() {
|
||||
const langs = Object.keys(langMap);
|
||||
const { hash } = location;
|
||||
|
||||
for (let i = 0; i < langs.length; i++) {
|
||||
if (hash.indexOf(langs[i]) !== -1) {
|
||||
return langs[i];
|
||||
}
|
||||
}
|
||||
|
||||
const userLang = localStorage.getItem('VANT_LANGUAGE') || navigator.language || 'en-US';
|
||||
return userLang.indexOf('zh-') !== -1 ? 'zh-CN' : 'en-US';
|
||||
}
|
||||
|
||||
export function setLang(lang) {
|
||||
if (currentLang === lang) {
|
||||
return;
|
||||
}
|
||||
|
||||
currentLang = lang;
|
||||
if (window.localStorage) {
|
||||
localStorage.setItem('VANT_LANGUAGE', lang);
|
||||
}
|
||||
Locale.use(lang, langMap[lang].messages);
|
||||
document.title = langMap[lang].title;
|
||||
}
|
||||
|
||||
setLang(getDefaultLang());
|
Reference in New Issue
Block a user