Adventures with Ruby

Form Labels in Rails 2.2

View Comments

Shamelessly copy-pasted from the README file of the new plugin I released today:

Since form labels don’t use I18n in Rails 2.2 (I was too late in submitting the patch), we’d have to make due with a plugin.

Installation and configuration consists of 1 easy steps:

  1. Run: ./script/plugin install git://github.com/iain/i18n_label.git

Example

In your translation file:

en-US:
  activerecord:
    attributes:
      topic:
        name: 'A nice name'

In your view:

<% form_for Topic.new do |f| %>
  <%= f.label :name %>
<% end %>

The result is:

<label for="topic_name">A nice name</label>

For more information about where to put your translations, read my post about translating ActiveRecord.

Written by Iain Hecker

September 12th, 2008 at 9:55 pm

Posted in Uncategorized

  • Erik

    Excellent! Just what I was waiting for to finally release my first rails website. Thank you so much!

  • Yaroslav

    Great stuff, will vendorize it in http://github.com/yaroslav/russian if you dont have anything against :) Will use in all my 2.2 apps.

  • Yaroslav

    You have a typo in README.

    It is not

  • Yaroslav
  • http://iain.nl Iain Hecker

    Yaroslav, rookie mistake ;) fixed it! Of course you can use it however you like, I’d be pleased!

  • jerry

    Is i18n_label broken with rails 2.2.2?

  • http://iain.nl Iain Hecker

    No, not that I’m aware of… I am using it with success…

  • Josh

    I recently began using Rails, and this is the first plugin I’ve installed. It works great for me with Rails 2.2.2. Thanks!

  • http://railsmaniac.com Nat

    Using this from now %) Any information on when this will be merged with rails?
    Thanx anyway!

  • rng

    Still not in Rails because they’re discussing over capitalization…
    https://rails.lighthouseapp.com/projects/8994/tickets/745-form-label-should-use-i18n

    The plugin seems to ignore an empty/blank string in the label text:
    form.label(:myfield, ' ')
    should render
    <label ...> </label>
    but it renders
    <label ...>My Field</label> (or whatever the translation is)
    which differs from the normal FormHelper behavior.

blog comments powered by Disqus
Fork me on GitHub