NUXT I18N:如何避免重复的翻译键

发布于 2025-01-29 20:58:16 字数 1142 浏览 3 评论 0原文

我正在使用nuxt,nuxt i18n和组件翻译:

<template>
  <div>{{ $t("Hello world") }}</div>
</template>

<i18n lang="yaml">
fr:
  Hello world: Bonjour le monde
</i18n>

我的问题是,该代码在英语中“不起作用”和显示“ bonjour le monde”,因为我希望以英语找到翻译,而我希望它留下来”世界”。为了使它起作用,我必须将其重新重新列出:

<template>
  <div>{{ $t("Hello world") }}</div>
</template>

<i18n lang="yaml">
fr:
  Hello world: Bonjour le monde
en:
  Hello world: Hello world
</i18n>

因此,仅翻译我的文本1次似乎太多了。

我很好地看了一下文档,但也许我缺少一些东西:(这是我的I18N配置:

modules: [
  '@nuxtjs/i18n',
  ...
],
i18n: {
  vueI18nLoader: true,
  langDir: 'locales/',
  lazy: true,
  loadLanguagesAsync: true,
  locales: [
    {
      code: "en",
      name: "English",
      iso: 'en-US',
      file: 'en.js'
    },
    {
      code: "fr",
      name: "Français",
      iso: 'fr-FR',
      file: 'fr.js'
    },
  ],
  defaultLocale: "en",
  strategy: 'prefix',
  parsePages: false
}

I'm using Nuxt, Nuxt i18n and in component translations :

<template>
  <div>{{ $t("Hello world") }}</div>
</template>

<i18n lang="yaml">
fr:
  Hello world: Bonjour le monde
</i18n>

My problem is that this code "doesn't work" in english and display "Bonjour le monde" because no translation is found in english while I would like it to stay "Hello world". To make it work, I have to redeclare it as so :

<template>
  <div>{{ $t("Hello world") }}</div>
</template>

<i18n lang="yaml">
fr:
  Hello world: Bonjour le monde
en:
  Hello world: Hello world
</i18n>

So it seems like too much effort to translate my texte only 1 time.

I took a good look at the doc but maybe I am missing something :( Here is my i18n configuration :

modules: [
  '@nuxtjs/i18n',
  ...
],
i18n: {
  vueI18nLoader: true,
  langDir: 'locales/',
  lazy: true,
  loadLanguagesAsync: true,
  locales: [
    {
      code: "en",
      name: "English",
      iso: 'en-US',
      file: 'en.js'
    },
    {
      code: "fr",
      name: "Français",
      iso: 'fr-FR',
      file: 'fr.js'
    },
  ],
  defaultLocale: "en",
  strategy: 'prefix',
  parsePages: false
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2025-02-05 20:58:16

如果您将其与&lt; i18n&gt;标签一起使用,则看起来很麻烦。

同时,这是通常的方法关于如何在一个较大的项目中使用I18N。

几乎如所示在这里,您也将在此处使用以下以下内容。您的代码

<div id="app">
  <p>{{ $t("message.hello") }}</p>
</div>

在您的项目中有一些fr.jsonen.json een.json 。

然后,它可以像自动化(如果需要)之间的合并一样简单。


这是有关如何与JSON

NUXT.CONFIG.JS

modules: [
  '@nuxtjs/i18n',
],
i18n: {
  defaultLocale: 'en',
  langDir: 'locales/',
  lazy: true,
  loadLanguagesAsync: true,
  locales: [
    {
      code: "en",
      iso: 'en',
      file: 'en.json'
    },
    {
      code: "fr",
      iso: 'fr',
      file: 'fr.json'
    },
  ],
  vueI18n: {
    fallbackLocale: 'en',
  }
},

/locales/fr.json

{
  "helloWorld": "Salut le monde"
}

/locales/en.json <

{
  "helloWorld": "hello world"
}

< < < 代码>/pages/index.vue

<template>
  <div>
    <nuxt-link :to="switchLocalePath('en')">Change to English</nuxt-link>
    <nuxt-link :to="switchLocalePath('fr')">Passer en Francais</nuxt-link>
    <p>here is the i18n string {{ $t('helloWorld') }}</p>
  </div>
</template>

带有实际结果

“在此处输入图像说明”

单击每个链接将正确更改有关JSON文件中的语言环境。


这是 github repo 如果您想要一个工作示例。

This looks quite cumbersome if you use it with the <i18n> tag as shown in your question.

Meanwhile, this is the usual approach regarding how to use i18n across a bigger project.

Pretty much as shown here too, you will be using the following in your code

<div id="app">
  <p>{{ $t("message.hello") }}</p>
</div>

While having some fr.json and en.json locales in your project.

Then, it can be as simple as automating a merge between your 2 JSON files if needed.


Here is a basic configuration on how to work with JSON

nuxt.config.js

modules: [
  '@nuxtjs/i18n',
],
i18n: {
  defaultLocale: 'en',
  langDir: 'locales/',
  lazy: true,
  loadLanguagesAsync: true,
  locales: [
    {
      code: "en",
      iso: 'en',
      file: 'en.json'
    },
    {
      code: "fr",
      iso: 'fr',
      file: 'fr.json'
    },
  ],
  vueI18n: {
    fallbackLocale: 'en',
  }
},

/locales/fr.json

{
  "helloWorld": "Salut le monde"
}

/locales/en.json

{
  "helloWorld": "hello world"
}

/pages/index.vue

<template>
  <div>
    <nuxt-link :to="switchLocalePath('en')">Change to English</nuxt-link>
    <nuxt-link :to="switchLocalePath('fr')">Passer en Francais</nuxt-link>
    <p>here is the i18n string {{ $t('helloWorld') }}</p>
  </div>
</template>

With the actual result

enter image description here

Clicking on each link will properly change the locale regarding the one in the JSON file.


Here is a Github repo if you want a working example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文