待翻译的文本在邮件中无法识别
Text to be translated not recognized in mailer
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 文档后,我的印象是,这是正确的做法。我做错了什么?
您必须注意语法:t("mailers.message_notifier.user_name")
而不是 t(mailers.message_notifier.user_Name)
。请注意,t
方法的参数必须是字符串,并且 uppercase/lowercase 在您定义翻译键时很重要。
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 文档后,我的印象是,这是正确的做法。我做错了什么?
您必须注意语法:t("mailers.message_notifier.user_name")
而不是 t(mailers.message_notifier.user_Name)
。请注意,t
方法的参数必须是字符串,并且 uppercase/lowercase 在您定义翻译键时很重要。