我正在使用Ruby on Rails 3.1,我想知道如何正确处理与邮件程序相关的国际化。那就是......
...在我的app/views/users/mailer/_custom.html.erb
文件中:
...
<%= @test_email.to_s %>
...
...在我的app/mailers/users/mailer.rb
文件中:
class Users::Mailer < ActionMailer::Base
def custom(user)
...
@test_email = t('test_email')
# I also tried to use '@test_email = I18n.t('test_email')'
end
end
通过使用上面的代码,@test_email.to_s
文本在发送的电子邮件中不会显示(似乎是空白的),而I18n gem似乎无法正确完成应该完成的操作。 如何解决问题,以便在“目标”电子邮件正文中正确显示区域设置消息?
更新
您在答案中发布的代码对我不起作用。我还尝试将以下代码添加到我的config/locales/defaults/en.yml
文件中:
en:
hello: Test text!!!
Test text!!!
仍然没有显示在已发送的电子邮件中(它是空白的)。
答案 0 :(得分:1)
以下是工作代码的示例(我使用Rails 3.1.0和ruby 1.9.3p0(2011-10-30修订版33570)):
# Fragment of mailer
def test_locale
@test_email = I18n.t('hello')
mail(:to => "<your_email_goes_here>", :subject => "test")
end
# Contents of test_locale.html.erb
String, retrieved from I18n: <%= @test_email %>
为了测试,我使用了Rails控制台:
MyMailer.test_locale.deliver
所以你需要检查一下:
更新:尝试在本地化文件中用引号括起字符串:
en:
hello: "Some text here"