【发布时间】:2020-07-20 20:04:44
【问题描述】:
我有一个只导出数据的常量。
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;
【问题讨论】:
-
你怎么打电话给
t()? -
itemButton: i18n.t('item1')
-
对不起,我错过了。另一个问题 - 为什么不在组件中而不是外部进行翻译?类似
offersList.map(o => <Button>{i18n.t(o.itemButton)}</Button>)`? -
嗯不知道,会考虑的,但我想无论如何我都需要在外面使用它,因为这只是示例,真正的数组要大得多。但我会尝试
-
也许你会在这里找到答案:stackoverflow.com/questions/44518857/…
标签: reactjs react-i18next