问题
Using https://github.com/kazupon/vue-i18n for localization
Vue.t() || $t() || trans() receive a string which is a key to be translated by vue-i18n
Hey guys! I'am trying the following code:
import Vue from 'vue'
export default {
task: {
status: [
{ id: 'pending', name: Vue.t('pending') },
{ id: 'done', name: 'Done' }
]
}
}
That is my state.js which is the state of my VUEX! When I try to use the Vue.t function I have the following error:
Uncaught TypeError: _vue2.default.t is not a function
My solution (which I don't think it's the best one also good for performance)
Let my state.js like that:
import Vue from 'vue'
export default {
task: {
status: [
{ id: 'pending', name: 'pending' },
{ id: 'done', name: 'done' }
]
}
}
And I've done the following getters (vuex getter)
import { map } from 'lodash'
import { trans } from 'utils/helpers/translation' // helper for Vue.t(string)
export const getTaskStatus = ({ task }) => map(task.status, (obj) => {
return { id: obj.id, name: trans(obj.name) }
})
Anyone knows how is the best way to make it work?
回答1:
Take a look at vuex-i18n library, it does what you are trying to do out of the box.
回答2:
1- main.js
import Vue from 'vue';
import Vuex from 'vuex';
import App from './App';
import { store } from './store'
new Vue({
el: '#app',
store,
render: h => h(App)
})
2- App.vue: here I used buttons to change languages:
<template>
<div id="app">
<img src="./assets/logo.png" />
<div>
<div>
<h1>{{ "index.title" | translate }}</h1>
<p>{{ $t("index.content", {"type": "nice"}) }}</p>
</div>
<div>
<h1>{{ "home.title" | translate }}</h1>
<p>{{ $t("home.content", {"type": "nice"}) }}</p>
</div>
<div>
<h1>{{ "product.title" | translate }}</h1>
<p>{{ $t("product.content", {"type": "nice"}) }}</p>
<p>{{ $t("product.unit", {"type": "nice"}) }}</p>
</div>
<button @click="setLang('fa')">پارسی</button>
<button @click="setLang('en')">English</button>
<button @click="setLang('ar')">العربیه</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
posts: [],
errors: []
};
},
methods: {
setLang(lang) {
this.$store.dispatch("changeCulture", lang);
}
},
created() {
this.$store.dispatch("changeCulture", "en");
}
};
</script>
3-store > index.js: here I tried to use sore, mutation and actions to change local:
import Vue from 'vue';
import Vuex from 'vuex';
import vuexI18n from 'vuex-i18n';
import translationsEn from '../locals/en.json';
import translationsFa from '../locals/fa.json';
Vue.use(Vuex);
export const store = new Vuex.Store({
state: {
fallback: 'en',
locale: ''
},
mutations: {
setCulture(state, payload) {
state.locale = payload
Vue.i18n.set(state.locale);
}
},
actions: {
changeCulture({ commit }, payload, adad) {
commit('setCulture', payload)
}
},
getters: {
language(state) {
return state.locale
}
}
})
Vue.use(vuexI18n.plugin, store);
Vue.i18n.add('en', translationsEn);
Vue.i18n.add('fa', translationsFa);
Vue.i18n.set('fa')
4- en.json / fa.json : some may ask for it:
{
"index": {
"title": "Hello",
"content": "This is some {type} content"
},
"home": {
"title": "About us",
"content": "Node developing team"
},
"product": {
"title": "Product",
"content": "Select one product",
"unit": "unit"
}
}
... 4- fa.json is :
{
"index": {
"title": "سلام",
"content": "به دنیای کد خوش آمدید"
},
"home": {
"title": "درباره ما",
"content": "تیم برنامه نویسی نود"
},
"product": {
"title": "محصولات",
"content": "یک محصول را انتخاب کنید",
"unit": "واحد شمارش"
}
}
And it works like a charm.
来源:https://stackoverflow.com/questions/41557351/using-kazupon-vue-i18n-inside-a-state-of-vuex