How do I translate labels on error messages when label is set to false?

86 views
Skip to first unread message

Leandro França

unread,
Mar 16, 2015, 2:19:19 PM3/16/15
to plataformate...@googlegroups.com
Hi there,

I'm using only placeholders on my devise login form, but I'd like to translate the label part on error messages, using I18n locale files.

Is this possible?

Here's a sample from my form

<%= f.input :email, required: false, placeholder: 'Informe seu e-mail', autofocus: true,label: false %><br>


Thanks in advance,
Leandro

Leandro França

unread,
Mar 16, 2015, 2:20:43 PM3/16/15
to plataformate...@googlegroups.com
for a better example, I would like to show "senha" instead of password on error message for:

<%= f.input :password, required: false,placeholder: 'Informe sua senha',label: false %><br>

Carlos Antonio da Silva

unread,
Mar 16, 2015, 2:24:40 PM3/16/15
to plataformate...@googlegroups.com
The error component uses the object's errors, so it should pick up the human attribute name from i18n in case you're using something like the full error component. I think the normal error only shows the message itself, there's no label involved.

Which error you're seeing? Have you changed any simple form config in your app?

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Leandro França

unread,
Mar 16, 2015, 2:33:34 PM3/16/15
to plataformate...@googlegroups.com
Hey Carlos,

I'm using full error on simple_form.rb

     b.use :full_error, wrap_with: { tag: :span, class: :error }

On simple_form.pt-BR.yml I have:

pt-BR:
  simple_form:
    "yes": 'Sim'
    "no": 'Não'
    required:
      text: 'required'
      mark: '*'
    error_notification:
      default_message: "Por favor, verifique os erros encontrados:"
    labels:
       defaults:
         password: 'Password'

 The error message I'm seeing is 
"Password não pode ficar em branco"



--
You received this message because you are subscribed to a topic in the Google Groups "SimpleForm" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/plataformatec-simpleform/oBfUmEyXCLE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to plataformatec-simp...@googlegroups.com.

Carlos Antonio da Silva

unread,
Mar 16, 2015, 2:41:35 PM3/16/15
to plataformate...@googlegroups.com
Right, I see. The error message does not use the `label` translation, but the human attribute name that comes from the errors translation on the model. You need to add the activerecord.attributes.your_model_name.password key to your locale with the attribute name you want, or in case you want a generic attribute you can use activerecord.attributes.attributes.password.

Internally Simple Form just calls your_model.errors.full_messages_for(:password), you can try that in the console.

Hope that helps.

Leandro França

unread,
Mar 16, 2015, 3:01:01 PM3/16/15
to plataformate...@googlegroups.com
Carlos,

That solved the problem. Thanks a lot.

For reference, I just added on locales/pt-BR.yml:

pt-BR:
  activerecord:
    attributes:
      user:
        password: "Senha"

Regards 

Carlos Antonio da Silva

unread,
Mar 16, 2015, 3:02:23 PM3/16/15
to plataformate...@googlegroups.com
Awesome, no problem :)
Reply all
Reply to author
Forward
0 new messages