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.

33 lines
707 B

3 years ago
  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