DEV Community

Cover image for I18n in StimulusReflex

Posted on

I18n in StimulusReflex

How do you use I18n in your Reflexes?

There is an official approach in the Documentation of StimulusReflex to do that.

However, here's another one:

# ./app/reflexes/application_reflex.rb
class ApplicationReflex < StimulusReflex::Reflex
  around_reflex :switch_locale


  def switch_locale(&action)
    # replace `session[:locale]` with your custom logic
    locale = session[:locale] || I18n.default_locale
    I18n.with_locale(locale, &action)
Enter fullscreen mode Exit fullscreen mode

Yes, it is basically the same thing as Rails suggests for Managing the Locale across Requests.

This way I like that all reflexes have the locale set by default, and I do not have to care about it anymore when adding new reflexes.

What's your experience/opinion regarding I18n in StimulusReflex?

(Cover image by Nareeta Martin on unsplash)

Top comments (0)