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.

31 lines
515 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') { I18n.default_locale }
  23. end
  24. end