Dan*_*ans 3 javascript internationalization vue.js bootstrap-vue
我已经尝试在 vue.js 组件中翻译我的表头几个晚上,但它对我不起作用。可能这是因为我是 Vue.js 的新手,并且可能我忘记了一些东西,但我无法找到线索。HTML 措辞中的翻译工作正常,但一旦我想翻译脚本标记中的属性(例如数据属性),我就会收到控制台错误,无法找到某些字段。
我所做的,首先我在 main.js 中初始化了 i18n 组件
import Vue from 'vue'
import BootstrapVue from 'bootstrap-vue'
import App from './App'
import router from './router'
import axios from './api'
import VueAxios from 'vue-axios'
import VueI18n from 'vue-i18n'
Vue.use(BootstrapVue)
Vue.use(VueAxios, axios)
Vue.prototype.$axios = axios;
Vue.use(VueI18n)
// Ready translated locale messages
const messages = {
en: require('./locales/en_GB.json'),
nl: require('./locales/nl_NL.json')
}
// Create VueI18n instance with options
const i18n = new VueI18n({
locale: 'nl', // set locale
fallbackLocale: 'en',
messages // set locale messages
})
// TODO load messages async, otherwise all messages will be loaded at once: http://kazupon.github.io/vue-i18n/guide/lazy-loading.html
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
i18n,
template: '<App/>',
components: {
App
}
})
Run Code Online (Sandbox Code Playgroud)
然后,在“用户”组件的脚本标记中,我尝试翻译其中定义的表头。但是由于某种原因,我收到了控制台错误,例如TypeError: "o is undefined".
data: () => {
return {
items_data: [],
fields: [
{key: this.$i18n.t('next')}, //<-- Translate table header values
{key: 'name'},
{key: 'registered'},
{key: 'role'},
{key: 'status'}
],
currentPage: 1,
perPage: 5,
totalRows: 0
}
Run Code Online (Sandbox Code Playgroud)
请参阅下面的完整文件:
<template>
<b-row>
<b-col cols="12" xl="6">
<transition name="slide">
<b-card :header="caption">
<b-table :hover="hover" :striped="striped" :bordered="bordered" :small="small" :fixed="fixed" responsive="sm" :items="items" :fields="fields" :current-page="currentPage" :per-page="perPage" @row-clicked="rowClicked">
<template slot="id" slot-scope="data">
<strong>{{data.item.id}}</strong>
</template>
<template slot="name" slot-scope="data">
<strong>{{data.item.name}}</strong>
</template>
<template slot="status" slot-scope="data">
<b-badge :variant="getBadge(data.item.status)">{{data.item.status}}</b-badge>
</template>
</b-table>
<nav>
<b-pagination size="sm" :total-rows="5" :per-page="perPage" v-model="currentPage" :prev-text="$t('previous')" :next-text="$t('next')" hide-goto-end-buttons/>
</nav>
</b-card>
</transition>
</b-col>
</b-row>
</template>
<script>
var usersData = null;
export default {
name: 'Test Users',
props: {
caption: {
type: String,
default: 'Users 2'
},
hover: {
type: Boolean,
default: true
},
striped: {
type: Boolean,
default: true
},
bordered: {
type: Boolean,
default: false
},
small: {
type: Boolean,
default: false
},
fixed: {
type: Boolean,
default: false
}
},
data: () => {
return {
items_data: [],
fields: [
{key: this.$i18n.t('next')}, //<-- Translate table header values
{key: 'name'},
{key: 'registered'},
{key: 'role'},
{key: 'status'}
],
currentPage: 1,
perPage: 5,
totalRows: 0
}
},
mounted() {
this.axios.getAll()
.then(response => {
//this.$log.debug("Data loaded: ", response.data)
this.items_data = response.data
}).catch(error => {
//this.$log.debug(error)
this.error = "Failed to load todos"
})
},
computed: {
items: function () {
return this.items_data;
}
},
methods: {
getBadge (status) {
return status === 'Active' ? 'success'
: status === 'Inactive' ? 'secondary'
: status === 'Pending' ? 'warning'
: status === 'Banned' ? 'danger' : 'primary'
},
getRowCount (items) {
return items.length
},
userLink (id) {
return `users/${id.toString()}`
},
rowClicked (item) {
const userLink = this.userLink(item.id)
this.$router.push({path: userLink})
}
}
}
</script>
<style scoped>
.card-body >>> table > tbody > tr > td {
cursor: pointer;
}
</style>
Run Code Online (Sandbox Code Playgroud)
当有人能帮助我告诉我应该如何翻译这些类型的文本时,我将非常感激。我试图通过谷歌找到我的解决方案,但根据谷歌的说法,这或多或少是它应该如何工作的。
根据文档:
fields 属性用于自定义表格列标题以及数据列的显示顺序。字段对象键用于从每个项目行中提取值...
这意味着在您的fields属性中,键的值需要与items键匹配。
例如,first_name:
fields: [
{ key: 'first_name'}
],
items: [
{ first_name: 'John' },
{ first_name: 'Jane' }
]
Run Code Online (Sandbox Code Playgroud)
如果您想自定义标题(例如翻译标题),您可以使用label:
fields: {
{
next: { label: this.$i18n.t('next') },
name: { label: this.$i18n.t('name') },
registered: { label: this.$i18n.t('registered') },
role: { label: this.$i18n.t('role') },
status: { label: this.$i18n.t('status') }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5965 次 |
| 最近记录: |