A very basic react-intl
setup.
- We have a HOC
withIntl
that injectst()
as prop to our localized components.t()
is just a wrapper around thereact-intl
formatMessage
function. - We have a
IntlProvider
component that wraps our whole application and provides the translated messages and loads the locale data. We are usingRelay
to get the locale of the user, but you can do it any way you want. - Sample de.js that hold our German translations.
- In our
Example
component we can call thet()
. We can pass values or not depending on the translated message.
We are using the english translations (Hello, {name}
) as an ID, but you can have arbitrary IDs (hello_firstName
).
Read the docs: https://github.com/yahoo/react-intl
In
react-intl
they already have such tag it is called<FormattedMessage>
, but then you have to useintl.formatMessage
function in the non JSX code. We didn't want to use a tag in one place and a function in another, so we opted for the wrapper that provides a function everywhere.https://github.com/yahoo/react-intl/wiki/Components#formattedmessage