【问题标题】:How to retrieve a string in ReactIntl 2.0 without using FormattedMessage如何在不使用 FormattedMessage 的情况下在 ReactIntl​​ 2.0 中检索字符串
【发布时间】:2016-05-13 04:42:03
【问题描述】:

如果我错了,请纠正我,ReactIntl​​ 中的 FormattedMessage 返回一个由 span 标签包裹的字符串。在 ReactIntl​​ 1.2 中,我们可以选择使用 this.getIntlMessage('key') 来仅获取字符串部分。

这是我的问题:ReactIntl​​ 2.0 中是否有类似的功能?我知道字符串可以通过使用 FormattedMessage 中的 Function-As-Child 模式作为

<FormattedMessage id="placeholder">
    {(formattedValue)=>(
        <MyComponent ref="mycomponent" placeholder={formattedValue}/>
    )}
</FormattedMessage>

但是,它弄乱了我组件中的“引用”,我无法再使用this.refs.mycomponent 访问该组件。

【问题讨论】:

    标签: reactjs react-intl


    【解决方案1】:

    如果您使用的是功能组件,那么您可以使用useIntl() 钩子获取intl 对象并从下面的代码sn-p 中获取string 的消息。

    import {IntlProvider, useIntl} from 'react-intl';
    
    export function MyFunctionalComponent() {
        const intl = useIntl();
        return (
            <div>
                <p>{intl.formatMessage({id: "MyKey"})}</p>
            </div>
        )
    }
    

    注意:您的父组件应包裹在&lt;/IntlProvider&gt; 提供者周围。

    【讨论】:

      【解决方案2】:

      我使用 React 渲染道具解决了这个问题。

      我创建了一个实现它的 npm 包:http://g14n.info/react-intl-inject/

      就是这样的组件

      import { injectIntl } from 'react-intl'
      
      export default function reactIntlInject ({ children, ...props }) {
        if (typeof children === 'function') {
          return (
            children(props)
          )
        } else {
          return null
        }
      }
      

      您可以使用它来包装组件,例如具有要翻译的道具,例如

      import React, { Component } from 'react'
      // Import the package I created, available on npm with MIT license....
      import InjectIntl from 'react-intl-inject'
      // ...or copy the code above in a file in your project and import it, somethong like
      // import InjectIntl from './path/to/my/render/prop/component'
      
      class MyComponent extends Component {
        render () {
          return (
            <InjectIntl>
              {({ intl }) => (
                <button
                  type='submit'
                  value={intl.formatMessage({ id: 'enter.submit' })}
                />
              )}
            </InjectIntl>
          )
        }
      }
      
      export default injectIntl(reactIntlInject)
      

      【讨论】:

        【解决方案3】:

        有一个更好的解决placeholder的问题。

        <FormattedMessage ...messages.placeholderIntlText>
          {
             (msg) =>  <input type="text" placeholder = {msg} />
          }
        </FormattedMessage>
        

        【讨论】:

          【解决方案4】:

          您可以使用 react-intl 提供的 intl 对象轻松返回字符串。

          这就是你在 React 类中使用 intl 对象的方式更简单。

          注意:渲染组件(主组件)应该用 IntlProvider 包装

          class MySubComponent extends React.Component{
            {/*....*/}
          
            render(){
              return(
               <div>
                  <input type="text" placeholder = {this.context.intl.formatMessage({id:"your_id", defaultMessage: "your default message"})}
               </div>
          
              )
            }
             }
          MySubComponent.contextTypes ={
           intl:React.PropTypes.object.isRequired
          }
          

          通过定义 contextTypes,您可以使用 intl 对象,它是一个上下文类型属性。有关更多详细信息,请参阅反应上下文。

          【讨论】:

            【解决方案5】:

            好的,有一个解决方法。我可以像这样在组件中添加 ReactIntl​​ 作为上下文:

            contextTypes: {
                intl: React.PropTypes.object.isRequired,
            },
            

            然后,当尝试检索消息的字符串并使用它时,例如在占位符中,我可以这样做。

            <MyComponent ref="mycomponent" placeholder={this.context.intl.messages.placeholder}/>
            

            【讨论】:

            • 答案不是很丰富,请考虑添加更多信息
            猜你喜欢
            • 1970-01-01
            • 2010-09-25
            • 2012-09-06
            • 1970-01-01
            • 1970-01-01
            • 2017-12-24
            • 2011-03-20
            • 2016-01-17
            相关资源
            最近更新 更多