hedgedoc/public/js/locale.js
David Mehren 7b00a59661
Set secure flag for non-session cookies
This adds the secure flag to all cookies that are set
in the frontend for storing various settings.
If `SameSite=none` is set (like when embedding the instance is allowed),
 the `secure` flag is necessary to set any cookie.

Signed-off-by: David Mehren <git@herrmehren.de>
2021-08-15 00:08:31 +02:00

44 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,
secure: window.location.protocol === 'https:'
})
window.location.reload()
})
}
window.moment.locale(lang)