简体   繁体   English

带有命名空间的 i18next 未获取预期的翻译文件

[英]i18next with namespace does not fetch expected translation file

I'm trying to setup localisation for an existing app.我正在尝试为现有应用设置本地化。 I've gotten fairly far.我已经走得很远了。 But something is not quite right.但有些事情不太对劲。

My setup is having multiple translation calls in the files like so: {attribute: 'stress', text: t('dass21::I found it hard to wind down')},我的设置是在文件中有多个翻译调用,如下所示: {attribute: 'stress', text: t('dass21::I found it hard to wind down')},

As you can see, I changed the namespace separator to be a double:: since I have a lot of medical specific sentences which are hard to think keys for, so the actual sentence is the key.如您所见,我将命名空间分隔符更改为 double:: 因为我有很多医学特定的句子很难想出关键,所以实际的句子是关键。

To export the translation into json translations files I use: i18next 'src/**/*.js' which works fine.要将翻译导出到 json 翻译文件中,我使用: i18next 'src/**/*.js'可以正常工作。 It makes a folder for every language, and in that folder, for every namespace it makes a file.它为每种语言创建一个文件夹,并在该文件夹中为每个命名空间创建一个文件。 (Eg in this case, it will make a file dass21.json ). (例如,在这种情况下,它将创建一个文件dass21.json )。

My configuration for the i18next-parser is:我对i18next-parser的配置是:

module.exports = {
  // Since keys hold regular english I can't have the default settings
  namespaceSeparator: '::',
  keySeparator: '_',
  pluralSeparator: "|",

  locales: ['en', 'es'],

  output: 'assets/locales/$LOCALE/$NAMESPACE.json',
}

I matches those settings in the i18next init like so:我匹配i18next init 中的这些设置,如下所示:

import i18next from "i18next";

import HttpApi from 'i18next-http-backend';

i18next
.use(HttpApi)
.init({
  backend: {
    // for all available options read the backend's repository readme file
    loadPath: '/assessments/locales/{{lng}}/{{ns}}.json',
  },

  fallbackLng: 'en',
  lng: 'en',
  supportedLngs: ['en', 'es'],

  namespaceSeparator: '::',
  keySeparator: '_',
  pluralSeparator: "|",
});

const t = i18next.t

export default t

As you can see, I want to load my translation files over http (which are served by a node server and this works).如您所见,我想通过 http 加载我的翻译文件(由节点服务器提供服务,并且可以正常工作)。 However, it only tries to load a generic translation file called translation.js and none of my specific namespace files.但是,它只尝试加载一个名为translation.js的通用翻译文件,而不是我的特定命名空间文件。 Even though the call to t with dass21::I found it hard to wind down is called.尽管调用tdass21::I found it hard to wind down被调用。 (Since it is visible on the screen using its key). (因为使用它的键在屏幕上可见)。

How can I make sure it also tries to load the namespace specific files over http, while having the custom namespace separators?如何确保它还尝试通过 http 加载命名空间特定文件,同时具有自定义命名空间分隔符?

If you're using i18next, make sure you pass in the desired namespace in to the useTranslation or withTranslation function: https://react.i18next.com/guides/multiple-translation-files如果您使用 i18next,请确保将所需的命名空间传递给 useTranslation 或 withTranslation function: https://react.i18next.com/guides/multiple-translation-files

Alternatively, use the ns init option and define your namespaces.或者,使用 ns init 选项并定义您的命名空间。

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

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