首页 > 解决方案 > 使用 i18n 翻译 vue-router 中的元标记

问题描述

我不知道如何在 vue.router 中使用 i18n 来翻译用于我的面包屑的元标记

main.js

import vuexI18n from 'vuex-i18n';
Vue.use(vuexI18n.plugin, store);
const i18n = Vue.i18n
import es_es from '@/assets/langs/es_es';
import en_us from '@/assets/langs/en_us';
i18n.add('es_es', es_es)
i18n.add('en_us', en_us)
i18n.set('en_us')

路由器.js

import vuexI18n from 'vuex-i18n'
Vue.use(Router)
Vue.use(vuexI18n.plugin, store)
const i18n = Vue.i18n

尝试翻译的路线的关键

{
    path: '/clientes/nuevo',
    name: 'CustomersNew',
    beforeEnter: (to, from, next) => { 
         Auth(to, from, next); 
         CheckPermission(to, from, next, "customers@add_customer");
    },
    component: CustomersNew,
    meta: {
        breadcrumb: {
            title: Vue.i18n.translate('customers.title'),
            links: [ "Customers", "new"]
        }
    }
}

任何模板中使用的任何翻译都可以很好地翻译。

标签: vue.jslocalizationvue-routervue-i18n

解决方案


您可以将密钥作为字符串传递并直接在面包屑组件中进行翻译,而不是尝试直接在路由器上进行翻译

{
path: '/clientes/nuevo',
name: 'CustomersNew',
beforeEnter: (to, from, next) => { 
     Auth(to, from, next); 
     CheckPermission(to, from, next, "customers@add_customer");
},
component: CustomersNew,
meta: {
    breadcrumb: {
        title: "key.to.translate.as.string',
        links: [ "Customers", "new"]
    }
}

}

在组件中,一如既往地翻译。


推荐阅读