4

I'm trying to support different languages in a React.js app and found react-intl to be a good candidate. They've been transitioning into V2 but I'm having a hard time figuring out how it all works together. The example app is too complex and involves a client/server architecture. I just want a single page without a server.

It looks like the steps are something like this:

  • Define messages using react-intl's defineMessage
  • Add locales using addLocaleData
  • Build flattened message data into files for each locale using a build script

I've done these steps but I'm at a loss as to how to display the messages. I have my React component wrapped in <IntlProvider>. The react-intl V2 github issue is really long and I've been wading through it trying to find an answer. Can anyone provide a simple working example?

Reed G. Law
  • 3,897
  • 1
  • 41
  • 78
  • Did u figure out some example, please share if u've done or found out a basic example using reac-intl as i'm also facing same problem as u – iamsaksham May 16 '16 at 06:05

2 Answers2

4

This is an example modified from the react-intl wiki

Looking at the code below, in the <IntlProvider> you will need to pass in a prop of messages={{post.title: "Hello World", post.body: "Amazing Content"}}. This would be the object with keys you have translated from your build script.

Switching the locale to 'en' would load the default messages. The addLocaleData adds data for translating number and date formats, not strings.

import React, {PropTypes} from 'react';
import ReactDOM from 'react-dom';

import {addLocaleData} from 'react-intl';
import en from 'react-intl/locale-data/en';
import fr from 'react-intl/locale-data/fr';
import es from 'react-intl/locale-data/es';
import pt from 'react-intl/locale-data/pt'

addLocaleData([...en, ...fr, ...es, ...pt]);

import {
    injectIntl,
    IntlProvider,
    FormattedRelative,
    FormattedMessage
} from 'react-intl';

const PostDate = injectIntl(({date, intl}) => (
    <span title={intl.formatDate(date)}>
        <FormattedRelative value={date}/>
    </span>
));

const App = ({post}) => (
    <div>
        <h1>{post.title}</h1>
        <p><PostDate date={post.date}/></p>
        <div>{post.body}</div>
    </div>
);

ReactDOM.render(
    <IntlProvider locale={'pt'} messages={{post.title: "Olá Mundo", post.body: "conteúdo surpreendente"}}>
        <App
            post={{
                title: <FormattedMessage id='post.title' defaultMessage='Hello, World!'} />,
                date: new Date(1459913574887),
                body: <FormattedMessage id='post.body' defaultMessage='Amazing Content!'} />,
            }}
        />
    </IntlProvider>,
    document.getElementById('container')
);
slchap5
  • 460
  • 3
  • 7
  • Any idea how to add the imports 'import en from 'react-intl/locale-data/en';' dynamically based on user's locale? – Santhosh Dec 21 '16 at 09:13
  • Hi @Santhosh. Could you create a new question? Separate from this one. I would be happy to answer. – slchap5 Dec 21 '16 at 15:49
  • Sure you can post your suggestions here http://stackoverflow.com/questions/41282510/how-to-add-locale-data-dynamically-using-react-intl – Santhosh Dec 22 '16 at 11:53
  • 1
    I don't understand what these built in language translations are. `react-intl/locale-data/en` – chovy Oct 15 '17 at 20:09
0

I think this answers the question. However it is based on using a 3rd party translation service and suggests to overwriting the translations file. What I personally did was to generate the file as per the script and used it to manually write my translations into another file. https://medium.freecodecamp.com/internationalization-in-react-7264738274a0

Paul Paulincai
  • 644
  • 4
  • 8
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes – slfan Jun 23 '17 at 20:34
  • The article is a tutorial, the essential part of it is every line between the first and the last letter. – Paul Paulincai Jun 24 '17 at 14:19