2021-06-24 01:24:57 +02:00
|
|
|
import Vue from 'vue'
|
|
|
|
import VueI18n from 'vue-i18n'
|
2021-07-25 15:27:15 +02:00
|
|
|
import langEN from './lang/en.json'
|
2021-06-24 01:24:57 +02:00
|
|
|
|
|
|
|
Vue.use(VueI18n)
|
|
|
|
|
|
|
|
export const i18n = new VueI18n({
|
|
|
|
locale: 'en', // set locale
|
|
|
|
fallbackLocale: 'en',
|
|
|
|
messages: {
|
2021-07-25 15:27:15 +02:00
|
|
|
en: langEN,
|
2021-06-24 01:24:57 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export const availableLanguages = {
|
|
|
|
en: 'English',
|
2021-09-05 13:31:53 +02:00
|
|
|
'de-DE': 'Deutsch',
|
|
|
|
'de-swiss': 'Schwizertütsch',
|
|
|
|
'ru-RU': 'Русский',
|
|
|
|
'fr-FR': 'Français',
|
2021-06-24 01:24:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const loadedLanguages = ['en'] // our default language that is preloaded
|
|
|
|
|
|
|
|
const setI18nLanguage = lang => {
|
|
|
|
i18n.locale = lang
|
|
|
|
document.querySelector('html').setAttribute('lang', lang)
|
|
|
|
return lang
|
|
|
|
}
|
|
|
|
|
|
|
|
export const loadLanguageAsync = lang => {
|
|
|
|
// If the same language
|
|
|
|
if (i18n.locale === lang) {
|
|
|
|
return Promise.resolve(setI18nLanguage(lang))
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the language was already loaded
|
|
|
|
if (loadedLanguages.includes(lang)) {
|
|
|
|
return Promise.resolve(setI18nLanguage(lang))
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the language hasn't been loaded yet
|
2021-07-25 15:27:15 +02:00
|
|
|
return import(`./lang/${lang}.json`).then(
|
2021-06-24 01:24:57 +02:00
|
|
|
messages => {
|
|
|
|
i18n.setLocaleMessage(lang, messages.default)
|
|
|
|
loadedLanguages.push(lang)
|
|
|
|
return setI18nLanguage(lang)
|
2021-07-19 11:18:22 +02:00
|
|
|
},
|
2021-06-24 01:24:57 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getCurrentLanguage = () => {
|
|
|
|
const savedLanguage = localStorage.getItem('language')
|
2021-09-05 13:31:53 +02:00
|
|
|
if (savedLanguage !== null) {
|
2021-06-24 01:24:57 +02:00
|
|
|
return savedLanguage
|
|
|
|
}
|
|
|
|
|
|
|
|
let browserLanguage = navigator.language || navigator.userLanguage
|
|
|
|
|
2021-09-05 13:31:53 +02:00
|
|
|
for (let k in availableLanguages) {
|
|
|
|
if (browserLanguage[k] === browserLanguage || k.startsWith(browserLanguage + '-')) {
|
|
|
|
return k
|
|
|
|
}
|
2021-06-24 01:24:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 'en'
|
|
|
|
}
|
|
|
|
|
|
|
|
export const saveLanguage = lang => {
|
|
|
|
localStorage.setItem('language', lang)
|
|
|
|
setLanguage()
|
|
|
|
}
|
|
|
|
|
|
|
|
export const setLanguage = () => {
|
|
|
|
loadLanguageAsync(getCurrentLanguage())
|
|
|
|
}
|