-
Notifications
You must be signed in to change notification settings - Fork 328
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
Add support for localized Turbo Streams broadcasts #437
base: main
Are you sure you want to change the base?
Conversation
This commit adds support for localized broadcasts in Turbo. The change includes a new configuration option to enable/disable the feature, updated helper and updated channel method to handle the localization. The main changes include: - Add `I18n` gem to the Gemfile - Implement a new method in `Turbo::Streams::Broadcasts` to handle broadcasting to multiple streams in different locales - Update the `turbo_stream_from` helper in `Turbo::StreamsHelper` to add the locale to the stream name when using localized broadcasts - Add a new config option to enable/disable localized broadcasts` - Added tests for the changes
What is missing for this to be merged? |
+1 |
@seanpdoyle Could you take a look at this? |
+1 Would be great to see this get merged. Turbo Streams currently ignores locales. |
@neudabei I know. This is why I added this PR. Its working perfectly in production and there's no considerable overhead when rendering out in different locales. The code might not be the prettiest. But I can work on improving it if anyone has some pointers. |
👋 Sorry for the direct ping @jorgemanrubia but is there anything preventing these localization improvements from being merged and released? |
@mrtnin I was able to render components with Turbo Stream components by simply changing how the locale is set before each action. In the def set_locale
I18n.locale = params[:locale] || session[:locale] || I18n.default_locale
end |
@pabvald If you have two subscribers, using a different locale each. When rendering HTML to send over websocket it will use the locale for the request that initiated the message. Meaning only one would receive correctly localized HTML. |
@mrtnin I see. My apologies. In a simpler setup the I hope they merge your PR then and you solve the issue 🍀 |
This PR adds support for broadcasting Turbo Streams in multiple locales using the I18n gem. The feature is opt-in and can be enabled by setting the
Turbo.localized_broadcasts
configuration to true.The
broadcast_action_to
andbroadcast_render_to
methods have been updated to broadcast content in the current locale via a new private methodbroadcasts_with_locale
. This method handles broadcasting the content for all available locales.Additionally, the
turbo_stream_from
helper has been updated to include the current locale in thestreamables
array so that you are subscribed to a stream in your current locale.Overall, I think this PR adds a useful new feature that can help developers build more powerful and localized web applications using Turbo Streams.