2017-03-08 13:41:05 -05:00
|
|
|
/* eslint-env browser, jquery */
|
|
|
|
/* global Cookies */
|
2021-04-25 18:18:08 -04:00
|
|
|
const supportedLanguages = require('../../locales/_supported.json')
|
2020-08-13 17:41:44 -04:00
|
|
|
|
|
|
|
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]
|
2021-04-25 18:18:08 -04:00
|
|
|
const supportedLanguagesList = Object.keys(supportedLanguages)
|
|
|
|
if (supportedLanguagesList.includes(userLangCode)) {
|
2020-08-13 17:41:44 -04:00
|
|
|
return userLangCode
|
2021-04-25 18:18:08 -04:00
|
|
|
} else if (supportedLanguagesList.includes(userLang)) {
|
2020-08-13 17:41:44 -04:00
|
|
|
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')
|
2021-04-25 18:18:08 -04:00
|
|
|
Object.entries(supportedLanguages).forEach(function ([isoCode, nativeName]) {
|
|
|
|
localeSelector.append(`<option value="${isoCode}">${nativeName}</option>`)
|
|
|
|
})
|
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,
|
2021-08-14 08:08:39 -04:00
|
|
|
sameSite: window.cookiePolicy,
|
|
|
|
secure: window.location.protocol === 'https:'
|
2020-08-13 17:41:44 -04:00
|
|
|
})
|
|
|
|
window.location.reload()
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2020-08-13 17:41:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.moment.locale(lang)
|