简体   繁体   English

Vue i18n:复数化和链接消息未在生产中解释

[英]Vue i18n : Pluralization and Linked Messages not interpreted in production

I've managed to integrate nicely the vue-i18n library on my development environment and it works perfectly.我已经成功地将vue-i18n库很好地集成到我的开发环境中,并且它运行良好。

However, when I deploy the project on Heroku, every message using vue-i18n Pluralization or Linked Locale Messages is not being interpreted and shows the entire locale message string:但是,当我在 Heroku 上部署项目时,使用 vue-i18n PluralizationLinked Locale Messages的每条消息都不会被解释并显示整个语言环境消息字符串:

I've opened a discussion on the repo but no reply so far...我已经对回购进行了讨论,但到目前为止还没有回复......

I am using vue-i18n (9.2.2) in a Nuxt3 plugin, therefore using VueJS 3 , building with Vite with the plugin for vue-i18n vite-plugin-vue-i18n .我在Nuxt3插件中使用 vue-i18n (9.2.2),因此使用VueJS 3 ,使用Vite和 vue-i18n vite-plugin-vue-i18n 插件构建

Nuxt3 configuration (nuxt.config.ts) , showing the Vite plugin config to resolve the locales JSON files: Nuxt3 配置 (nuxt.config.ts) ,显示 Vite 插件配置以解析语言环境 JSON 文件:

import { defineNuxtConfig } from 'nuxt'
import { resolve, dirname } from 'node:path'
import { fileURLToPath } from 'url'
import VueI18nVitePlugin from '@intlify/unplugin-vue-i18n/vite'

export default defineNuxtConfig({
  ...
  vite: {
    plugins: [
      VueI18nVitePlugin({
        include: [
          resolve(dirname(fileURLToPath(import.meta.url)), './locales/*.json')
        ]
      })
    ]
  },
 ...
});

Nuxt plugin for vue-i18n (plugins/vue-i18n.ts):用于 vue-i18n 的 Nuxt 插件(plugins/vue-i18n.ts):

import { createI18n } from 'vue-i18n'
import fr from '~/locales/fr.json'
import en from '~/locales/en.json'

// DOC I18N : https://www.i18next.com/translation-function/essentials
export default defineNuxtPlugin(({ vueApp }) => {
  const i18n = createI18n({
    legacy: false,
    allowComposition: true,
    globalInjection: true,
    locale: 'fr',
    fallbackLocale: 'en',
    messages: { 
      'en': en, 
      'fr': fr, 
    }
  });
  vueApp.use(i18n);
})

And here are the 2 locales files:这是 2 个语言环境文件:

locales/fr.json:语言环境/fr.json:

{
  "test": "Zero test | Un test | {n} tests",
  "testnested": {
    "nestedvalue": "Zero sous-test | Un sous-test | {n} sous-tests"
  },
  ...

locales/en.json:语言环境/en.json:

  "test": "Zero test | One test | {n} tests",
  "testnested": {
    "nestedvalue": "Zero sub-test | Un sub-test | {n} sub-tests"
  },
  ...
}

Example of how the locale messages are used within the Vue templates:在 Vue 模板中如何使用语言环境消息的示例:

    <p>
      -- TEST ROOT --<br />
      {{ $t('test', 0) }}<br />
      {{ $t('test', 1) }}<br />
      {{ $t('test', 2) }}<br />
      <br />
      -- TEST NESTED --<br />
      {{ $t('testnested.nestedvalue', 0) }}<br />
      {{ $t('testnested.nestedvalue', 1) }}<br />
      {{ $t('testnested.nestedvalue', 2) }}<br />
    </p>

The Plurals and Linked messages syntax in the locales messages should be interpreted by vue-i18n and not display the full locale message string.语言环境消息中的复数和链接消息语法应该由 vue-i18n 解释,而不是显示完整的语言环境消息字符串。

[EXPECTED] Local environment output: [预期] 本地环境 output:

预期结果(在本地开发上运行)

[BUG RESULT] Same code packaged for production, deployed on Heroku: [BUG 结果] 为生产打包的相同代码,部署在 Heroku 上:

打包和部署后显示的错误结果

Another example with failing Linked Messages (in production):另一个链接消息失败的例子(在生产中):

其他示例

Here are some infos about my system:以下是有关我的系统的一些信息:

  System:
    OS: Windows 10 10.0.22000
    CPU: (8) x64 Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz
    Memory: 16.35 GB / 31.59 GB
  Binaries:
    Node: 16.13.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), 
    Chromium (105.0.1343.27)
    Internet Explorer: 11.0.22000.120
  npmPackages:
    @intlify/unplugin-vue-i18n: ^0.5.0 => 0.5.0
    vitest: ^0.20.3 => 0.20.3
    vue-i18n: ^9.2.2 => 9.2.2
    vue-toastification: next => 2.0.0-rc.5

I have read on other threads and on StackOverflow here but the accepted answer doesn't seem to be up-to-date, and believe there could be issues with the way webpack or vite are packaging the project.我在这里阅读了其他线程和 StackOverflow 上的内容,但接受的答案似乎不是最新的,并且相信 webpack 或 vite 打包项目的方式可能存在问题。

What am I doing wrong here?我在这里做错了什么?

If nothing seems wrong, would you recommend I open a bug report on vue-i18n repo?如果没有任何问题,你会建议我在 vue-i18n repo 上打开一个错误报告吗? Or on the @intlify/vite-plugin-vue-i18n repository?或者在@intlify/vite-plugin-vue-i18n 存储库上?

I had the same problem.我有同样的问题。

Think this is an issue with the vite-plugin-vue-i18n pre-compile which converts messages to JS functions.认为这是将消息转换为 JS 函数的 vite-plugin-vue-i18n 预编译的问题。 I set runtimeOnly:false in vite.config.js and it then worked the same in Production as in HMR.我在 vite.config.js 中设置了 runtimeOnly:false,然后它在生产环境中的工作方式与在 HMR 中的工作方式相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM