From 963d11a000437241c958f8af9b3de118e100f133 Mon Sep 17 00:00:00 2001
From: Thierry Joyal <thierry.joyal@gmail.com>
Date: Tue, 16 Apr 2019 18:54:51 +0000
Subject: [Rails::MailersController] Do not leak I18n global setting changes

---
 railties/lib/rails/mailers_controller.rb          | 7 +++++--
 railties/test/application/mailer_previews_test.rb | 7 +++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

(limited to 'railties')

diff --git a/railties/lib/rails/mailers_controller.rb b/railties/lib/rails/mailers_controller.rb
index 5cffa52860..4a1942790b 100644
--- a/railties/lib/rails/mailers_controller.rb
+++ b/railties/lib/rails/mailers_controller.rb
@@ -5,8 +5,9 @@ require "rails/application_controller"
 class Rails::MailersController < Rails::ApplicationController # :nodoc:
   prepend_view_path ActionDispatch::DebugView::RESCUES_TEMPLATE_PATH
 
+  around_action :set_locale, only: :preview
+  before_action :find_preview, only: :preview
   before_action :require_local!, unless: :show_previews?
-  before_action :find_preview, :set_locale, only: :preview
 
   helper_method :part_query, :locale_query
 
@@ -92,6 +93,8 @@ class Rails::MailersController < Rails::ApplicationController # :nodoc:
     end
 
     def set_locale
-      I18n.locale = params[:locale] || I18n.default_locale
+      I18n.with_locale(params[:locale] || I18n.default_locale) do
+        yield
+      end
     end
 end
diff --git a/railties/test/application/mailer_previews_test.rb b/railties/test/application/mailer_previews_test.rb
index fb84276b8a..fa9ed868c4 100644
--- a/railties/test/application/mailer_previews_test.rb
+++ b/railties/test/application/mailer_previews_test.rb
@@ -515,6 +515,13 @@ module ApplicationTests
       assert_match '<option selected value="locale=ja">ja', last_response.body
     end
 
+    test "preview does not leak I18n global setting changes" do
+      I18n.with_locale(:en) do
+        get "/rails/mailers/notifier/foo.txt?locale=ja"
+        assert_equal :en, I18n.locale
+      end
+    end
+
     test "mailer previews create correct links when loaded on a subdirectory" do
       mailer "notifier", <<-RUBY
         class Notifier < ActionMailer::Base
-- 
cgit v1.2.3