2017-03-08 13:41:05 -05:00
|
|
|
/* eslint-env browser, jquery */
|
|
|
|
/* global Cookies */
|
|
|
|
|
2020-08-13 17:41:44 -04:00
|
|
|
const supported = ['en', 'zh-CN', 'zh-TW', 'fr', 'de', 'ja', 'es', 'ca', 'el', 'pt', 'it', 'tr', 'ru', 'nl', 'hr', 'pl', 'uk', 'hi', 'sv', 'eo', 'da', 'ko', 'id', 'sr', 'vi', 'ar', 'cs', 'sk']
|
|
|
|
|
|
|
|
function detectLang () {
|
|
|
|
if (Cookies.get('locale')) {
|
|
|
|
let lang = Cookies.get('locale')
|
|
|
|
if (lang === 'zh') {
|
|
|
|
lang = 'zh-TW'
|
|
|
|
}
|
|
|
|
return lang
|
|
|
|
}
|
|
|
|
const userLang = navigator.language || navigator.userLanguage
|
|
|
|
const userLangCode = userLang.split('-')[0]
|
|
|
|
if (supported.includes(userLangCode)) {
|
|
|
|
return userLangCode
|
|
|
|
} else if (supported.includes(userLang)) {
|
|
|
|
return userLang
|
2017-10-16 22:54:52 -04:00
|
|
|
}
|
2020-08-13 17:41:44 -04:00
|
|
|
return 'en'
|
2016-08-18 23:49:24 -04:00
|
|
|
}
|
|
|
|
|
2020-08-13 17:41:44 -04:00
|
|
|
const lang = detectLang()
|
|
|
|
const localeSelector = $('.ui-locale')
|
2016-10-22 23:38:17 -04:00
|
|
|
|
2020-08-13 17:41:44 -04:00
|
|
|
// the following condition is needed as the selector is only available in the intro/history page
|
|
|
|
if (localeSelector.length > 0) {
|
|
|
|
localeSelector.val(lang)
|
|
|
|
$('select.ui-locale option[value="' + lang + '"]').attr('selected', 'selected')
|
|
|
|
localeSelector.change(function () {
|
|
|
|
Cookies.set('locale', $(this).val(), {
|
|
|
|
expires: 365,
|
|
|
|
sameSite: 'strict'
|
|
|
|
})
|
|
|
|
window.location.reload()
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2020-08-13 17:41:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.moment.locale(lang)
|