summaryrefslogtreecommitdiff
path: root/public/js/locale.js
blob: aca35b98f6b80f8cd10ca7f7c7934a6f9133058f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* 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)