To ensure Devise error messages are correctly translated, you must set the locale in your application.
Important: Do not use I18n.with_locale inside an around_action to set the locale per request, as a known Warden bug will prevent some error messages from being translated. Instead, use a before_action in your controller or a custom middleware.
# Option 1: Using a before_action
before_action do
I18n.locale = :es # Or your dynamic logic
end
# Option 2: Using Middleware
class LocaleMiddleware
def initialize(app)
@app = app
end
def call(env)
I18n.locale = :es # Or your dynamic logic
status, headers, body = @app.call(env)
end
end
# In config/application.rb
config.middleware.use ::LocaleMiddleware