Browse Source

Change locale detection to run once per session (#8657)

Fix #6462
pull/4/head
Eugen Rochko 4 years ago
committed by GitHub
parent
commit
bd1545de5e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 21 deletions
  1. +1
    -5
      app/controllers/application_controller.rb
  2. +8
    -5
      app/controllers/concerns/localized.rb
  3. +3
    -0
      config/application.rb
  4. +5
    -11
      spec/controllers/concerns/localized_spec.rb

+ 1
- 5
app/controllers/application_controller.rb View File

@ -138,11 +138,7 @@ class ApplicationController < ActionController::Base
def respond_with_error(code) def respond_with_error(code)
respond_to do |format| respond_to do |format|
format.any { head code } 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
end end

+ 8
- 5
app/controllers/concerns/localized.rb View File

@ -4,16 +4,19 @@ module Localized
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
before_action :set_locale
around_action :set_locale
end end
private private
def set_locale 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 end
def default_locale def default_locale

+ 3
- 0
config/application.rb View File

@ -114,6 +114,9 @@ module Mastodon
Doorkeeper::AuthorizationsController.layout 'modal' Doorkeeper::AuthorizationsController.layout 'modal'
Doorkeeper::AuthorizedApplicationsController.layout 'admin' Doorkeeper::AuthorizedApplicationsController.layout 'admin'
Doorkeeper::Application.send :include, ApplicationExtension 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 end
end end

+ 5
- 11
spec/controllers/concerns/localized_spec.rb View File

@ -7,16 +7,10 @@ describe ApplicationController, type: :controller do
include Localized include Localized
def success def success
head 200
render plain: I18n.locale, status: 200
end end
end end
around do |example|
current_locale = I18n.locale
example.run
I18n.locale = current_locale
end
before do before do
routes.draw { get 'success' => 'anonymous#success' } routes.draw { get 'success' => 'anonymous#success' }
end end
@ -25,19 +19,19 @@ describe ApplicationController, type: :controller do
it 'sets available and preferred language' do it 'sets available and preferred language' do
request.headers['Accept-Language'] = 'ca-ES, fa' request.headers['Accept-Language'] = 'ca-ES, fa'
get 'success' get 'success'
expect(I18n.locale).to eq :fa
expect(response.body).to eq 'fa'
end end
it 'sets available and compatible language if none of available languages are preferred' do it 'sets available and compatible language if none of available languages are preferred' do
request.headers['Accept-Language'] = 'fa-IR' request.headers['Accept-Language'] = 'fa-IR'
get 'success' get 'success'
expect(I18n.locale).to eq :fa
expect(response.body).to eq 'fa'
end end
it 'sets default locale if none of available languages are compatible' do it 'sets default locale if none of available languages are compatible' do
request.headers['Accept-Language'] = '' request.headers['Accept-Language'] = ''
get 'success' get 'success'
expect(I18n.locale).to eq :en
expect(response.body).to eq 'en'
end end
end end
@ -48,7 +42,7 @@ describe ApplicationController, type: :controller do
sign_in(user) sign_in(user)
get 'success' get 'success'
expect(I18n.locale).to eq :ca
expect(response.body).to eq 'ca'
end end
end end

Loading…
Cancel
Save