[React Intl] Render Content with Placeholders using react-intl FormattedMessage

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[React Intl] Render Content with Placeholders using react-intl FormattedMessage相关的知识,希望对你有一定的参考价值。

Learn how to use react-intl to set dynamic values into your language messages. We’ll also learn how to pass in those values by using a values prop in the react-intl FormattedMessage component.

We‘ll also take a look at how to pass values with markup and still get all the benefits of translation.

 

Pass the ‘values‘ porp to the FormattedMessage:

          <h3>
            <FormattedMessage id="detail.author" values={{
              author: book.author
            }} />
          </h3>

 

For translations:

  ‘en-US‘: {
    detail: {
      author: ‘by {author}‘
    }
  },

 

It also supports pass in markdown as param:

              <p>
                <FormattedMessage id="detail.userRating" values={{
                  name: <strong>{review.name}</strong>,
                  rating: review.rating
                }}/><br />
                {new Date(review.date).toLocaleDateString()}
              </p>
  ‘en-US‘: {
    detail: {
      userRating: ‘{name} rated it: {rating} out of 5‘
    }
  },

 

以上是关于[React Intl] Render Content with Placeholders using react-intl FormattedMessage的主要内容,如果未能解决你的问题,请参考以下文章

React-intl 多语言应用程序:更改语言和翻译存储

[React Intl] Install and Configure the Entry Point of react-intl

为 react/react-intl 动态导入语言 json 文件

[React Intl] Format Date and Time Using react-intl FormattedDate and FormattedTime

非组件的 React-intl

react-intl 实现 React 国际化多语言