使用 in 函数响应 JS i18n



我有很多这样的函数:

export function dialogContent() {
const { t, i18n } = this.props;
switch (this.state.dialogHandlerVariable) {
//Delete changeLog
case 0:
return (<div> {t("dialog.dashboard.changelog.deleteChangelog.body")}</div>);
}
}

但是在这里我得到了一个错误。 -> t 不是一个函数.

因为缺少:

export default compose(
translate('translations'),
connect()
)(LanguageChooser);

如何将翻译('翻译'(部分添加到函数中?

谢谢

翻译 hoc 仅对组件是必需的 ->它断言组件在翻译更改时重新渲染,或者如果设置,则组件在初始渲染之前等待加载翻译文件。

要在函数中使用 i18next,只需:

import i18n from '../i18n'; // assuming you got an i18n instance configured and exported like in the samples - else just import i18n from 'i18next';
export function dialogContent() {
const t = i18n.t;
switch (this.state.dialogHandlerVariable) {
//Delete changeLog
case 0:
return (<div> {t("dialog.dashboard.changelog.deleteChangelog.body")}</div>);
}
}

只需确保在调用函数之前加载了翻译即可。

最新更新