Skip to content

Commit a6c844a

Browse files
Gargronhiyuki2578
authored andcommitted
Change locale detection to run once per session (mastodon#8657)
Fix mastodon#6462
1 parent 5a781ee commit a6c844a

4 files changed

Lines changed: 17 additions & 21 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,7 @@ def cache_collection(raw, klass)
138138
def respond_with_error(code)
139139
respond_to do |format|
140140
format.any { head code }
141-
142-
format.html do
143-
set_locale
144-
render "errors/#{code}", layout: 'error', status: code
145-
end
141+
format.html { render "errors/#{code}", layout: 'error', status: code }
146142
end
147143
end
148144

app/controllers/concerns/localized.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ module Localized
44
extend ActiveSupport::Concern
55

66
included do
7-
before_action :set_locale
7+
around_action :set_locale
88
end
99

1010
private
1111

1212
def set_locale
13-
I18n.locale = default_locale
14-
I18n.locale = current_user.locale if user_signed_in?
15-
rescue I18n::InvalidLocale
16-
I18n.locale = default_locale
13+
locale = current_user.locale if respond_to?(:user_signed_in?) && user_signed_in?
14+
locale ||= session[:locale] ||= default_locale
15+
locale = default_locale unless I18n.available_locales.include?(locale.to_sym)
16+
17+
I18n.with_locale(locale) do
18+
yield
19+
end
1720
end
1821

1922
def default_locale

config/application.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class Application < Rails::Application
114114
Doorkeeper::AuthorizationsController.layout 'modal'
115115
Doorkeeper::AuthorizedApplicationsController.layout 'admin'
116116
Doorkeeper::Application.send :include, ApplicationExtension
117+
Devise::FailureApp.send :include, AbstractController::Callbacks
118+
Devise::FailureApp.send :include, HttpAcceptLanguage::EasyAccess
119+
Devise::FailureApp.send :include, Localized
117120
end
118121
end
119122
end

spec/controllers/concerns/localized_spec.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@
77
include Localized
88

99
def success
10-
head 200
10+
render plain: I18n.locale, status: 200
1111
end
1212
end
1313

14-
around do |example|
15-
current_locale = I18n.locale
16-
example.run
17-
I18n.locale = current_locale
18-
end
19-
2014
before do
2115
routes.draw { get 'success' => 'anonymous#success' }
2216
end
@@ -25,19 +19,19 @@ def success
2519
it 'sets available and preferred language' do
2620
request.headers['Accept-Language'] = 'ca-ES, fa'
2721
get 'success'
28-
expect(I18n.locale).to eq :fa
22+
expect(response.body).to eq 'fa'
2923
end
3024

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

3731
it 'sets default locale if none of available languages are compatible' do
3832
request.headers['Accept-Language'] = ''
3933
get 'success'
40-
expect(I18n.locale).to eq :en
34+
expect(response.body).to eq 'en'
4135
end
4236
end
4337

@@ -48,7 +42,7 @@ def success
4842
sign_in(user)
4943
get 'success'
5044

51-
expect(I18n.locale).to eq :ca
45+
expect(response.body).to eq 'ca'
5246
end
5347
end
5448

0 commit comments

Comments
 (0)