Ruby on Rails 3.2 Mailer, localize mail subject field

你说的曾经没有我的故事 提交于 2019-12-18 04:54:11

问题


I'm currently writing a mailer in RoR 3.2 that would send out mails that should be localized based on a users' language. I managed to render the correct localized views but I'm having some difficulties with some fields that require changing the locale (like the subject). I've already read some posts that are against changing the locale before sending the email. The users have many different languages and that would mean changing my locale every time a user is sent an email.

I know that it would be possible to change the locale, send the email, change back the locale. This doesn't feel like the rails way. Is there a correct way of doing this?

Here's a snippet:

class AuthMailer < ActionMailer::Base
  add_template_helper(ApplicationHelper)
  default :from => PREDEF_MAIL_ADDRESSES::System[:general]

  [...]

  def invite(address, token, locale)
    @token = token
    @locale = locale
    @url = url_for(:controller => "signup_requests", :action => "new", :token => token.key, :locale => locale)

    mail(:subject => "Invitation", :to => address) do |format|
      format.html { render ("invite."+locale) }
      format.text { render ("invite."+locale) }
    end
  end

  [...]
end

My views

auth_mailer
  invite.en.html.erb
  invite.en.text.erb
  invite.it.html.erb
  invite.it.text.erb
  ...

In short, in this case, I'd like to localize the :subject using the @locale, but not by running: I18n.locale = locale


回答1:


It is OK to change the global locale temporarily. There is a handy I18n.with_locale method for that. Also ActionMailer automatically translates a subject.

class AuthMailer
  def invite(address, token, locale)
    @token = token
    @locale = locale
    @url = url_for(:controller => "signup_requests", :action => "new", :token => token.key, :locale => locale)

    I18n.with_locale(locale) do
      mail(:to => address)
    end
  end
end

In the locale:

en:
  auth_mailer:
    invite:
      subject: Invitation



回答2:


Rails 4 way:

# config/locales/en.yml
en:
  user_mailer:
    welcome:
      subject: 'Hello, %{username}'

# app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
  def welcome(user)
    mail(subject: default_i18n_subject(username: user.name))
  end
end

default_i18n_subject - Translates the subject using Rails I18n class under [mailer_scope, action_name] scope. If it does not find a translation for the subject under the specified scope it will default to a humanized version of the action_name. If the subject has interpolations, you can pass them through the interpolations parameter.




回答3:


You should be able to pass a locale when you call I18n like so:

mail(:subject => I18n.t("app.invite.subject", :locale => locale), :to => address) do |format|
  format.html { render ("invite."+locale) }
  format.text { render ("invite."+locale) }
end

Remember that the locale variable needs to be a symbol.



来源:https://stackoverflow.com/questions/11097855/ruby-on-rails-3-2-mailer-localize-mail-subject-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!