使用in函数反应JS i18n

Fel*_*lix 0 javascript i18next reactjs react-i18next

我有很多这样的功能:

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>);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这里我有一个错误。-> t不是函数..

因为这是缺少的:

export default compose(
    translate('translations'),
    connect()
)(LanguageChooser);
Run Code Online (Sandbox Code Playgroud)

我怎样才能将translate('translations')部分添加到函数中?

谢谢

jam*_*uhl 5

仅组件需要translate hoc->它断言在转换更改时或在设置了转换后重新渲染组件,以便组件在初始渲染之前等待加载翻译文件。

在函数中使用i18next,只需:

import i18n from '../i18n'; // assuming you got a 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>);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 继续 { t } = i18n; (3认同)