加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 如何翻译ActiveRecord的完整错误消息?

发布时间:2020-12-17 03:45:59 所属栏目:百科 来源:网络整理
导读:如何翻译ActiveRecord完整错误消息? 例如,我想用西班牙语展示它们. 我创建了这个文件config / locales / es.yml: es: errors: attributes: email: blank: "El email no puede estar en blanco" 但是,当提交具有在线状态的表单时,验证是将该属性始终包含在
如何翻译ActiveRecord完整错误消息?
例如,我想用西班牙语展示它们.

我创建了这个文件config / locales / es.yml:

es:
  errors:
    attributes:
      email:
        blank: "El email no puede estar en blanco"

但是,当提交具有在线状态的表单时,验证是将该属性始终包含在消息的开头:

Email El email no puede estar en blanco

第一个“电子邮件”字样不是必需的.我怎么能摆脱它?

解决方法

好的,经过多次阅读并重新阅读官方的i18n指南后,我在5.2.2节中发现:

ActiveModel::Errors#full_messages prepends the attribute name to the
error message using a separator that will be looked up from
errors.format (and which defaults to “%{attribute} %{message}”).

所以,解决方案是配置格式,如下所示:

es:
  errors:
    format: "%{message}"
    attributes:
      email:
        blank: "El email no puede estar en blanco"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读