我有很多这样的函数:
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>);
}
}
只需确保在调用函数之前加载翻译即可。
关于javascript - 在函数中使用 React JS i18n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45367018/