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.

40 lines
871 B

  1. # frozen_string_literal: true
  2. module Localized
  3. extend ActiveSupport::Concern
  4. included do
  5. before_action :set_locale
  6. end
  7. private
  8. def set_locale
  9. I18n.locale = default_locale
  10. I18n.locale = current_user.locale if user_signed_in?
  11. rescue I18n::InvalidLocale
  12. I18n.locale = default_locale
  13. end
  14. def default_locale
  15. request_locale || env_locale || I18n.default_locale
  16. end
  17. def env_locale
  18. ENV['DEFAULT_LOCALE']
  19. end
  20. def request_locale
  21. preferred_locale || compatible_locale
  22. end
  23. def preferred_locale
  24. http_accept_language.preferred_language_from([env_locale]) ||
  25. http_accept_language.preferred_language_from(I18n.available_locales)
  26. end
  27. def compatible_locale
  28. http_accept_language.compatible_language_from([env_locale]) ||
  29. http_accept_language.compatible_language_from(I18n.available_locales)
  30. end
  31. end