How to simplify the translation call on i18n?

Asked

Viewed 817 times

1

See this example below of a missing translation:

{
  "errors": [
    "Email translation missing: pt-BR.activerecord.errors.models.artemis/user.attributes.email.taken"
  ]
}

To solve this translation I must follow exactly the structure generated above:

activerecord:
  errors:
    models:
      artemis/user:
        attributes:
          cpf:
            taken: 'Aqui vem a tradução'

My question is whether there is a more automated and simple way to simplify this translation? See below:

errors:
  messages:
    cpf: 'Aqui vem a tradução'

I know I can put the validation before something like message: I18n.t('errors.messages.cpf'), but having to do this in every validation would leave the code well polluted.

3 answers

2

Just as Luiz commented, this naming rule is a Rails convention.

There is a way, that would create a validate in the model by calling a function, and add a "custom" error to the error that happened. Ex:

validates_presence_of :email, :message => "mensagem de erro com outra tradução"

Or also, could create an expiration on the model, as follows:

class MeuModel < ActiveRecord::Base
   validate :email

   def parent_released
     errors.add(:email, "mensagem de erro com outra tradução") if 
     deu_erro?
   end
end

As a solution to facilitate translation management, there is a tool called "Localeapp", https://www.localeapp.com... With this gem https://github.com/Locale/localeapp Voce can easily download translations created for your project.

I hope it helps! Hug!

2


Like the ActiveRecord is who validates he already has his call hierarchy in the I18n, that is to follow your convention, as you have already demonstrated in your question. To change this behavior you would have to overwrite this in the ActiveRecord.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.