【问题标题】:Use i18n tag for translating使用 i18n 标签进行翻译
【发布时间】:2019-10-14 21:34:11
【问题描述】:

我的新 Vuejs 项目有问题,我使用 vuei18n 的方式与我公司的其他项目相同,但在新项目中标签不起作用。 当我在 i18n/index.js 中禁用 silentTranslationWarn 时,我收到以下消息:

[vue-i18n] 键 'pages' 的值不是字符串! [vue-i18n] 无法翻译 keypath 'pages' 的值。使用 keypath 的值作为默认值

当我像这样将我的翻译放入 i18n/fr.js 时,我的翻译工作:

const messages = {
  pages: 'Pages',
};

但是当我想在组件中使用标签时,就像在旧项目中一样,我之前有过 vue-i18n 警告。 我在旧项目中搜索了“i18n”、“local”、“messages”等的所有出现...但我在新项目中的出现完全相同,在我的 package.json 中我有相同版本的 vue-i18n

我错过了什么吗?

【问题讨论】:

  • 您是否正确创建了 vue i18n 实例?可以举个例子吗
  • 你如何称呼 i18n?尝试在行中使用$t('messages.pages') 或在您的代码中使用this

标签: vue.js vue-i18n


【解决方案1】:

@kalidou.diagne:

在我的 i18n/index.js 中:

import Vue from 'vue';
import VueI18n from 'vue-i18n';

import en from './en';
import fr from './fr';

Vue.use(VueI18n);
const messages = { en, fr };

export default new VueI18n({
  locale: 'fr',
  silentTranslationWarn: true,
  messages,
});

在我的 main.js 中:

import i18n from './i18n';

...

const Mdm = Vue.component('app', App);

/* eslint-disable no-new */
new Mdm({
  el: '#app',
  i18n,
  router,
  store,
  components: { App },
  template: '<App/>',
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2016-12-11
    • 2020-12-20
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多