【发布时间】:2016-05-25 05:48:40
【问题描述】:
我正在寻找一种不使用 FormattedMessage 来获取翻译文本的方法。到目前为止,我只找到了提供使用ContextTypes 和React's EXPERIMENTAL feature 的解决方案。还有其他方法可以完成此任务(或其他库/npm 模块)吗?
【问题讨论】:
标签: node.js reactjs webpack react-intl
我正在寻找一种不使用 FormattedMessage 来获取翻译文本的方法。到目前为止,我只找到了提供使用ContextTypes 和React's EXPERIMENTAL feature 的解决方案。还有其他方法可以完成此任务(或其他库/npm 模块)吗?
【问题讨论】:
标签: node.js reactjs webpack react-intl
我更喜欢使用context,但是 react-intl 也提供了一个更高阶的组件injectIntl,你可以使用它来代替。这将传递一个 prop intl,它具有所有命令式格式化函数。
import React from "react";
import {injectIntl, intlShape} from "react-intl";
class MyComponent extends React.Component {
static propTypes = {
intl: intlShape.isRequired
}
render() {
return <p>{this.props.intl.formatDate(new Date())}</p>;
}
}
export default injectIntl(Component);
【讨论】:
injectIntl 函数代替 context。这是由 react-intl 提供的。