【问题标题】:Trouble on using the i18n gem with the mailer将 i18n gem 与邮件程序一起使用时遇到问题
【发布时间】:2012-01-07 09:04:38
【问题描述】:

我正在使用 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!!! 仍然没有显示在发送的电子邮件中(它是空白的)。

【问题讨论】:

  • 尝试用引号括起来。例如:你好:“测试文本”
  • @volodymyr - 问题与引号无关...请阅读答案下方的评论以获取更多信息。

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


【解决方案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 模板文件名与邮件程序的方法名匹配
  • 本地化文件中有对应记录
  • 邮件方法中的变量名与模板文件匹配
  • “视图”中的文件夹名称与邮件名称匹配

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

en:
    hello: "Some text here"

【讨论】:

  • 也许我找到了解决方案。我正在使用延迟作业 gem,在我重新启动作业后(rake jobs:work 在开发模式下)它开始工作。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
  • 2013-03-20
  • 2021-07-15
  • 2013-06-19
  • 2014-01-29
  • 1970-01-01
相关资源
最近更新 更多