Class: RailsErrorDashboard::LocalesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_error_dashboard/locales_controller.rb

Overview

Per-user language selection for the dashboard, persisted in the session.

Its own controller rather than another action on ErrorsController: this touches no error data, and keeping it separate means the picker cannot be reached by anything that filters or loads errors.

Instance Method Summary collapse

Methods inherited from ApplicationController

resolved_pagy_locales

Methods included from Translation

#red_locale, #red_t, #red_tp

Instance Method Details

#createObject

POST /locale

Sets session and returns the user to where they were.

The new locale takes effect on the NEXT request, not this one. That is deliberate and it is what keeps the around_action ordering intact (P5-T1 REQ-9): with_dashboard_locale has already resolved and set Current.locale by the time this action body runs, so writing the session here cannot disturb a locale that is mid-request. The redirect then renders the target page under the new value.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/rails_error_dashboard/locales_controller.rb', line 20

def create
  requested = params[:locale].to_s

  if I18nStore.available?(requested)
    # Store the canonical shipped spelling, not what the user sent.
    # I18nStore.resolve matches case-insensitively, so "PT-br" would
    # otherwise be written to the session and re-resolved on every
    # subsequent request.
    session[:red_locale] = I18nStore.resolve(requested)
  else
    # REQ-6: an unrecognized value is cleared rather than left to fail
    # silently on every later request. Falling through to config is the
    # correct behaviour, and clearing is what makes it happen.
    session.delete(:red_locale)
    flash[:alert] = red_t("red.flash.locale.unavailable")
  end

  redirect_to safe_return_path
rescue StandardError => e
  # Session storage can be unavailable (REQ-7: an API-only host where the
  # engine's session middleware did not take effect). Changing the language
  # is never worth breaking the dashboard over — the user keeps the
  # configured locale and the page still renders.
  Rails.logger.warn("[RailsErrorDashboard] Locale selection failed: #{e.class} - #{e.message}")
  redirect_to safe_return_path
end