NUXT I18N:如何避免重复的翻译键
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将其与
&lt; i18n&gt;
标签一起使用,则看起来很麻烦。同时,这是通常的方法关于如何在一个较大的项目中使用I18N。
几乎如所示在这里,您也将在此处使用以下以下内容。您的代码
在您的项目中有一些
fr.json
和en.json
een.json 。然后,它可以像自动化(如果需要)之间的合并一样简单。
这是有关如何与JSON
NUXT.CONFIG.JS
/locales/fr.json
/locales/en.json
<< < < 代码>/pages/index.vue
带有实际结果
单击每个链接将正确更改有关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
While having some
fr.json
anden.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
/locales/fr.json
/locales/en.json
/pages/index.vue
With the actual result
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.