Rails i18n and yml structure for form labels

27,086

Solution 1

I think I found another solution here.

My app was version 2.3.5. I've now changed it to 2.3.8 and <%= f.label :username %> now uses the translation in:

dk:
  activerecord:
    attributes:
      user:
        username:

I found the hint in this ticket:

https://rails.lighthouseapp.com/projects/8994/tickets/745-form-label-should-use-i18n

Solution 2

In Rails 3.1 that is a little bit changed.

<% form_for @post do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title %>
  <%= f.submit %>
<% end %>

en:
  helpers:
    label:
      post:
        title: 'Customized title'

Solution 3

That's because the label method you are calling is not the one from ActionView::Helpers::FormHelper but is in fact the label_tag method from ActionView::Helpers::FormTagHelper. The form_for method is rewriting the code in the given block by adding _tag to the used form helpers. So you're not looking at the documentation for the right method!

I've not yet used that method, as sometimes the label for a field can be different from multiple forms using the same model, so I've written my own helper.

Share:
27,086

Related videos on Youtube

rhardih
Author by

rhardih

Developer with an interest in web and mobile technologies. éncoder.dk/blog

Updated on July 09, 2022

Comments

  • rhardih
    rhardih almost 2 years

    According to the ActionView documentation. Quote:

    The text of label will default to the attribute name unless a translation is found in the current I18n locale (through views.labels.<modelname>.<attribute>) or you specify it explicitly.

    I have a "user" model and a registration form. Here's a snippet of the relevant part:

    <% form_for(@user) do |f| %>
        ...
        <p>
        <%= f.label :username %>
        <%= f.text_field :username, :class => 'full_width' %>
        </p>
        ...
    <% end %>
    

    Dots hide unimportant code.

    As I understand the documentation, if I provide a translation in my locale file, in this case :dk, my dk.yml looking like so:

    dk:
        views:
            labels:
                user:
                    username:
                        "blahblah"
    

    Rails should translate the label text and insert "blahblah" instead of "Username".

    This is not happening, so I must have missed something. Any help appreciated.

  • rhardih
    rhardih almost 14 years
    Thank you, I didn't know that. The form_for and label methods are both on ActionView::Helpers::FormHelper doc page, so I just assumed they worked together like that. I've changed my code to: <%= label :user, :username %> With the same yml file, but I still do not get the translation. Does it matter that the label call is within the form_for block?
  • Nicolas Buduroi
    Nicolas Buduroi almost 14 years
    To be honest, I've never got that feature to work. Being curious, I've just looked at Rails code and I think this is an old feature and that the documentation hasn't been fixed: content = (text.blank? ? nil : text.to_s) || method_name.humanize at line 473 of form_helper.rb
  • astjohn
    astjohn over 12 years
    Thanks for this. Is there a release note or something that I can look at to learn more about this format?
  • simonnordberg
    simonnordberg about 10 years
    The description for this format is available in the ActionView::Helpers::FormHelper.label documentation.
  • mariowise
    mariowise about 9 years
    In that case you should use the t method specifying the scope present in your .yml translation file. <%= t :title, scope: [:helpers, :label, :post] %>.
  • mzrnsh
    mzrnsh about 7 years
    What if you want to have a label like 'Enter your first name'? I don't feel setting attribute to a phrase like that is clean, will probably lead to some problems in some specific cases. See Voldy's answer for a correct way to customize a form label. You can even use both approaches together, but Voldy's aproach will override this one on your form, and it should
  • Pieter Jongsma
    Pieter Jongsma about 7 years
    If the helpers: path is not available, i18n will fallback to the activerecord: path. Very clear when using the excellent i18n-debug gem.
  • W.M.
    W.M. over 6 years
    @PieterJongsma, thank you for this precious suggestions. Very helpful.