2020-12-07 23:11:31 +01:00
|
|
|
import Vue from 'vue'
|
|
|
|
import VueI18n from 'vue-i18n'
|
2021-04-05 20:58:54 +02:00
|
|
|
// do not preload language files as they are to weight
|
|
|
|
// import * as en from 'locales/en.json'
|
2020-12-08 15:26:56 +01:00
|
|
|
// import * as fr from 'locales/fr.json'
|
2021-04-05 20:58:54 +02:00
|
|
|
import axios from 'axios'
|
2020-12-07 23:11:31 +01:00
|
|
|
|
|
|
|
Vue.use(VueI18n)
|
|
|
|
|
2021-04-05 20:58:54 +02:00
|
|
|
// const messages = {
|
|
|
|
// en: {
|
|
|
|
// // ...en.default
|
|
|
|
// },
|
|
|
|
// fr: {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// export const i18n = new VueI18n({
|
|
|
|
// locale: 'en',
|
|
|
|
// fallbackLocale: 'en',
|
|
|
|
// messages
|
|
|
|
// })
|
|
|
|
|
|
|
|
export const i18n = new VueI18n()
|
|
|
|
|
|
|
|
// const loadedLanguages = ['en'] // our default language that is preloaded
|
|
|
|
const loadedLanguages = [] // our default language that is preloaded
|
2020-12-08 15:26:56 +01:00
|
|
|
|
|
|
|
function setI18nLanguage (lang) {
|
|
|
|
i18n.locale = lang
|
|
|
|
// axios.defaults.headers.common['Accept-Language'] = lang
|
|
|
|
// document.querySelector('html').setAttribute('lang', lang)
|
|
|
|
return lang
|
|
|
|
}
|
|
|
|
|
|
|
|
export function 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
|
|
|
|
// return import(/* webpackChunkName: "lang-[request]" */ `sites/default/files/lang/${lang}.json`).then(
|
|
|
|
return axios.get(`/sites/default/files/lang/${lang}.json`)
|
|
|
|
.then(({ data }) => {
|
|
|
|
console.log(`webpack import ${lang} messages`, data)
|
|
|
|
i18n.setLocaleMessage(lang, data)
|
|
|
|
loadedLanguages.push(lang)
|
|
|
|
return setI18nLanguage(lang)
|
|
|
|
})
|
|
|
|
}
|