diff options
author | David Mehren | 2020-08-19 19:36:04 +0200 |
---|---|---|
committer | GitHub | 2020-08-19 19:36:04 +0200 |
commit | 8cf41ee669e30d743b2f3cd2056aff10c6778a77 (patch) | |
tree | 9a09cf42831c9adda5c99da5aa5d33c0b515ccc9 /public/js/locale.js | |
parent | 60f5b814819efc818fc9900b1ce476bbc18d5617 (diff) | |
parent | 062facb23295ed706e7339157dc27dc1f4a6f971 (diff) |
Merge pull request #468 from codimd/fix/moment-js-locale
[1.x] Fix: Set moment.js locale to users' locale (#275)
Diffstat (limited to '')
-rw-r--r-- | public/js/locale.js | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/public/js/locale.js b/public/js/locale.js index 670370d4..aca35b98 100644 --- a/public/js/locale.js +++ b/public/js/locale.js @@ -1,32 +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) -$('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) |