DEV Community

loading...
Cover image for I18n in StimulusReflex

I18n in StimulusReflex

lxxxvi profile image Mario ・1 min read

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

  private

  def switch_locale(&action)
    # replace `session[:locale]` with your custom logic
    locale = session[:locale] || I18n.default_locale
    I18n.with_locale(locale, &action)
  end
end
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)

Discussion (0)

Forem Open with the Forem app