2

我正在寻找一种不使用 FormattedMessage 来获取翻译文本的方法。到现在为止,我发现只有这个解决方案提供了使用ContextTypesReact 的实验特性。还有其他方法可以完成此任务(或其他库/npm 模块)吗?

4

1 回答 1

3

I prefer using context, but react-intl does also provide a higher order component injectIntl you can use instead. This will pass a prop intl that has all the imperative formatting functions.

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);
于 2016-05-24T20:52:53.743 回答