翻译simple_form错误消息

时间:2013-11-22 09:27:01

标签: ruby-on-rails ruby simple-form

我有这段代码显示本地化文本:

  <%= f.input :full_name, :label => _('registration.form.label.name') + ':', :required => false -%>
  <%= f.input :company, :label => _('registration.form.label.company') + ':', :required => false -%>

如您所见,标签正在使用键和外部系统进行翻译。标签部分正常工作。但是,不会翻译错误消息(“不能为空”等)。最简单的方法是通过将错误消息包装在_()标记中来自定义错误消息,但我似乎无法找到它的位置。

翻译错误消息最简单的方法是什么?最好使用当前的翻译系统。

2 个答案:

答案 0 :(得分:0)

You could simply add the error messages to the activerecord yaml

en:
  activerecord:
    models:
      mymodel:
        attributes:
          name:
            too_short: "%{attribute} is too short."

This way you can also reference the attribute name within your yaml.

Of course, ideally you would place both the label and the validation in the simple_form i18n yaml, but I was not able to figure this out. Any suggestions for this are welcome.

答案 1 :(得分:-1)

解决方案是创建自定义错误消息。 (例如:Customize error message with simple_form

validates_length_of :name, :minimum => 5, :message => "blah blah blah"

并像这样使用它们:

<%= f.input :name, :error_html => { :id => "name_error"} %>