我使用VueI18n支持网站中的两种语言,基于VueJS,但现在我想在这两种语言之间切换。你能帮帮我吗?
main.js:
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import {messages} from './locales/bg_en_messages'
Vue.use(VueI18n)
const i18n = new VueI18n({
locale: 'en', // set locale
messages // set locale messages
});
const app = new Vue({
i18n,
el: '#app',
render: h => h(App),
router
})./locales/bg_en_messages.js:
export const messages = {
bg: {
labels: {
personName: 'Име на лицето'
}
},
en: {
labels: {
personName: 'Name of person'
}
}
}VueJS:
<label>{{ $t("labels.personName") }}</label>我想要放置一个下拉菜单或按钮,以便在两种语言之间进行切换。我正在查看VueI18n的文档,但是没有太多的信息,所以如果您能帮助我,我将非常感激。:)
更新文章:
我让它转换语言。现在,我还有一个关于vueI18n好桌子的问题。
我有张桌子:
<template>
<vue-good-table
:columns="columns"
:rows="items"
:paginate="true"
:lineNumbers="true">
</vue-good-table>
</template>
columns: [
{
label: 'Column1',
field: 'column1',
type: 'String',
filterable: true,
placeholder: 'Column1'
},
{
label: 'Column2',
field: 'column2',
type: 'String',
filterable: true,
placeholder: 'Column2'
},
{
label: 'Column3',
field: 'column3',
type: 'String',
filterable: true,
placeholder: 'Column3'
},
{
label: 'Column3',
field: 'column3',
type: 'String',
filterable: true,
placeholder: 'Column3'
}
]我可以以某种方式给这个{{ $t("label.column1") }加上标签吗?目前label只接受字符串,但我也需要更改列的语言。
发布于 2018-06-05 11:31:58
您可以尝试按照下面的github问题在Vue实例中创建一个getter和setter:
app.i18n = new VueI18n({
locale: 'es',
fallbackLocale: 'es',
messages
})
Object.defineProperty(Vue.prototype, '$locale', {
get: function () {
return app.i18n.locale
},
set: function (locale) {
app.i18n.locale = locale
}
})
// this part happens later
new Vue(app)在任何地方使用,像这样:
this.$locale // root Vuei18n locale
this.$locale = 'en' // set root Vuei18n locale
// or
this.$root.i18n.locale // root Vuei18n locale
this.$root.i18n.locale = 'en' // set root Vuei18n localehttps://stackoverflow.com/questions/50695325
复制相似问题