在函数中使用 React JS i18n

react JS i18n using in functions

我有很多这样的功能:

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);

如何将 translate('translations') 部分添加到函数中?

谢谢

仅组件需要 translate 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>);
    }
}

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