使用i18n gem与邮件的麻烦

时间:2012-01-07 09:04:38

标签: ruby-on-rails ruby ruby-on-rails-3 email internationalization

我正在使用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似乎无法正确完成应该完成的操作。 如何解决问题,以便在“目标”电子邮件正文中正确显示区域设置消息?


@volodymyr

更新

您在答案中发布的代码对我不起作用。我还尝试将以下代码添加到我的config/locales/defaults/en.yml文件中:

en:
  hello: Test text!!!

Test text!!!仍然没有显示在已发送的电子邮件中(它是空白的)。

1 个答案:

答案 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

所以你需要检查一下:

  • Erb模板文件名与邮件程序的名称相匹配
  • 本地化文件中有相应的记录
  • 邮件程序方法和模板文件中的变量名称匹配
  • “views”中的文件夹名称与邮件名称
  • 相匹配

更新:尝试在本地化文件中用引号括起字符串:

en:
    hello: "Some text here"