I18n in StimulusReflex

lxxxvi

Mario

Posted on April 14, 2021

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

  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)

💖 💪 🙅 🚩
lxxxvi
Mario

Posted on April 14, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

I18n in StimulusReflex
rails I18n in StimulusReflex

April 14, 2021