changed locales initialization & add date-fns locale imports

This commit is contained in:
Axolotle 2020-08-29 14:16:59 +02:00
parent 167407e192
commit 7fa75f224c
5 changed files with 128 additions and 59 deletions

View file

@ -91,10 +91,6 @@ export default {
// This hook is only triggered at page first load // This hook is only triggered at page first load
async created () { async created () {
// Save user locales in store
const { locale, fallbackLocale } = this.$i18n
this.$store.dispatch('INIT_LOCALES', { locale, fallbackLocale })
// From this hook the value of `connected` always come from the localStorage. // From this hook the value of `connected` always come from the localStorage.
if (!this.connected) { if (!this.connected) {
// user is not connected: allow the login view to be rendered. // user is not connected: allow the login view to be rendered.

View file

@ -1,6 +1,8 @@
import i18n from '@/i18n' import i18n from '@/i18n'
import store from '@/store'
import supportedLocales from './supportedLocales' import supportedLocales from './supportedLocales'
let dateFnsLocale
const loadedLanguages = [] const loadedLanguages = []
/** /**
@ -10,9 +12,8 @@ const loadedLanguages = []
* @return {string[]} * @return {string[]}
*/ */
function getDefaultLocales () { function getDefaultLocales () {
const locale = localStorage.getItem('locale') const locale = store.getters.locale
const fallbackLocale = localStorage.getItem('fallbackLocale') const fallbackLocale = store.getters.fallbackLocale
if (locale && fallbackLocale) return [locale, fallbackLocale] if (locale && fallbackLocale) return [locale, fallbackLocale]
const navigatorLocales = navigator.languages || [navigator.language] const navigatorLocales = navigator.languages || [navigator.language]
@ -49,7 +50,7 @@ function loadLocaleMessages (locale) {
return Promise.resolve(locale) return Promise.resolve(locale)
} }
return import( return import(
/* webpackChunkName: "lang-[request]" */ `@/locales/${locale}.json` /* webpackChunkName: "lc/lang-[request]" */ `@/locales/${locale}`
).then(messages => { ).then(messages => {
i18n.setLocaleMessage(locale, messages.default) i18n.setLocaleMessage(locale, messages.default)
loadedLanguages.push(locale) loadedLanguages.push(locale)
@ -57,8 +58,35 @@ function loadLocaleMessages (locale) {
}) })
} }
export { /**
getDefaultLocales, * Loads a date-fns locale object
updateDocumentLocale, */
loadLocaleMessages async function loadDateFnsLocale (locale) {
const dateFnsLocaleName = supportedLocales[locale].dateFnsLocale || locale
return import(
/* webpackChunkName: "lc/datefns-[request]" */
`date-fns/locale/${dateFnsLocaleName}/index.js`
).then(locale => {
dateFnsLocale = locale.default
})
}
/**
* Initialize all locales
*/
function initDefaultLocales () {
// Get defined locales from `localStorage` or `navigator`
const [locale, fallbackLocale] = getDefaultLocales()
store.dispatch('UPDATE_LOCALE', locale)
store.dispatch('UPDATE_FALLBACK_LOCALE', fallbackLocale || 'en')
loadLocaleMessages('en')
}
export {
initDefaultLocales,
updateDocumentLocale,
loadLocaleMessages,
loadDateFnsLocale,
dateFnsLocale
} }

View file

@ -5,21 +5,12 @@
import Vue from 'vue' import Vue from 'vue'
import VueI18n from 'vue-i18n' import VueI18n from 'vue-i18n'
import { getDefaultLocales, loadLocaleMessages } from './helpers' import { initDefaultLocales } from './helpers'
// Plugin Initialization // Plugin Initialization
Vue.use(VueI18n) Vue.use(VueI18n)
// Get defined locales from `localStorage` or `navigator` export default new VueI18n({})
const [locale, fallbackLocale] = getDefaultLocales()
export default new VueI18n({ // Load default locales translations files and setup store data
locale, initDefaultLocales()
fallbackLocale: fallbackLocale ? [fallbackLocale, 'en'] : ['en'],
messages: {}
})
// Load default locales translations files
loadLocaleMessages(locale)
loadLocaleMessages(fallbackLocale)
loadLocaleMessages('en')

View file

@ -1,26 +1,83 @@
// date-fns locales can be found here : https://github.com/date-fns/date-fns/tree/master/src/locale
export default { export default {
ar: 'عربي', ar: {
bn_BD: 'বাংলা', name: 'عربي'
br: 'Brezhoneg', },
ca: 'Català', bn_BD: {
de: 'Deutsch', name: 'বাংলা',
el: 'Eλληνικά', dateFnsLocale: 'bn'
en: 'English', },
eo: 'Esperanto', br: {
es: 'Español', name: 'Brezhoneg',
eu: 'Euskara', dateFnsLocale: 'fr'
fr: 'Français', },
hi: 'हिन्दी', ca: {
hu: 'Magyar', name: 'Català'
it: 'Italiano', },
nb_NO: 'Norsk bokmål', de: {
ne: 'नेपाली', name: 'Deutsch'
nl: 'Nederlands', },
oc: 'Occitan', el: {
pl: 'Polski', name: 'Eλληνικά'
pt: 'Português', },
ru: 'Русский', en: {
sv: 'Svenska', name: 'English',
tr: 'Türkçe', dateFnsLocale: 'en-GB'
zh_Hans: '简化字' },
eo: {
name: 'Esperanto'
},
es: {
name: 'Español'
},
eu: {
name: 'Euskara'
},
fr: {
name: 'Français'
},
hi: {
name: 'हिन्दी'
},
hu: {
name: 'Magyar'
},
it: {
name: 'Italiano'
},
nb_NO: {
name: 'Norsk bokmål',
dateFnsLocale: 'nb'
},
ne: {
name: 'नेपाली',
dateFnsLocale: 'en-GB'
},
nl: {
name: 'Nederlands'
},
oc: {
name: 'Occitan',
dateFnsLocale: 'ca'
},
pl: {
name: 'Polski'
},
pt: {
name: 'Português'
},
ru: {
name: 'Русский'
},
sv: {
name: 'Svenska'
},
tr: {
name: 'Türkçe'
},
zh_Hans: {
name: '简化字',
dateFnsLocale: 'zh-CN'
}
} }

View file

@ -4,12 +4,12 @@
*/ */
import i18n from '@/i18n' import i18n from '@/i18n'
import { loadLocaleMessages, updateDocumentLocale } from '@/i18n/helpers' import { loadLocaleMessages, updateDocumentLocale, loadDateFnsLocale } from '@/i18n/helpers'
export default { export default {
state: { state: {
locale: undefined, locale: localStorage.getItem('locale'),
fallbackLocale: undefined fallbackLocale: localStorage.getItem('fallbackLocale')
}, },
mutations: { mutations: {
@ -27,22 +27,19 @@ export default {
actions: { actions: {
'UPDATE_LOCALE' ({ commit }, locale) { 'UPDATE_LOCALE' ({ commit }, locale) {
loadLocaleMessages(locale).then(() => { loadLocaleMessages(locale).then(() => {
i18n.locale = locale
updateDocumentLocale(locale) updateDocumentLocale(locale)
commit('SET_LOCALE', locale) commit('SET_LOCALE', locale)
i18n.locale = locale
}) })
// also query the date-fns locale object for filters
loadDateFnsLocale(locale)
}, },
'UPDATE_FALLBACK_LOCALE' ({ commit }, locale) { 'UPDATE_FALLBACK_LOCALE' ({ commit }, locale) {
loadLocaleMessages(locale).then(() => { loadLocaleMessages(locale).then(() => {
i18n.fallbackLocale = [locale, 'en']
commit('SET_FALLBACK_LOCALE', locale) commit('SET_FALLBACK_LOCALE', locale)
i18n.fallbackLocale = [locale, 'en']
}) })
},
'INIT_LOCALES' ({ commit }, { locale, fallbackLocale }) {
commit('SET_LOCALE', locale)
commit('SET_FALLBACK_LOCALE', fallbackLocale[0])
} }
}, },