mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 09:46:30 -05:00
da35e73346
Until now client-side translations were only possible in the context of the intro/history page, because the locale-detection logic relied on the language selector as a source of available languages. The editor of course has no such selector. With this commit, I copied the list of available languages from the i18n-initialization (server-side) to support language detection in the editor too. Signed-off-by: Erik Michelson <github@erik.michelson.eu>
40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
/* eslint-env browser, jquery */
|
|
/* global Cookies */
|
|
|
|
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
|
|
}
|
|
return 'en'
|
|
}
|
|
|
|
const lang = detectLang()
|
|
const localeSelector = $('.ui-locale')
|
|
|
|
// 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()
|
|
})
|
|
}
|
|
|
|
window.moment.locale(lang)
|