自第 3 版以来,上述所有方法都不能真正起作用,以翻译主题和内容,并确保将语言环境重置回原始语言……所以我做了以下事情(所有邮件程序都继承自该类:
class ResourceMailer < ActionMailer::Base
def mail(headers={}, &block)
I18n.locale = mail_locale
super
ensure
reset_locale
end
def i18n_subject(options = {})
I18n.locale = mail_locale
mailer_scope = self.class.mailer_name.gsub('/', '.')
I18n.t(:subject, options.merge(:scope => [mailer_scope, action_name], :default => action_name.humanize))
ensure
reset_locale
end
def set_locale(locale)
@mail_locale = locale
end
protected
def mail_locale
@mail_locale || I18n.locale
end
def reset_locale
I18n.locale = I18n.default_locale
end
end
您只需要在调用 mail() 方法之前设置语言环境:
set_locale @user.locale
您可以使用 i18n_subject 方法来确定当前路径的范围,从而使所有内容都结构化:
mail(:subject => i18n_subject(:name => @user.name)