Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs react-i18-next在组件之外的简单文件中使用t(';title';)函数_Reactjs_React I18next - Fatal编程技术网

Reactjs react-i18-next在组件之外的简单文件中使用t(';title';)函数

Reactjs react-i18-next在组件之外的简单文件中使用t(';title';)函数,reactjs,react-i18next,Reactjs,React I18next,我有一个常数,它只输出数据 Import i18n from './i18n' export const offersList = [ { id: 0, itemButton: i18n.t('item1'), title: i18n.t('title1'), }, { id: 0, itemButton: 'Item 1', title: 'Title 1', }, { id: 0, itemButton: '

我有一个常数,它只输出数据

Import i18n from './i18n'
export const offersList = [
  {
    id: 0,
    itemButton: i18n.t('item1'),
    title: i18n.t('title1'),
  },
  {
    id: 0,
    itemButton: 'Item 1',
    title: 'Title 1',
  },
  {
    id: 0,
    itemButton: 'Item 1',
    title: 'Title 1',
  }
];
当我试图在key中使用t函数时,它只返回一个简单的字符串,其中key是我想要显示的。 我有这样的i18n.ts文件

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import Backend from 'i18next-http-backend';

i18n
  .use(Backend)
  .use(initReactI18next)
  .init({
    fallbackLng: 'ru',
    debug: true,
    react: {
      useSuspense: false
    },
    interpolation: {
      escapeValue: false, 
    }
  });


export default i18n;
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

// the translations
// (tip move them in a JSON file and import them)
const resources = {
  en: {
    translation: {
      'title': 'En test title',
    },
  },
  ru: {
    translation: {
      'title': 'Ru test title',
    },
  }
};

i18n
  .use(initReactI18next) // passes i18n down to react-i18next
  .init({
    resources,
    lng: 'en',

    keySeparator: false, // we do not use keys in form messages.welcome

    interpolation: {
      escapeValue: false, // react already safes from xss
    },
  });

export default i18n;

所以我找到了一个解决办法,它在我的情况下是有效的,但也许你也有同样的办法

问题是我使用了i18next的后端模块

import Backend from 'i18next-http-backend'
在我的情况下,我不应该使用这个模块。 所以i18n.js(在我的例子中是.ts)配置文件如下

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import Backend from 'i18next-http-backend';

i18n
  .use(Backend)
  .use(initReactI18next)
  .init({
    fallbackLng: 'ru',
    debug: true,
    react: {
      useSuspense: false
    },
    interpolation: {
      escapeValue: false, 
    }
  });


export default i18n;
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

// the translations
// (tip move them in a JSON file and import them)
const resources = {
  en: {
    translation: {
      'title': 'En test title',
    },
  },
  ru: {
    translation: {
      'title': 'Ru test title',
    },
  }
};

i18n
  .use(initReactI18next) // passes i18n down to react-i18next
  .init({
    resources,
    lng: 'en',

    keySeparator: false, // we do not use keys in form messages.welcome

    interpolation: {
      escapeValue: false, // react already safes from xss
    },
  });

export default i18n;
如果您想在一个简单的js/ts文件中处理组件之外的数据 可以使用t()函数。您应该在文件中导入i18next配置文件(在我的例子中,是一个简单的对象数组,其中包含我导出的数据)


您如何调用
t()
?itemButton:i18n.t('item1')对不起,我错过了。另一个问题-为什么不在组件内部而不是外部进行翻译?类似于
offersList.map(o=>{i18n.t(o.itemButton)}
)`?嗯,我不知道,我会考虑一下,但我想我还是需要在外面使用它,因为这只是一个例子,真正的数组要大得多。但我会尝试在这里找到答案: