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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change locale detection to run once per session #8657

Merged
merged 1 commit into from
Jul 21, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ def cache_collection(raw, klass)
def respond_with_error(code)
respond_to do |format|
format.any { head code }

format.html do
set_locale
render "errors/#{code}", layout: 'error', status: code
end
format.html { render "errors/#{code}", layout: 'error', status: code }
end
end

Expand Down
13 changes: 8 additions & 5 deletions app/controllers/concerns/localized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ module Localized
extend ActiveSupport::Concern

included do
before_action :set_locale
around_action :set_locale
end

private

def set_locale
I18n.locale = default_locale
I18n.locale = current_user.locale if user_signed_in?
rescue I18n::InvalidLocale
I18n.locale = default_locale
locale = current_user.locale if respond_to?(:user_signed_in?) && user_signed_in?
locale ||= session[:locale] ||= default_locale
locale = default_locale unless I18n.available_locales.include?(locale.to_sym)

I18n.with_locale(locale) do
yield
end
end

def default_locale
Expand Down
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class Application < Rails::Application
Doorkeeper::AuthorizationsController.layout 'modal'
Doorkeeper::AuthorizedApplicationsController.layout 'admin'
Doorkeeper::Application.send :include, ApplicationExtension
Devise::FailureApp.send :include, AbstractController::Callbacks
Devise::FailureApp.send :include, HttpAcceptLanguage::EasyAccess
Devise::FailureApp.send :include, Localized
end
end
end
16 changes: 5 additions & 11 deletions spec/controllers/concerns/localized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@
include Localized

def success
head 200
render plain: I18n.locale, status: 200
end
end

around do |example|
current_locale = I18n.locale
example.run
I18n.locale = current_locale
end

before do
routes.draw { get 'success' => 'anonymous#success' }
end
Expand All @@ -25,19 +19,19 @@ def success
it 'sets available and preferred language' do
request.headers['Accept-Language'] = 'ca-ES, fa'
get 'success'
expect(I18n.locale).to eq :fa
expect(response.body).to eq 'fa'
end

it 'sets available and compatible language if none of available languages are preferred' do
request.headers['Accept-Language'] = 'fa-IR'
get 'success'
expect(I18n.locale).to eq :fa
expect(response.body).to eq 'fa'
end

it 'sets default locale if none of available languages are compatible' do
request.headers['Accept-Language'] = ''
get 'success'
expect(I18n.locale).to eq :en
expect(response.body).to eq 'en'
end
end

Expand All @@ -48,7 +42,7 @@ def success
sign_in(user)
get 'success'

expect(I18n.locale).to eq :ca
expect(response.body).to eq 'ca'
end
end

Expand Down