翻译自定义错误消息 [英] Translating custom error messages

查看:125
本文介绍了翻译自定义错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form (using simple_form) which I want to implement support for translated error messages. All my translations appear with the exception of the error message.

我的客户模型是:

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)

有没有我想要的东西?

推荐答案

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

At first, you should use a Symbol with validates_presence_of. Don't translate it with I18n manually:

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

其次,将您的错误消息的翻译添加到您的区域设置文件中,如下所示:

Secondly, add translation for your error message to your locale file like this:

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

这篇关于翻译自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆