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

Add support for localized Turbo Streams broadcasts #437

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mrtnin
Copy link

@mrtnin mrtnin commented Feb 26, 2023

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 and broadcast_render_to methods have been updated to broadcast content in the current locale via a new private method broadcasts_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 the streamables 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.

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
@brunoprietog
Copy link
Contributor

What is missing for this to be merged?

@mrtnin
Copy link
Author

mrtnin commented Apr 2, 2023

What is missing for this to be merged?

+1
I'd love to have this added and can work to fix any issues.

@mrtnin
Copy link
Author

mrtnin commented May 22, 2023

@seanpdoyle Could you take a look at this?

@neudabei
Copy link

neudabei commented Dec 8, 2023

+1 Would be great to see this get merged. Turbo Streams currently ignores locales.

@mrtnin
Copy link
Author

mrtnin commented Dec 10, 2023

+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.

@gjtorikian
Copy link

👋 Sorry for the direct ping @jorgemanrubia but is there anything preventing these localization improvements from being merged and released?

@pabvald
Copy link

pabvald commented Sep 10, 2024

@mrtnin I was able to render components with Turbo Stream components by simply changing how the locale is set before each action.

In the ApplicationController include the callback before_action :set_locale where the method directly sets I18n.locale:

def set_locale 
  I18n.locale = params[:locale] || session[:locale] || I18n.default_locale
end

Source

@mrtnin
Copy link
Author

mrtnin commented Sep 10, 2024

@pabvald
I believe you're wrong about this PR not being necessary.

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.

@pabvald
Copy link

pabvald commented Sep 10, 2024

@mrtnin I see. My apologies. In a simpler setup the before_action :set_locale seems to be enough.

I hope they merge your PR then and you solve the issue 🍀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants