gpt4 book ai didi

ruby-on-rails - 将 i18n gem 与邮件一起使用时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:35:14 24 4
gpt4 key购买 nike

我正在使用 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!!!

测试文本!!! 仍然没有显示在发送的电子邮件中(它是空白的)。

最佳答案

这是工作代码的示例(我使用的是 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"

关于ruby-on-rails - 将 i18n gem 与邮件一起使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8768532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com