Files
gancio/plugins/i18n.js

33 lines
950 B
JavaScript
Raw Normal View History

2019-04-03 00:25:12 +02:00
import Vue from 'vue'
import VueI18n from 'vue-i18n'
2019-07-24 21:26:56 +02:00
import merge from 'lodash/merge'
2019-07-26 23:51:32 +02:00
import locales from '../locales'
2019-09-19 16:23:46 +02:00
import acceptLanguage from 'accept-language'
2019-04-03 00:25:12 +02:00
Vue.use(VueI18n)
2019-09-19 16:23:46 +02:00
export default async ({ app, store, req }) => {
2019-07-24 21:26:56 +02:00
2019-09-19 16:23:46 +02:00
if (process.server) {
const acceptedLanguages = req.headers['accept-language']
const supportedLanguages = ['en', 'it', 'es']
acceptLanguage.languages(supportedLanguages)
const lang = acceptLanguage.get(acceptedLanguages)
store.commit('setLocale', lang || 'it')
const user_locale = await app.$axios.$get('/settings/user_locale')
if (user_locale[store.state.locale]) { store.commit('setUserLocale', user_locale[store.state.locale]) }
}
if (store.state.user_locale) {
merge(locales[store.state.locale], store.state.user_locale)
2019-07-26 23:51:32 +02:00
}
2019-09-19 16:23:46 +02:00
// Set i18n instance on app
2019-04-03 00:25:12 +02:00
app.i18n = new VueI18n({
2019-06-25 01:05:38 +02:00
locale: store.state.locale,
fallbackLocale: 'it',
2019-07-26 23:51:32 +02:00
messages: locales
2019-04-03 00:25:12 +02:00
})
}