Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread-safe locale switching #4237

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Decidim::User.where(**search for old subscribed users**).update(newsletter_notif
- **decidim-core**: Fix newsletter opt-in migration [\#4198](https://github.com/decidim/decidim/pull/4198)
- **decidim-core**: Hide weird flash message [\#4235](https://github.com/decidim/decidim/pull/4235)
- **decidim-core**: Fix newsletter subscription checkbox always being unchecked [\#4238](https://github.com/decidim/decidim/pull/4238)
- **decidim-core**: Thread safe locale switching [\#4237](https://github.com/decidim/decidim/pull/4237)

**Removed**:

Expand Down
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case someone checks this code and wonders (like me) where are we changing the user locale, we currently do this here:

def create
enforce_permission_to :create, :locales
current_user.update!(locale: params["locale"]) if current_user && params["locale"] && available_locales.include?(params["locale"])
redirect_to referer_with_new_locale
end

end

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