要翻译的文本在邮件程序中无法识别

时间:2017-03-01 18:55:44

标签: ruby-on-rails rails-i18n

Rails 3.2

在我的config / locales / en.yml中,我有:

mailers:
  message_notifier:
    notify_admin_subject: ' added a comment to ticket '
    user_name: 'User: '
    email_address: 'Email: '
    company_name: 'Company: '
    added_following_comment: 'Added the following comment: '
    to_ticket: 'To Ticket: '

在我的app / mailers / message_notifier.rb中,我有以下内容:

def notify_admins_when_ticket_has_new_comment(comment)
  @global_admin = User.find("global-admin")
  email_to = @global_admin.email
  @user = User.find(comment.user_id)
  msg = [t(mailers.message_notifier.user_Name) + @user.first_name + ' ' + @user.last_name]
  msg << (t(mailers.message_notifier.email_address) + @user.email)
  msg << (t(mailers.message_notifier.company_name) + @user.company.name)
  msg << (t(mailers.message_notifier.added_following_comment) + @comment.content)
  @ticket = Ticket.find(@comment.commentable_id)
  msg << (t(mailers.message_notifier.to_ticket) + @ticket.number)
  plain_msg = ''
  html_msg = ''
  msg.each do |m|
    plain_msg = plain_msg + m + '\n'
    html_msg = html_msg + m + "<br>"
  end
  subject = @user.email + (t(mailers.message_notifier.notify_admin_subject)) + @ticket.number
  mail(to: email_to, subject: subject) do |format|
    format.text { render text: plain_msg.html_safe }
    format.html { render text: html_msg.html_safe }
  end
end

但是,我收到以下错误消息:

NameError (undefined local variable or method `mailers' for #<MessageNotifier:0x007faf5f540810>):
app/mailers/message_notifier.rb:24:in `notify_admins_when_ticket_has_new_comment'

通过阅读I18n API文档,我的印象是,这是正确的方法。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您必须注意语法:t("mailers.message_notifier.user_name")而不是t(mailers.message_notifier.user_Name)。请注意,t方法的参数需要是字符串,而大写/小写在定义翻译键时很重要。