Skip to content

Commit ecb1981

Browse files
committed
Template lookup now respect default locale and I18n fallbacks.
Given the following templates: mailer/demo.html.erb mailer/demo.en.html.erb mailer/demo.pt.html.erb Before this change for a locale that doesn't have its related file the `mailer/demo.html.erb` will be rendered even if `en` is the default locale. Now `mailer/demo.en.html.erb` has precedence over the file without locale. Also, it is possible to give a fallback. mailer/demo.pt.html.erb mailer/demo.pt-BR.html.erb So if the locale is `pt-PT`, `mailer/demo.pt.html.erb` will be rendered given the right I18n fallback configuration. Fixes #11884.
1 parent d9b080b commit ecb1981

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed

actionmailer/CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1+
* Template lookup now respect default locale and I18n fallbacks.
2+
3+
Given the following templates:
4+
5+
mailer/demo.html.erb
6+
mailer/demo.en.html.erb
7+
mailer/demo.pt.html.erb
8+
9+
Before this change for a locale that doesn't have its related file the `mailer/demo.html.erb` will
10+
be rendered even if `en` is the default locale.
11+
12+
Now `mailer/demo.en.html.erb` has precedence over the file without locale.
13+
14+
Also, it is possible to give a fallback.
15+
16+
mailer/demo.pt.html.erb
17+
mailer/demo.pt-BR.html.erb
18+
19+
So if the locale is `pt-PT`, `mailer/demo.pt.html.erb` will be rendered given the right I18n
20+
fallback configuration.
21+
22+
*Rafael Mendonça França*
23+
124
Please check [4-2-stable](https://github.com/rails/rails/blob/4-2-stable/actionmailer/CHANGELOG.md) for previous changes.

actionmailer/lib/action_mailer/base.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,6 @@ def process(method_name, *args) #:nodoc:
586586
}
587587

588588
ActiveSupport::Notifications.instrument("process.action_mailer", payload) do
589-
lookup_context.skip_default_locale!
590-
591589
super
592590
@_message = NullMail.new unless @_mail_was_called
593591
end

actionmailer/test/base_test.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,35 @@ def welcome
353353
assert_equal("text/plain", email.parts[0].mime_type)
354354
assert_equal("Implicit with locale PL TEXT", email.parts[0].body.encoded)
355355
assert_equal("text/html", email.parts[1].mime_type)
356-
assert_equal("Implicit with locale HTML", email.parts[1].body.encoded)
356+
assert_equal("Implicit with locale EN HTML", email.parts[1].body.encoded)
357357
end
358358
end
359359

360+
test "implicit multipart with fallback locale" do
361+
fallback_backend = Class.new(I18n::Backend::Simple) do
362+
include I18n::Backend::Fallbacks
363+
end
364+
365+
begin
366+
backend = I18n.backend
367+
I18n.backend = fallback_backend.new
368+
I18n.fallbacks[:"de-AT"] = [:de]
369+
370+
swap I18n, locale: 'de-AT' do
371+
email = BaseMailer.implicit_with_locale
372+
assert_equal(2, email.parts.size)
373+
assert_equal("multipart/alternative", email.mime_type)
374+
assert_equal("text/plain", email.parts[0].mime_type)
375+
assert_equal("Implicit with locale DE-AT TEXT", email.parts[0].body.encoded)
376+
assert_equal("text/html", email.parts[1].mime_type)
377+
assert_equal("Implicit with locale DE HTML", email.parts[1].body.encoded)
378+
end
379+
ensure
380+
I18n.backend = backend
381+
end
382+
end
383+
384+
360385
test "implicit multipart with several view paths uses the first one with template" do
361386
old = BaseMailer.view_paths
362387
begin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implicit with locale DE-AT TEXT
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implicit with locale DE HTML

actionview/lib/action_view/lookup_context.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ def normalize_name(name, prefixes) #:nodoc:
191191

192192
def initialize(view_paths, details = {}, prefixes = [])
193193
@details, @details_key = {}, nil
194-
@skip_default_locale = false
195194
@cache = true
196195
@prefixes = prefixes
197196
@rendered_format = nil
@@ -213,12 +212,6 @@ def formats=(values)
213212
super(values)
214213
end
215214

216-
# Do not use the default locale on template lookup.
217-
def skip_default_locale!
218-
@skip_default_locale = true
219-
self.locale = nil
220-
end
221-
222215
# Override locale to return a symbol instead of array.
223216
def locale
224217
@details[:locale].first
@@ -233,7 +226,7 @@ def locale=(value)
233226
config.locale = value
234227
end
235228

236-
super(@skip_default_locale ? I18n.locale : default_locale)
229+
super(default_locale)
237230
end
238231

239232
# Uses the first format in the formats array for layout lookup.

0 commit comments

Comments
 (0)