【问题标题】:Rails i18n - Can't change the format of the errorsRails i18n - 无法更改错误的格式
【发布时间】:2017-05-05 11:52:38
【问题描述】:

在我的 Rails 应用程序中,我有以下翻译

de:
  activerecord:
    errors:
      models:
        applicant:
          attributes:
            name:
              blank: "Bitte nenne uns Deinen %{attribute}"

但是当我检查模型上的错误时,我收到以下消息:

申请人姓名 Bitte nenne uns Deinen Name

消息开头怎么会有“申请人姓名”?

我怎样才能删除它?

【问题讨论】:

    标签: ruby-on-rails internationalization rails-i18n


    【解决方案1】:

    您必须使用full_messages 来显示错误。像这样的:

    applicant.errors.full_messages
    #=> ["Applicant name Bitte nenne uns Deinen Name"]
    

    改成

    applicant.errors.messages
    #=> ["Bitte nenne uns Deinen Name"]
    

    注意: full_messages 将在错误消息之前附加属性名称。这就是为什么您在消息中附加了Applicant name

    解决方案2

    如果您想更改full_messages,请尝试更改

    en:
      errors:
        format: "%{attribute} %{message}"
    

    en:
      errors:
        format: "%{message}"
    

    【讨论】:

    • 我认为您在解决方案 #2 中忘记了 activerecord:... 但这不会改变输出中的任何内容。这是我的代码:hastebin.com/icamazepig.pl
    • 我的错,你是对的。没有activerecord: 它可以工作。谢谢!
    猜你喜欢
    • 2012-09-22
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    相关资源
    最近更新 更多