index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import Vue from 'vue'
  2. import VueI18n from 'vue-i18n'
  3. // do not preload language files as they are to weight
  4. // import * as en from 'locales/en.json'
  5. // import * as fr from 'locales/fr.json'
  6. import axios from 'axios'
  7. Vue.use(VueI18n)
  8. // const messages = {
  9. // en: {
  10. // // ...en.default
  11. // },
  12. // fr: {
  13. //
  14. // }
  15. // }
  16. // export const i18n = new VueI18n({
  17. // locale: 'en',
  18. // fallbackLocale: 'en',
  19. // messages
  20. // })
  21. export const i18n = new VueI18n()
  22. // const loadedLanguages = ['en'] // our default language that is preloaded
  23. const loadedLanguages = [] // our default language that is preloaded
  24. function setI18nLanguage (lang) {
  25. i18n.locale = lang
  26. // axios.defaults.headers.common['Accept-Language'] = lang
  27. // document.querySelector('html').setAttribute('lang', lang)
  28. return lang
  29. }
  30. export function loadLanguageAsync (lang) {
  31. // If the same language
  32. if (i18n.locale === lang) {
  33. return Promise.resolve(setI18nLanguage(lang))
  34. }
  35. // If the language was already loaded
  36. if (loadedLanguages.includes(lang)) {
  37. return Promise.resolve(setI18nLanguage(lang))
  38. }
  39. // If the language hasn't been loaded yet
  40. // return import(/* webpackChunkName: "lang-[request]" */ `sites/default/files/lang/${lang}.json`).then(
  41. return axios.get(`/sites/default/files/lang/${lang}.json`)
  42. .then(({ data }) => {
  43. console.log(`webpack import ${lang} messages`, data)
  44. i18n.setLocaleMessage(lang, data)
  45. loadedLanguages.push(lang)
  46. return setI18nLanguage(lang)
  47. })
  48. }