Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在不使用FormattedMessage和React ContextTypes的情况下获取翻译文本_Node.js_Reactjs_Webpack_React Intl - Fatal编程技术网

Node.js 在不使用FormattedMessage和React ContextTypes的情况下获取翻译文本

Node.js 在不使用FormattedMessage和React ContextTypes的情况下获取翻译文本,node.js,reactjs,webpack,react-intl,Node.js,Reactjs,Webpack,React Intl,我正在寻找一种不使用FormattedMessage获取翻译文本的方法。到目前为止,我发现只有这个解决方案提供了使用。是否有其他方法来完成此任务(或其他库/npm模块)?我更喜欢使用上下文,但react intl也提供了一个更高阶的组件,您可以使用它。这将传递一个具有所有命令格式功能的propintl import React from "react"; import {injectIntl, intlShape} from "react-intl"; class MyComponent ex

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

我更喜欢使用
上下文,但react intl也提供了一个更高阶的组件,您可以使用它。这将传递一个具有所有命令格式功能的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);
从“React”导入React;
从“react intl”导入{injectIntl,intlShape};
类MyComponent扩展了React.Component{
静态类型={
intl:intlShape.isRequired
}
render(){
返回{this.props.intl.formatDate(new Date())}

; } } 导出默认injectIntl(组件);
我更喜欢使用context的解决方案,但这是一个实验性的特性,随时可能发生变化。你知道其他库,比如react-intl吗?@Webman我不确定你是否理解我的答案,我更新了它以澄清:你可以使用react-intl的
injectIntl
函数,而不是
context
。这由react-intl提供。