nayaabkhan / react-polyglot

React components and hooks for using Polyglot
MIT License
56 stars 15 forks source link

Update locale and messages on the fly #1

Closed rlebosse closed 8 years ago

rlebosse commented 8 years ago

Do you think it would be possible to make i18n updates on the fly ?

For instance, when the app starts, currant lang is en and the Greeter component displays Hello rlebosse.

But at a particular time, we decide we want to switch from en to fr locale and display Bonjour rlebosse by updating props <I18n locale={locale} messages={messages}>.

Thank you for your answer

nayaabkhan commented 8 years ago

Hey @rlebosse,

It is something I am yet to figure if it is a concern of this library or not.

Though, I think it is possible to achieve it simply by wrapping the <I18n locale={locale} messages={messages}> tree inside a React component which maintains 2 state items for the locale and messages. And then passing those in this way <I18n locale={this.state.locale} messages={this.state.messages}>. Then you could have some callback passed down the hierarchy to change the language. I'll see it I could write a demo for this.

rouflak commented 8 years ago

Sorry for the double issue reference. I've tested the code in my own app, and it did work well!

nayaabkhan commented 8 years ago

Hey @rouflak, great! Thanks for the PR. I'll check it and pull if all okay soon.

sidharthachatterjee commented 6 years ago

Even though I18n receives a new value for locale and messages if wrapped like in the example above, it doesn't really work because components wrapped with the translate decorator don't re-render when the Polyglot locale changes. Any ideas, @nayaabkhan?

controtie commented 6 years ago

@sidharthachatterjee FWIW, I've tried connecting the Provider to redux store and it seems to be working well. react-i18next seems to be passing a changeLanguage method down in context as well.

ruciu commented 6 years ago

Hi,

@controtie Could you provide example how did you do that? I am really struggling with it. Thanks!