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
Upgrade guide from v1.x to v2.x? #193
Comments
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? |
My projects now have some language files, written as JSON-like Object, such as: var zh_CN = {
"locales": "zh",
"messages": {
"GENERAL": {
"UNKNOWN_DEVICE": "未知设备",
"MAP_LOAD_ERROR": "地图加载失败",
},
} And one of the files is loaded, which depends on current locale, and be set via props: <MainFrame {...intlData} /> And finally, these translations will be used like: var someText =
<FormattedMessage
message={this.getIntlMessage('GENERAL.UNKNOWN_DEVICE')} />; 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: const messages = 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 function flattenMessages(nestedMessages, prefix = '') {
return Object.keys(nestedMessages).reduce((messages, key) => {
let value = nestedMessages[key];
let prefixedKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'string') {
messages[prefixedKey] = value;
} else {
Object.assign(messages, flattenMessages(value, prefixedKey));
}
return messages;
}, {});
}
let messages = flattenMessages(nestedMessages); 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: var someText =
<FormattedMessage
message={this.getIntlMessage('GENERAL.UNKNOWN_DEVICE')} />; The above example you provided in not correct. First, React components return elements, not a text string; second, <p>
<FormattedMessage id="GENERAL.UNKNOWN_DEVICE" />
</p> 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): <p>
<FormattedMessage
id="GENERAL.UNKNOWN_DEVICE"
defaultMessage="Unknown Device"
/>
</p> And finally, if you wanted to format the message to a string and use it in a non-element way, like a const messages = defineMessages({
unknown_device: {
id: 'GENERAL.UNKNOWN_DEVICE',
defaultMessage: 'Unknown Device'
}
});
const SomeComponent = injectIntl(({intl}) => {
const message = intl.formatMessage(messages.unknown_device);
return <div title={message}>{message}</div>;
});
export default SomeComponent; In this example I used a stateless component and passed it into The major upgrade steps:
|
Thanks! Now it seems much clearer and easier when upgrade. |
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? |
Yes. The strings are no longer interpreted so you can use whatever schema you want. |
Thanks Eric, I will give it a go. |
@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? |
@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. |
Likely to be imperceptible. |
Bumps [@types/chai](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/chai) from 4.2.2 to 4.2.3. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/chai) Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
Hi all,
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?
The text was updated successfully, but these errors were encountered: