mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-12-01 13:31:04 -05:00
0d943d1284
Signed-off-by: Erik Michelson <github@erik.michelson.eu>
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
/* eslint-env browser, jquery */
|
|
/* global Cookies */
|
|
const supportedLanguages = require('../../locales/_supported.json')
|
|
|
|
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]
|
|
const supportedLanguagesList = Object.keys(supportedLanguages)
|
|
if (supportedLanguagesList.includes(userLangCode)) {
|
|
return userLangCode
|
|
} else if (supportedLanguagesList.includes(userLang)) {
|
|
return userLang
|
|
}
|
|
return 'en'
|
|
}
|
|
|
|
const lang = detectLang()
|
|
const localeSelector = $('.ui-locale')
|
|
Object.entries(supportedLanguages).forEach(function ([isoCode, nativeName]) {
|
|
localeSelector.append(`<option value="${isoCode}">${nativeName}</option>`)
|
|
})
|
|
|
|
// 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: window.cookiePolicy
|
|
})
|
|
window.location.reload()
|
|
})
|
|
}
|
|
|
|
window.moment.locale(lang)
|