You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
664 B

  1. # frozen_string_literal: true
  2. module Localized
  3. extend ActiveSupport::Concern
  4. included do
  5. around_action :set_locale
  6. end
  7. private
  8. def set_locale
  9. locale = default_locale
  10. if user_signed_in?
  11. begin
  12. locale = current_user.try(:locale) || default_locale
  13. rescue I18n::InvalidLocale
  14. locale = default_locale
  15. end
  16. end
  17. I18n.with_locale(locale) do
  18. yield
  19. end
  20. end
  21. def default_locale
  22. ENV.fetch('DEFAULT_LOCALE') {
  23. user_supplied_locale || I18n.default_locale
  24. }
  25. end
  26. def user_supplied_locale
  27. http_accept_language.language_region_compatible_from(I18n.available_locales)
  28. end
  29. end