From ce469b1e2d66a5f598d316e0836ea361a4ccc61a Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Thu, 13 Aug 2020 15:08:27 +0200 Subject: Fixed setting moment.js locale to user-defined language Signed-off-by: Erik Michelson --- public/js/locale.js | 1 + 1 file changed, 1 insertion(+) (limited to 'public/js/locale.js') diff --git a/public/js/locale.js b/public/js/locale.js index 670370d4..05c7b3e3 100644 --- a/public/js/locale.js +++ b/public/js/locale.js @@ -21,6 +21,7 @@ if (Cookies.get('locale')) { } locale.val(lang) +window.moment.locale(lang) $('select.ui-locale option[value="' + lang + '"]').attr('selected', 'selected') locale.change(function () { -- cgit v1.2.3 From da35e73346eba908071a7a5c5831a3d280769b9c Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Thu, 13 Aug 2020 23:41:44 +0200 Subject: Restructured locale.js to be included into the editor's js bundle 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 --- public/js/locale.js | 57 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 25 deletions(-) (limited to 'public/js/locale.js') diff --git a/public/js/locale.js b/public/js/locale.js index 05c7b3e3..aca35b98 100644 --- a/public/js/locale.js +++ b/public/js/locale.js @@ -1,33 +1,40 @@ /* eslint-env browser, jquery */ /* global Cookies */ -var lang = 'en' -var userLang = navigator.language || navigator.userLanguage -var userLangCode = userLang.split('-')[0] -var locale = $('.ui-locale') -var supportLangs = [] -$('.ui-locale option').each(function () { - supportLangs.push($(this).val()) -}) -if (Cookies.get('locale')) { - lang = Cookies.get('locale') - if (lang === 'zh') { - lang = 'zh-TW' +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 } -} else if (supportLangs.indexOf(userLang) !== -1) { - lang = supportLangs[supportLangs.indexOf(userLang)] -} else if (supportLangs.indexOf(userLangCode) !== -1) { - lang = supportLangs[supportLangs.indexOf(userLangCode)] + return 'en' } -locale.val(lang) -window.moment.locale(lang) -$('select.ui-locale option[value="' + lang + '"]').attr('selected', 'selected') +const lang = detectLang() +const localeSelector = $('.ui-locale') -locale.change(function () { - Cookies.set('locale', $(this).val(), { - expires: 365, - sameSite: 'strict' +// 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.location.reload() -}) +} + +window.moment.locale(lang) -- cgit v1.2.3