【问题标题】:Translate meta tags in vue-router with i18n使用 i18n 翻译 vue-router 中的元标记
【发布时间】:2019-05-13 22:37:21
【问题描述】:

我不知道如何在 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.js localization vue-router vue-i18n


    【解决方案1】:

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

    {
    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"]
        }
    }
    

    }

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

    【讨论】:

      【解决方案2】:

      对我来说,我使用这个解决方案。

      src/router/index.js

      import i18n from '@/i18n';
      
      const DEFAULT_TITLE = 'DEFAULT_TITLE';
      router.afterEach((to, from) => {
          Vue.nextTick(() => {
              document.title = i18n.t(to.meta.title || DEFAULT_TITLE);
          });
      });
      

      你可以直接在 meta 中使用i18n.t('Text to translate')

      这是我在src/i18n/index.js中的代码。

      import Vue from 'vue';
      import VueI18n from 'vue-i18n';
      
      Vue.prototype.isRtl = localStorage.getItem('lang') === 'ar';
      
      Vue.use(VueI18n);
      
      function loadLocaleMessages() {
          const locales = require.context('./locales', true, /[A-Za-z0-9-_,\s]+\.json$/i);
          const messages = {};
          locales.keys().forEach(key => {
              const matched = key.match(/([A-Za-z0-9-_]+)\./i);
              if (matched && matched.length > 1) {
                  const locale = matched[1];
                  messages[locale] = locales(key);
              }
          });
          return messages;
      }
      
      export default new VueI18n({
          locale: localStorage.getItem('lang') || 'ar',
          fallbackLocale: localStorage.getItem('lang') || 'ar',
          messages: loadLocaleMessages(),
      });
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2020-05-06
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      相关资源
      最近更新 更多