【问题标题】:Get translation text without using FormattedMessage and React ContextTypes在不使用 FormattedMessage 和 React ContextTypes 的情况下获取翻译文本
【发布时间】:2016-05-25 05:48:40
【问题描述】:

我正在寻找一种不使用 FormattedMessage 来获取翻译文本的方法。到目前为止,我只找到了提供使用ContextTypesReact's EXPERIMENTAL feature 的解决方案。还有其他方法可以完成此任务(或其他库/npm 模块)吗?

【问题讨论】:

    标签: node.js reactjs webpack react-intl


    【解决方案1】:

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

    【讨论】:

    • 我更喜欢使用 context 的解决方案,但它是一个实验性功能,可能随时更改。你知道其他像 react-intl 这样的库吗?
    • @Webman 我不确定你是否理解我的答案,我更新了它以澄清:你可以使用 react-intl 的injectIntl 函数代替 context。这是由 react-intl 提供的。
    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    相关资源
    最近更新 更多