Permalink
Browse files

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.
  • Loading branch information...
rafaelfranca committed Dec 30, 2014
1 parent d9b080b commit ecb1981bfd3ffaca3a3efd110fc85380ece8191d
View
@@ -1 +1,24 @@
* 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.
*Rafael Mendonça França*
Please check [4-2-stable](https://github.com/rails/rails/blob/4-2-stable/actionmailer/CHANGELOG.md) for previous changes.
@@ -586,8 +586,6 @@ def process(method_name, *args) #:nodoc:
}
ActiveSupport::Notifications.instrument("process.action_mailer", payload) do
lookup_context.skip_default_locale!
super
@_message = NullMail.new unless @_mail_was_called
end
@@ -353,10 +353,35 @@ def welcome
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("Implicit with locale PL TEXT", email.parts[0].body.encoded)
assert_equal("text/html", email.parts[1].mime_type)
assert_equal("Implicit with locale HTML", email.parts[1].body.encoded)
assert_equal("Implicit with locale EN HTML", email.parts[1].body.encoded)
end
end
test "implicit multipart with fallback locale" do
fallback_backend = Class.new(I18n::Backend::Simple) do
include I18n::Backend::Fallbacks
end
begin
backend = I18n.backend
I18n.backend = fallback_backend.new
I18n.fallbacks[:"de-AT"] = [:de]
swap I18n, locale: 'de-AT' do
email = BaseMailer.implicit_with_locale
assert_equal(2, email.parts.size)
assert_equal("multipart/alternative", email.mime_type)
assert_equal("text/plain", email.parts[0].mime_type)
assert_equal("Implicit with locale DE-AT TEXT", email.parts[0].body.encoded)
assert_equal("text/html", email.parts[1].mime_type)
assert_equal("Implicit with locale DE HTML", email.parts[1].body.encoded)
end
ensure
I18n.backend = backend
end
end
test "implicit multipart with several view paths uses the first one with template" do
old = BaseMailer.view_paths
begin
@@ -0,0 +1 @@
Implicit with locale DE-AT TEXT
@@ -0,0 +1 @@
Implicit with locale DE HTML
@@ -191,7 +191,6 @@ def normalize_name(name, prefixes) #:nodoc:
def initialize(view_paths, details = {}, prefixes = [])
@details, @details_key = {}, nil
@skip_default_locale = false
@cache = true
@prefixes = prefixes
@rendered_format = nil
@@ -213,12 +212,6 @@ def formats=(values)
super(values)
end
# Do not use the default locale on template lookup.
def skip_default_locale!
@skip_default_locale = true
self.locale = nil
end
# Override locale to return a symbol instead of array.
def locale
@details[:locale].first
@@ -233,7 +226,7 @@ def locale=(value)
config.locale = value
end
super(@skip_default_locale ? I18n.locale : default_locale)
super(default_locale)
end
# Uses the first format in the formats array for layout lookup.

0 comments on commit ecb1981

Please sign in to comment.