Merge pull request #4696 from consul/set_request_locale

Enable the `Rails/I18nLocaleAssignment` cop to scan all the application ruby files
This commit is contained in:
Senén Rodero
2022-06-13 18:43:35 +02:00
committed by GitHub
5 changed files with 23 additions and 16 deletions

View File

@@ -269,8 +269,8 @@ Rails/HttpStatus:
Rails/I18nLocaleAssignment:
Enabled: true
Exclude:
- "spec/spec_helper.rb"
Include:
- "**/*.rb"
Rails/InverseOf:
Enabled: true

View File

@@ -11,7 +11,7 @@ class ApplicationController < ActionController::Base
before_action :authenticate_http_basic, if: :http_basic_auth_site?
before_action :ensure_signup_complete
before_action :set_locale
around_action :switch_locale
before_action :track_email_campaign
before_action :set_return_url
@@ -40,14 +40,15 @@ class ApplicationController < ActionController::Base
end
end
def set_locale
I18n.locale = current_locale
def switch_locale(&action)
locale = current_locale
if current_user && current_user.locale != I18n.locale.to_s
current_user.update(locale: I18n.locale)
if current_user && current_user.locale != locale.to_s
current_user.update(locale: locale)
end
session[:locale] = I18n.locale
session[:locale] = locale
I18n.with_locale(locale, &action)
end
def current_locale

View File

@@ -4,7 +4,7 @@ class Management::BaseController < ActionController::Base
default_form_builder ConsulFormBuilder
before_action :verify_manager
before_action :set_locale
around_action :switch_locale
helper_method :managed_user
helper_method :current_user
@@ -38,14 +38,14 @@ class Management::BaseController < ActionController::Base
redirect_to management_document_verifications_path, alert: message
end
def set_locale
def switch_locale(&action)
if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym)
session[:locale] = params[:locale]
end
session[:locale] ||= I18n.default_locale
I18n.locale = session[:locale]
I18n.with_locale(session[:locale], &action)
end
def current_budget

View File

@@ -1,5 +1,6 @@
class SubscriptionsController < ApplicationController
before_action :set_user, :set_user_locale
before_action :set_user
around_action :set_user_locale
skip_authorization_check
def edit
@@ -29,10 +30,10 @@ class SubscriptionsController < ApplicationController
[:email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter]
end
def set_user_locale
def set_user_locale(&action)
if params[:locale].blank?
I18n.locale = I18n.available_locales.find { |locale| locale == @user.locale&.to_sym }
session[:locale] = I18n.locale
session[:locale] = I18n.available_locales.find { |locale| locale == @user.locale&.to_sym }
end
I18n.with_locale(session[:locale], &action)
end
end

View File

@@ -30,11 +30,16 @@ RSpec.configure do |config|
end
config.before do |example|
I18n.locale = :en
Globalize.set_fallbacks_to_all_available_locales
Setting["feature.user.skip_verification"] = nil
end
config.around do |example|
I18n.with_locale(:en) do
example.run
end
end
config.around(:each, :race_condition) do |example|
self.use_transactional_tests = false
example.run