How to simplify the translation call on i18n?

1

See this example below for a missing translation:

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

To resolve this translation I must exactly follow 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 that I can put the front of the validation something like message: I18n.t('errors.messages.cpf') , but having to do this insertion in any validation would leave the code well polluted.

    
asked by anonymous 20.03.2017 / 13:07

3 answers

2

Since ActiveRecord is valid, it already has its call hierarchy in the I18n file, ie you should follow its convention, as you have already demonstrated in your question. To change this behavior you would have to overwrite it with ActiveRecord .

    
21.03.2017 / 12:53
2

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

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

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

Or, you could create a validity in 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 the management of translations, there is a tool called "Localeapp", link ... With this GEM link you can download the easily created translations for your project.

Hope it helps! Hugs!

    
18.04.2017 / 18:49
1

You can place this file: link in the folder config/locales/

Then add this line to the file config/application.rb :

config.i18n.default_locale = :'pt-BR'
    
21.03.2017 / 13:13