闭社主体 forked from https://github.com/tootsuite/mastodon
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.

42 lines
809 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. if ENV['DEFAULT_LOCALE'].present?
  16. I18n.default_locale
  17. else
  18. request_locale || I18n.default_locale
  19. end
  20. end
  21. def request_locale
  22. preferred_locale || compatible_locale
  23. end
  24. def preferred_locale
  25. http_accept_language.preferred_language_from(available_locales)
  26. end
  27. def compatible_locale
  28. http_accept_language.compatible_language_from(available_locales)
  29. end
  30. def available_locales
  31. I18n.available_locales.reverse
  32. end
  33. end