バージョン 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)