node.js - 不使用 FormattedMessage 和 React ContextTypes 获取翻译文本

标签 node.js reactjs webpack react-intl

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

最佳答案

我更喜欢使用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);

关于node.js - 不使用 FormattedMessage 和 React ContextTypes 获取翻译文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37422133/

相关文章:

javascript - 从文件导入数组后在 react 组件内映射数组

node.js - Webpack 没有创建它用来创建的 HTML 文件

javascript - react 16.14.0 : Error was not caught ReferenceError: exports is not defined

javascript - 将 vue.js 与语义 UI 结合使用

node.js - mongoose-paginate 按引用文档排序

node.js - Node Canvas :使用自定义字体

node.js - grunt 安装时遇到问题

node.js - Windows Azure 网站上运行的是哪个版本的 node.js?

javascript - 在React循环中向JSX元素添加 key 的不同方法

reactjs - Redux:无法弄清楚为什么 ownProps 与状态不同步