localized.rb 707 B

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. module Localized
  3. extend ActiveSupport::Concern
  4. included do
  5. around_action :set_locale
  6. end
  7. def set_locale
  8. locale = current_user.locale if respond_to?(:user_signed_in?) && user_signed_in?
  9. locale ||= session[:locale] ||= default_locale
  10. locale = default_locale unless I18n.available_locales.include?(locale.to_sym)
  11. I18n.with_locale(locale) do
  12. yield
  13. end
  14. end
  15. private
  16. def default_locale
  17. if ENV['DEFAULT_LOCALE'].present?
  18. I18n.default_locale
  19. else
  20. request_locale || I18n.default_locale
  21. end
  22. end
  23. def request_locale
  24. http_accept_language.language_region_compatible_from(I18n.available_locales)
  25. end
  26. end