使用jQuery设置语言首选项

时间:2017-11-20 07:49:24

标签: jquery

我试图为网页设置语言首选项,但似乎无法获得。发生的事情是,一旦页面加载它转到localhost / en / en,它就会重复,我不知道为什么。这是我的代码:

languageOptions = ['en', 'zh-cn', 'zh-hk', 'jp-jp', 'ko-kr', 'th-th'];
languageDictionary = {
  'en': 'en',
  'en-gb': 'en',
  'en-us': 'en',
  'zh': 'cn',
} 



function checkLanguage() {
    setTimeout(() => {

let lang = localStorage.getItem('lang');
if (this.changeLanguage(lang)) {
    window.location.href = lang;
} else {
    window.location.href = 'en';
}

let browserLang = navigator.language.toLocaleLowerCase();

if(this.languageDictionary[browserLang]) {
    window.location.href = browserLang;
} else {
    window.location.href = 'en';
}

let urlLang = window.location.pathname.replace('/', '');

if (this.languageOptions.indexOf(urlLang) !== -1) {
    window.location.href = urlLang;
} else {
    window.location.href = 'en';
}
})

};

checkLanguage();

function changeLanguage(lang) {
    console.log(lang);
    // add to storage
    window.location.href = '' + lang;
    localStorage.setItem('lang', lang)
}

我的代码中是否有导致此问题的内容?

0 个答案:

没有答案