Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow formatting embedded XML #1379

Merged
merged 4 commits into from
Jul 25, 2019
Merged

feat: Allow formatting embedded XML #1379

merged 4 commits into from
Jul 25, 2019

Conversation

longlho
Copy link
Member

@longlho longlho commented Jul 25, 2019

Enhanced FormattedMessage & formatMessage rich text formatting

In v2, in order to do rich text formatting (embedding a ReactElement), you had to do this:

<FormattedMessage
  defaultMessage="To buy a shoe, { link } and { cta }"
  values={{
    link: (
      <a class="external_link" target="_blank" href="https://www.shoe.com/">
        visit our website
      </a>
    ),
    cta: <strong class="important">eat a shoe</strong>,
  }}
/>

Now you can do:

<FormattedMessage
  defaultMessage="To buy a shoe, <a>visit our website</a> and <cta>eat a shoe</cta>"
  values={{
    link: msg => (
      <a class="external_link" target="_blank" href="https://www.shoe.com/">
        {msg}
      </a>
    ),
    cta: msg => <strong class="important">{msg}</strong>,
  }}
/>

The change solves several issues:

  1. Contextual information was lost when you need to style part of the string: In this example above, link effectively is a blackbox placeholder to a translator. It can be a person, an animal, or a timestamp. Conveying contextual information via description & placeholder variable is often not enough since the variable can get sufficiently complicated.
  2. This brings feature-parity with other translation libs, such as fluent by Mozilla (using Overlays).

However, in cases where we allow placeholders to be a ReactElement will have to be rewritten to 1 of the 2 syntax down below:

Before

<FormattedMessage
  defaultMessage="Hello, {name}"
  values={{
    name: <b>John</b>,
  }}
/>

After

<FormattedMessage
  defaultMessage="Hello, <b>John</b>"
  values={{
    b: name => <b>{name}</b>,
  }}
/>

OR (NOT RECOMMENDED)

<FormattedMessage
  defaultMessage="Hello, <name/>"
  values={{
    name: () => <b>{John}</b>,
  }}
/>

@longlho longlho requested a review from redonkulus July 25, 2019 10:46
@longlho longlho merged commit 61d3c1b into master Jul 25, 2019
@longlho longlho deleted the xml branch July 25, 2019 13:10
@longlho longlho added this to the v3.0.0 milestone Jul 25, 2019
@mbalaam
Copy link

mbalaam commented Nov 11, 2019

I was hoping this would do nesting, but it would appear not to. Is that correct behaviour?

For example with:

defaultMessage={"<strong>19<sup>th</sup> of the month<strong>"}

I am just getting:

<strong>19</strong>

@longlho
Copy link
Member Author

longlho commented Nov 11, 2019

We do support nesting (and have a test for it: https://github.com/formatjs/react-intl/blob/master/test/unit/components/message.tsx#L228)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants