CoderSpinoza
CoderSpinoza

Reputation: 2165

Rails Error messages with attributes regarding their genders

I am writing a translation for my rails application (pt.yml) that trasnlates all activerecord error messages in portuguese.

  activerecord:
    models:
      user: Usuário
      feedback: Contato
  attributes:
    user:
      password: "senha"
      phone_number: "celular"
      first_name: "nome"
      last_name: "sobrenome"
      password_confirmation: "Confirmação de senha"
    feedback:
      title: "Título"
      name: "Nome"
      message: "Mensagem"
    report:
      before_photo: "A foto do foco"
  errors:
    template:
      header:
        one: "1 erro impediu que o %{model.model_name.human} fosse salvo"
        other: "%{count} erros impediram que o %{model}  fosse salvo"
    messages:
      blank: "é obrigatório."
      confirmation: "não confere."
      empty: "é obrigatório."
      exclusion: "está reservado."
      invalid: "não é válido."
      taken: "já está em uso."
      too_long: "é muito longo. (mínimo de %{count} caracteres)"
      too_short: "é muito curto. (mínimo de %{count} caracteres)"
      not_a_number: "não é um número."

The problem is that an attribute can be masculine or feminine and error messages should be dependent on their gender. For example, "é obrigatório or é obrigatória". Is there a simple solution to handle gender of attributes when we produce error messages?

Upvotes: 1

Views: 273

Answers (2)

Mauricio Moraes
Mauricio Moraes

Reputation: 7373

This is more a workaround than a proper solution to the problem, but if you're not too concerned, that might be a good tip for latin derived languages.

I also struggled trying to make the adjective match the gender of the attribute (a problem that doesn't exist in english), then I found out I should always reference the field:

Female attribute -> The field must not be blank
Male attribute -> The field must be whatever

In that way, you get a DRYer approach.

Specifically in the case of portuguese, mine goes:

  # ActiveRecord
  errors:
    messages:
      empty: "O campo é obrigatório."
      blank: "é campo obrigatório." # another idea

Upvotes: 0

CoderSpinoza
CoderSpinoza

Reputation: 2165

I realized that I could use different error messages for each attribute of each models. config/locales/pt.yml would look something like this:

pt:
    activerecord:
        models:
            user: Usuário
        attributes:
            password: "Senha"
            first_name: "Nome"
        errors:
            models:
                user:
                    attributes:
                        password:
                            blank: "é obrigatória."
                        first_name:
                            blank: "é obrigatório."

So pt -> errors -> models -> #{model_name} -> attributes -> #{attribute_name} would specify the location for these custom gender-specific error messages for each attribute. Hope this could help anyone :)

Upvotes: 2

Related Questions