不使用 FormattedMessage 和 React ContextTypes 获取翻译文本

Get translation text without using FormattedMessage and React ContextTypes

我正在寻找一种无需使用 FormattedMessage 即可获取翻译文本的方法。到目前为止,我只找到了提供使用 an React's EXPERIMENTAL feature 的解决方案。有没有其他方法可以完成这个(或其他 library/npm 模块)?

我更喜欢使用 context,但 react-intl 也提供了一个更高阶的组件 injectIntl,您可以改用它。这将传递一个具有所有命令式格式化功能的道具 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);