简体   繁体   中英

How to edit error messages in devise

I need to modify the error messages of devise. I want to change the message "is Invalid" to "Es inválido" . The problem is that I have to go to change these messages in the gem. Can I overwrite these messages in the model User

Rails console

1.9.3-p547 :014 > user.save
 => false 
1.9.3-p547 :015 > user.errors
 => {:email=>["is invalid"], :password=>["is too short (minimum is 6 characters)"]} 
1.9.3-p547 :016 > 

User model

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

Devise Generates a devise.en.yml file in config/locales/ If you need your error messages to be in another language such as Spanish, replace your devise.en.yml with this file:

devise.es.yml

es:
  errors:
    messages:
      expired: "ha expirado, por favor pide una nueva"
      not_found: "no encontrado"
      already_confirmed: "ya fue confirmada. Intenta ingresar."
      not_locked: "no ha sido bloqueada"
      not_saved:
        one: "Ha habido 1 error:"
        other: "Han habido %{count} errores:"

  devise:
    failure:
      already_authenticated: 'Ya iniciaste sesión.'
      unauthenticated: 'Tienes que registrarte o iniciar sesión antes de continuar.'
      unconfirmed: 'Tienes que confirmar tu cuenta antes de continuar.'
      locked: 'Tu cuente está bloqueada.'
      invalid: 'Email o contraseña inválidos.'
      invalid_token: 'Token de autentificación inválido.'
      timeout: 'Tu sesión ha expirado. Inicia sesión nuevamente.'
      inactive: 'Tu cuenta aun no ha sido activada.'
    sessions:
      signed_in: 'Iniciaste sesión correctamente.'
      signed_out: 'Cerraste sesión correctamente.'
    passwords:
      send_instructions: 'Recibirás un email con instrucciones para reiniciar tu contraseña en unos minutos.'
      updated: 'Tu contraseña fue cambiada correctamente. Has iniciado sesión.'
      updated_not_active: 'Tu contraseña fue cambiada correctamente.'
      send_paranoid_instructions: "Si tu email existe en el sistema, recibirás instrucciones para recuperar tu contraseña en él"
    confirmations:
      send_instructions: 'Recibirás instrucciones para confirmar tu cuenta en tu email en unos minutos.'
      send_paranoid_instructions: 'Si tu email existe en el sistema, recibirás instrucciones para confirmar tu cuenta en tu email en unos minutos.'
      confirmed: 'Tu cuenta fue confirmada. Has iniciado sesión.'
    registrations:
      signed_up: 'Bienvenido! Te has registrado correctamente.'
      signed_up_but_unconfirmed: 'Te hemos enviado un email con instrucciones para que confirmes tu cuenta.'
      signed_up_but_inactive: 'Te has registrado correctamente, pero tu cuenta aun no ha sido activada.'
      signed_up_but_locked: 'Te has registrado correctamente, pero tu cuenta está bloqueada.'
      updated: 'Actualizaste tu cuenta correctamente.'
      update_needs_confirmation: "Actualizaste tu cuenta correctamente, pero tenemos que revalidar tu email. Revisa tu correo para confirmar la dirección."
      destroyed: 'Adiós, tu cuenta ha sido eliminada. Esperamos verte de vuelta pronto!'
    unlocks:
      send_instructions: 'Recibirás un email con instrucciones para desbloquear tu cuenta en unos minutos'
      unlocked: 'Tu cuenta ha sido desbloqueada. Inicia sesión para continuar.'
      send_paranoid_instructions: 'Si tu cuenta existe, recibirás instrucciones para desbloquear tu cuenta en unos minutos'
    omniauth_callbacks:
      success: 'Te autentificaste correctamente con tu cuenta de %{kind}.'
      failure: 'No pudimos autentificar tu cuenta de %{kind} por la siguiente razón: %{reason}.'
    mailer:
      confirmation_instructions:
        subject: 'Instrucciones de confirmación'
      reset_password_instructions:
        subject: 'Instrucciones de cambio de contraseña'
      unlock_instructions:
        subject: 'Instrucciones de desbloqueo'

UPDATE

es:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              blank: "El email no puede estar vacio"

This isn't Devise-specific. You can localize any of your ActiveRecord error messages in your config/locales/*.yml files.

In this case, you could, in config/local/es.yml , add something like the following:

es:
  activerecord:
    errors
      models
        user
          attributes:
            email:
              invalid: "Es inválido"

Rails' localization is extremely configurable. There is a lot more information in the guides .

You can do the devise specific translations using the devise.es.yml in your config/locales folder

List of all the different translations is given here in official devise wiki

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM