Nuxt/examples/i18n/store/lang.js
Alexandre Chopin b85de3151d example i18n
2017-02-08 20:13:14 +01:00

28 lines
608 B
JavaScript

import axios from '~plugins/axios'
export const state = {
locales: ['en', 'fr'], // available langages
lang: null, // current lang
_: {} // store for translations
}
export const mutations = {
SET_LANG (state, lang) {
state.lang = lang
},
SET_TRANSLATION (state, translation) {
state._[state.lang] = translation
}
}
export const actions = {
async setLang ({ state, commit }, lang) {
if (state._[lang]) {
return commit('SET_LANG', lang)
}
let res = await axios.get(`/locales/${lang}.json`)
commit('SET_LANG', lang)
commit('SET_TRANSLATION', res.data)
}
}