Skip to content

Commit

Permalink
Thread-safe locale switching
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Oct 5, 2018
1 parent 01a73a3 commit 06033a2
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ module LocaleSwitcher
extend ActiveSupport::Concern

included do
before_action :set_locale
around_action :switch_locale
helper_method :current_locale, :available_locales, :default_locale

# Sets the locale for the current session.
#
# Returns nothing.
def set_locale
def switch_locale(&action)
locale = if params["locale"]
params["locale"]
elsif current_user && current_user.locale.present?
current_user.locale
end

I18n.locale = available_locales.include?(locale) ? locale : default_locale
I18n.with_locale(available_locales.include?(locale) ? locale : default_locale, &action)
end

# Adds the current locale to all the URLs generated by url_for so users
Expand Down

0 comments on commit 06033a2

Please sign in to comment.