首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用formatMessage -intl对标签(链接)进行formatMessage?

如何使用formatMessage -intl对标签(链接)进行formatMessage?
EN

Stack Overflow用户
提问于 2016-12-14 04:03:35
回答 3查看 10.2K关注 0票数 8

我需要添加链接到我需要翻译的文本。我怎样才能使用有链接的formatMessages?

现在这就是我想要做的:

代码语言:javascript
复制
const messages = defineMessages({
  copy: {
    id: 'checkout.OrderReview.copy',
    description: 'Label for add card button',
    defaultMessage: 'By clicking the "Place Order" button, you confirm that you have read, understood, and accept our {termsAndConditionsLink}, {returnPolicyLink}, and {privacyPolicyLink}.',
  },
  termsAndConditions: {
    id: 'checkout.OrderReview.termsAndConditions',
    description: 'Label for terms and conditions link',
    defaultMessage: 'Terms and Conditions',
  },
  returnPolicy: {
    id: 'checkout.OrderReview.returnPolicy',
    description: 'Label for return policy link',
    defaultMessage: 'Return Policy',
  },
  privacyPolicy: {
    id: 'checkout.OrderReview.privacyPolicy',
    description: 'Label for privacy policy link',
    defaultMessage: 'Privacy Policy',
  },
});

然后,在render函数中:

代码语言:javascript
复制
  const copy = formatMessage(messages.copy, {
    termsAndConditionsLink: <a href="#" className="u-underline text-color-grey">`${formatMessage(messages.termsAndConditions)}`</a>,
    returnPolicyLink: <a href="#" className="u-underline text-color-grey">`${formatMessage(messages.returnPolicy)}`</a>,
    privacyPolicyLink: <a href="#" className="u-underline text-color-grey">`${formatMessage(messages.privacyPolicy)}`</a>,
  });

return <div> { copy } </div>

这不管用。我得到:通过单击"Place Order“按钮,您确认您已经阅读、理解并接受了我们的对象对象、对象对象和对象对象。

完成此任务的正确方法是什么?

EN

回答 3

Stack Overflow用户

发布于 2019-10-25 18:16:46

首先,这取决于您的react-intl版本。我已经使用react-intl v2.x (准确地说是2.8)实现了这个功能。下面是我是如何做到的:

代码语言:javascript
复制
const messages = defineMessages({
  copy: {
    id: 'copy',
    defaultMessage: 'Accept our {TermsAndConditionsLink}',
  },
  termsAndConditions: {
    id: 'termsAndConditions',
    defaultMessage: 'Terms and conditions',
  },
  termsAndConditionsUrl: {
    id: 'termsAndConditionsUrl',
    defaultMessage: '/url',
  },
});

<FormattedMessage
  {...messages.copy}
  values={{
    TermsAndConditionsLink: (
      <a href={intl.formatMessage(messages.termsAndConditionsUrl)}>
        {intl.formatMessage(messages.termsAndConditions)}
      </a>
    ),
  }}
/>

对于较新的react-intl版本,您可以在文档中找到答案:

v3.x:https://formatjs.io/docs/react-intl/upgrade-guide-3x#enhanced-formattedmessage--formatmessage-rich-text-formatting

v4.x:https://formatjs.io/docs/react-intl/api/#formatmessage

票数 7
EN

Stack Overflow用户

发布于 2016-12-20 22:05:43

Can you use the FormattedHTMLMessage component?

代码语言:javascript
复制
const messages = defineMessages({
  copy: {
    id: 'checkout.OrderReview.copy',
    description: 'Label for add card button',
    defaultMessage: 'By clicking the "Place Order" button, you confirm that you have read, understood, and accept our {termsAndConditionsLink}, {returnPolicyLink}, and {privacyPolicyLink}.',
  },
  termsAndConditions: {
    id: 'checkout.OrderReview.termsAndConditions',
    defaultMessage: '<a href="/url">Terms and Conditions</a>',
  },
});

const Component = () => <FormattedHTMLMessage {...{
  ...messages.copy,
  values: {
    termsAndConditionsLink: <FormattedHTMLMessage {...messages. termsAndConditions} />
  }
} />
票数 1
EN

Stack Overflow用户

发布于 2020-07-18 03:08:52

代码语言:javascript
复制
<FormattedMessage 
        id="footer.table_no" 
        defaultMessage="Hello {link}" 
        values={{ link: <a href="www.google.com">World</a> }}
    />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41129628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档