DEV Community

Cover image for I18n in StimulusReflex
Mario
Mario

Posted on

2 2

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)

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay