微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何使用react-intl使用标签(链接)formatMessage?

我需要添加我需要翻译的文本的链接.我怎么能有链接的formatMessages?

在这就是我想要做的:

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',});

然后,在渲染函数中:

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>

这不起作用.我明白了:
通过单击“下订单”按钮,您可以确认已阅读,理解并接受我们的[对象对象],[对象对象]和[对象对象].

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

解决方法

Can you use the FormattedHTMLMessage component?

const messages = defineMessages({
  copy: {
    id: 'checkout.OrderReview.copy',defaultMessage: '<a href="/url">Terms and Conditions</a>',});

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐