Skip to content
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

Support instant locale switching by making context consumers subscribe to IntlProvider #980

Closed
nat-n opened this issue Jul 6, 2017 · 3 comments
Labels

Comments

@nat-n
Copy link

nat-n commented Jul 6, 2017

As far as I can tell there's currently no simple and convenient way with React-Intl of supporting instant locale switching in a large app that uses pure components (such as your standard usage of react-redux).

I think the only good solution to this problem is the observer pattern, which would entail something along the lines of the following:

  • the intl object created by IntlProvider and shared on the context includes a subscribe method
  • consuming components like FormattedMessage or InjectIntl call context.intl.subscribe on componentWillMount with a listener and receive back a unsubscribe method
  • upon receiving a new value for locale, IntlProvider notifies all listeners
  • the listener calls forceUpdate on the consuming component, thus directly triggering the required updates
  • the unsubscribe function returned by the intl.subscribe is stored on the consuming component then called in componentWillUnmount. It removes the corresponding listener from the collection maintained on IntlProvider.

I believe the result would be a clean abstraction (no extra configuration or usage gotchas necessary for users), that should solve the problem seamlessly with minimal performance implications. However if there were any concerns about enabling this by default it could be enabled via a prop on IntlProvider, after-all not everyone uses pure components and needs instant locale switching.

I'd be happy to put together a pull request if you think this is a good idea and likely to be merged.

@nat-n nat-n changed the title Enable elegant instant locale switching by making context consumers subscribe to IntlProvider Support instant locale switching by making context consumers subscribe to IntlProvider Jul 6, 2017
@macku
Copy link

macku commented Apr 18, 2018

I was trying to find an existing solution but I failed. So I decided to build my own on top of React-intl and new React Context syntax.
You can find the results here: https://stackblitz.com/edit/react-locales

@stale
Copy link

stale bot commented May 30, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label May 30, 2019
@stale
Copy link

stale bot commented Jun 6, 2019

Issue was closed because of inactivity. If you think this is still a valid issue, please file a new issue with additional information.

@stale stale bot closed this as completed Jun 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants