如何使用收件人的语言环境在Rails 3中发送电子邮件? [英] How can I send emails in Rails 3 using the recipient's locale?

查看:95
本文介绍了如何使用收件人的语言环境在Rails 3中发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用收件人的语言环境在邮递员中发送邮件。我有数据库中每个用户的首选语言环境。请注意,这与当前语言环境(I18n.locale)不同,只要当前用户不必是收件人。因此,困难的事情是在不更改I18n.locale的情况下在其他语言环境中使用邮件程序:

How can I send mails in a mailer using the recipient's locale. I have the preferred locale for each user in the database. Notice this is different from the current locale (I18n.locale), as long as the current user doesn't have to be the recipient. So the difficult thing is to use the mailer in a different locale without changing I18n.locale:

def new_follower(user, follower)
  @follower = follower
  @user = user
  mail :to=>@user.email
end

在邮件前使用I18n.locale = @ user.profile.locale:to => ...将解决邮件问题,但会改变其余部分的行为

Using I18n.locale = @user.profile.locale before mail :to=>... would solve the mailer issue, but would change the behaviour in the rest of the thread.

推荐答案

我相信最好的方法是使用出色的方法 I18n.with_locale ,它允许您临时更改块中的 I18n.locale ,您可以像这样使用它:

I believe the best way to do this is with the great method I18n.with_locale, it allows you to temporarily change the I18n.locale inside a block, you can use it like this:

def new_follower(user, follower)
  @follower = follower
  @user = user
  I18n.with_locale(@user.profile.locale) do
    mail to: @user.email
  end
end

它将更改语言环境,仅发送电子邮件,在b之后立即更改

And it'll change the locale just to send the email, immediately changing back after the block ends.

来源: http://www.rubydoc.info/docs/rails/2.3.8/I18n.with_locale

这篇关于如何使用收件人的语言环境在Rails 3中发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆