【问题标题】:Translating custom error messages翻译自定义错误消息
【发布时间】:2012-10-18 12:54:19
【问题描述】:

我有一个表单(使用 simple_form),我想实现对已翻译错误消息的支持。我的所有翻译都会出现,但错误消息除外。

我的客户模型是:

class Customer < ActiveRecord::Base
  attr_accessible :name, :phone, :email, :contact_method

  validates_presence_of :phone, :email, :contact_method, :message => I18n.t(:required)
end

我的fr.yml 文件

fr:
  name: 'Nom'
  phone: 'Téléphone'
  email: 'Courriel'
  contact_method: 'Méthode de contact'
  required: 'Requis'

我的表格如下:

= simple_form_for @customer do |f|
  = f.input :name, label: t(:name)
  = f.input :phone, label: t(:phone)
  = f.input :email, label: t(:email)

我有什么遗漏吗?

【问题讨论】:

    标签: error-handling internationalization ruby-on-rails-3.2 simple-form


    【解决方案1】:

    首先,您应该使用Symbolvalidates_presence_of。不要用 I18n 手动翻译:

    validates_presence_of :phone, :email, :contact_method, :message => :required
    

    其次,将错误消息的翻译添加到您的语言环境文件中,如下所示:

    activerecord:
      errors:
        models:
          customer:
            required: 'Requis'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2016-11-03
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多