You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that react-intl v2.x is very different from v1.x. I've seen some of the breaking changes when browsing the release logs, but is there a more complete upgrade guide out there?
I believe this is the "standard" usage in react-intl v1.x. Of course, mixin is used.
Now I'm little confused about the new v2.x way, which looks like I need to re-write(correct me if im wrong) every language file in a "defineMessages" way:
constmessages=defineMessages({label: {id: 'send_button.label',defaultMessage: 'Send',},tooltip: {id: 'send_button.tooltip',defaultMessage: 'Send the message'}});
So is there any guide on this change? Do I have to re-write ALL translations? Or how to use those old translations with the brand new API? I think I need little help here.
@liuyanghejerry One change is that messages is expected to be a flat object. Before feeding the translated messages into <IntlProvider> you'll want to flatten the object. You could do something like:
A new feature of v2 is being able to define your default messages inside your source code where they are used and use The Babel plugin: babel-plugin-react-intl to extract these default messages for translation. But this is optional; you don't have to use this, and you can migrate to it overtime. You can keep your default messages being defined in a separate, single file, if you wish. That'll still work.
The above example you provided in not correct. First, React components return elements, not a text string; second, getIntlMessage() is gone, and third there is no message prop. Instead it would look like this (assuming you've flattened the messages:
If you wanted to move to the defining your default messages right where you're formatting them and use the Babel plugin to extract them, then you could do this (assuming English as the source/default language):
And finally, if you wanted to format the message to a string and use it in a non-element way, like a title or aria attribute value, then you could do this:
In this example I used a stateless component and passed it into injectIntl() which will provide the intl prop which has the imperative API on it which includes the formatMessage() function. This is the same API that's used internally in all of the React Intl <Formatted*> components. These format*() functions will always return a string instead of a React element.
The major upgrade steps:
Use a flat messages object.
Wrap the app's root component with <IntlProvider>.
Use <FormattedMessage id="some_message_id" />
Use injectIntl() HOC function to access imperative API via intl prop.
I know I am a little late to the party, but I am wondering what is the main driver behind flattening the objects is? My main concern is that it will increase our payload as there will be a lot of duplicated strings being sent over the line in the messages data structure. Would it be possible to keep the string identifiers with the dot notation like they are in the example above, but still keep the underlying messages structure nested?
Would it be possible to keep the string identifiers with the dot notation like they are in the example above, but still keep the underlying messages structure nested?
Yes. The strings are no longer interpreted so you can use whatever schema you want.
@jopdeklein You can still use non-flatten structures to store your messages, since you can transform any non-flattened structures into a flattened ones. The idea is that you can store and transfer nested structures, and flatten them when you feed them to react-intl.
@liuyanghejerry@ericf OK that makes sense - I'll balance the performance between sending more data vs the CPU cycles used for flattening the structure during run-time. Thanks for your suggestions, appreciated.
Activity
ericf commentedon Oct 27, 2015
Not yet, but this will be something that will be created along with the updated docs. Is there something in particular that you're curious about?
liuyanghejerry commentedon Oct 28, 2015
My projects now have some language files, written as JSON-like Object, such as:
And one of the files is loaded, which depends on current locale, and be set via props:
And finally, these translations will be used like:
I believe this is the "standard" usage in react-intl v1.x. Of course, mixin is used.
Now I'm little confused about the new v2.x way, which looks like I need to re-write(correct me if im wrong) every language file in a "defineMessages" way:
So is there any guide on this change? Do I have to re-write ALL translations? Or how to use those old translations with the brand new API? I think I need little help here.
ericf commentedon Oct 28, 2015
@liuyanghejerry One change is that
messagesis expected to be a flat object. Before feeding the translated messages into<IntlProvider>you'll want to flatten the object. You could do something like:A new feature of v2 is being able to define your default messages inside your source code where they are used and use The Babel plugin:
babel-plugin-react-intlto extract these default messages for translation. But this is optional; you don't have to use this, and you can migrate to it overtime. You can keep your default messages being defined in a separate, single file, if you wish. That'll still work.The above example you provided in not correct. First, React components return elements, not a text string; second,
getIntlMessage()is gone, and third there is nomessageprop. Instead it would look like this (assuming you've flattened the messages:If you wanted to move to the defining your default messages right where you're formatting them and use the Babel plugin to extract them, then you could do this (assuming English as the source/default language):
And finally, if you wanted to format the message to a string and use it in a non-element way, like a
titleorariaattribute value, then you could do this:In this example I used a stateless component and passed it into
injectIntl()which will provide theintlprop which has the imperative API on it which includes theformatMessage()function. This is the same API that's used internally in all of the React Intl<Formatted*>components. Theseformat*()functions will always return a string instead of a React element.The major upgrade steps:
messagesobject.<IntlProvider>.<FormattedMessage id="some_message_id" />injectIntl()HOC function to access imperative API viaintlprop.liuyanghejerry commentedon Oct 29, 2015
Thanks! Now it seems much clearer and easier when upgrade.
Add 1.0 -> 2.0 Upgrade Guide
jopdeklein commentedon Jan 21, 2016
I know I am a little late to the party, but I am wondering what is the main driver behind flattening the objects is? My main concern is that it will increase our payload as there will be a lot of duplicated strings being sent over the line in the messages data structure. Would it be possible to keep the string identifiers with the dot notation like they are in the example above, but still keep the underlying messages structure nested?
ericf commentedon Jan 21, 2016
Yes. The strings are no longer interpreted so you can use whatever schema you want.
jopdeklein commentedon Jan 22, 2016
Thanks Eric, I will give it a go.
jopdeklein commentedon Jan 22, 2016
@ericf I think I didn't communicate my question properly, from all documentation I read I get the impression only the fully flattened message structure is supported: https://github.com/yahoo/react-intl/blob/bcef0973868e7e284ab1f90deaa16c2933cf6d70/UPGRADE.md#flatten-messages-object, is this correct?
liuyanghejerry commentedon Jan 22, 2016
@jopdeklein You can still use non-flatten structures to store your messages, since you can transform any non-flattened structures into a flattened ones. The idea is that you can store and transfer nested structures, and flatten them when you feed them to react-intl.
jopdeklein commentedon Jan 22, 2016
@liuyanghejerry @ericf OK that makes sense - I'll balance the performance between sending more data vs the CPU cycles used for flattening the structure during run-time. Thanks for your suggestions, appreciated.
ericf commentedon Jan 22, 2016
Likely to be imperceptible.
chore(deps-dev): bump @types/chai from 4.2.2 to 4.2.3 (#193)