]> cat aescling's git repositories - mastodon.git/commitdiff
Localize with i18n for Devise::FailureApp (#2309)
authoralpaca-tc <alpaca-tc@alpaca.tc>
Tue, 25 Apr 2017 13:06:41 +0000 (22:06 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Tue, 25 Apr 2017 13:06:41 +0000 (15:06 +0200)
This PR fixes I18n.locale for rake middlewares. Mastodon uses Devise that depends on Warden.
Warden::Manager can be found in rake middleware. It is outside of the controller.

In the case of authentication failed, warden calls throw(:warden). At the time Warden::Manager
delegates request to failure_app to generate response and flash[:alert] after catching it.
Unfortunately, I18n.locale is already reset then because I18n.with_locale is enabled only
inside the controller. If we used I18n.locale=, Devise::FailureApp could get the current locale.

app/controllers/application_controller.rb
app/controllers/concerns/localized.rb
spec/controllers/auth/sessions_controller_spec.rb

index 2918954576d44b0ae95c68e0777f451085058d85..0c324762df87c85c4c992e62786588c33a963f4b 100644 (file)
@@ -115,8 +115,7 @@ class ApplicationController < ActionController::Base
   end
 
   def respond_with_error(code)
-    set_locale do
-      render "errors/#{code}", layout: 'error', status: code
-    end
+    set_locale
+    render "errors/#{code}", layout: 'error', status: code
   end
 end
index d9a7a7227bd05bbfcc49ad9b22abbcb6b4b6e72f..44762df2af2f6b04a82ecc67fddf297e4acc431d 100644 (file)
@@ -4,25 +4,16 @@ module Localized
   extend ActiveSupport::Concern
 
   included do
-    around_action :set_locale
+    before_action :set_locale
   end
 
   private
 
   def set_locale
-    locale = default_locale
-
-    if user_signed_in?
-      begin
-        locale = current_user.try(:locale) || default_locale
-      rescue I18n::InvalidLocale
-        locale = default_locale
-      end
-    end
-
-    I18n.with_locale(locale) do
-      yield
-    end
+    I18n.locale = default_locale
+    I18n.locale = current_user.locale if user_signed_in?
+  rescue I18n::InvalidLocale
+    I18n.locale = default_locale
   end
 
   def default_locale
index 0ec0b8f2f3438ec6220163be5de7feb171d6a735..2b68b4c5a74e6715068641e857206024c5f388a6 100644 (file)
@@ -49,6 +49,20 @@ RSpec.describe Auth::SessionsController, type: :controller do
           expect(controller.current_user).to be_nil
         end
       end
+
+      context 'using an unconfirmed password' do
+        before do
+          request.headers['Accept-Language'] = accept_language
+          post :create, params: { user: { email: unconfirmed_user.email, password: unconfirmed_user.password } }
+        end
+
+        let(:unconfirmed_user) { user.tap { |u| u.update!(confirmed_at: nil) } }
+        let(:accept_language) { 'fr' }
+
+        it 'shows a translated login error' do
+          expect(flash[:alert]).to eq(I18n.t('devise.failure.unconfirmed', locale: accept_language))
+        end
+      end
     end
 
     context 'using two-factor authentication' do