diff options
author | Claire <claire.github-309c@sitedethib.com> | 2022-02-08 18:23:53 +0100 |
---|---|---|
committer | Claire <claire.github-309c@sitedethib.com> | 2022-02-08 18:23:53 +0100 |
commit | 692963d43beb5e66a86e15d63b5aa3eeca82f0a1 (patch) | |
tree | bc7319ae242a889bb1d05b7afdd365d78a43ac1d /app/controllers/concerns | |
parent | b1983623aec8e0b066d115736d2151e0c74407fa (diff) | |
parent | b6d7726ecbc833abd00f6a9d36b24d9776cfe623 (diff) |
Merge branch 'main' into glitch-soc/merge-upstream
Diffstat (limited to 'app/controllers/concerns')
-rw-r--r-- | app/controllers/concerns/localized.rb | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/app/controllers/concerns/localized.rb b/app/controllers/concerns/localized.rb index fe1142f34..f7b62f09c 100644 --- a/app/controllers/concerns/localized.rb +++ b/app/controllers/concerns/localized.rb @@ -7,27 +7,24 @@ module Localized around_action :set_locale end - def set_locale - locale = current_user.locale if respond_to?(:user_signed_in?) && user_signed_in? - locale ||= session[:locale] ||= default_locale - locale = default_locale unless I18n.available_locales.include?(locale.to_sym) - - I18n.with_locale(locale) do - yield - end + def set_locale(&block) + I18n.with_locale(requested_locale || I18n.default_locale, &block) end private - def default_locale - if ENV['DEFAULT_LOCALE'].present? - I18n.default_locale - else - request_locale || I18n.default_locale - end + def requested_locale + requested_locale_name = available_locale_or_nil(params[:locale]) + requested_locale_name ||= available_locale_or_nil(current_user.locale) if respond_to?(:user_signed_in?) && user_signed_in? + requested_locale_name ||= http_accept_language if ENV['DEFAULT_LOCALE'].blank? + requested_locale_name + end + + def http_accept_language + HttpAcceptLanguage::Parser.new(request.headers.fetch('Accept-Language')).language_region_compatible_from(I18n.available_locales) if request.headers.key?('Accept-Language') end - def request_locale - http_accept_language.language_region_compatible_from(I18n.available_locales) + def available_locale_or_nil(locale_name) + locale_name.to_sym if locale_name.present? && I18n.available_locales.include?(locale_name.to_sym) end end |