我正在使用 devise gem 并想翻译确认邮件。我已经有了自己的模板和重写的邮件程序方法:
class LocalizedDeviseMailer < Devise::Mailer
def confirmation_instructions(record, locale)
@locale = locale
super
end
end
因此,在我的模板中我可以做类似的事情:
I18n.locale = @locale
然后:
t("it.really.works")
但我不知道如何将带有语言环境的变量传递给邮件程序方法。最好的方法是什么?任何帮助将不胜感激。
最佳答案
Devise 正在“本地化”提供邮件模板的本地化。
查看设计源代码
https://github.com/plataformatec/devise/blob/master/lib/devise/mailers/helpers.rb 在此文件中解释了如何本地化主题(将添加到您的语言环境文件中)
# Setup a subject doing an I18n lookup. At first, it attemps to set a subject
# based on the current mapping:
#
# en:
# devise:
# mailer:
# confirmation_instructions:
# user_subject: '...'
#
这就是您需要像任何其他 html.erb 一样本地化的正文模板
取决于您的新用户是使用 http://yoursite/it/users/sign_up
还是 http://yoursite/en/users/sign_up
进行注册(如您通常会在本地化应用程序的路由中执行此操作)将发送良好的本地化主题和邮件(前者为意大利语,后者为英语)。
关于ruby-on-rails - 设计并本地化自己的邮件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311956/