Rails 3 I18n changes

As we already know, in Rails 3 all dependencies will be bundled. This means you will be able to use latest I18n version which includes several improvements by itself.

Besides that, a couple things changed on Rails 3 I18n, some features were added and others were deprecated. This post is a quick walkthrough it:

1) Error messages defaults

With the addition of ActiveModel, it’s easy to add I18n behavior to any object by simply including ActiveModel::Translation and ActiveModel::Validations. As side effect, if we followed the same translation pattern as in Rails 2.3, each ORM would have to specify its default error messages at #{ORM.name}.errors.messages. This means that if you are using two ORMs in your app, you would have to translate the same messages twice.

To handle this situation, error messages now has errors.attributes and errors.messages as fallbacks in case a message cannot be found for an specific ORM. Notice that this also allows you to customize a message for an specific attribute used by models in different ORMs. So if you have several models with title attribute, you can customize the blank message for them all by simply placing it at errors.attributes.title.blank.

2) Attributes default

In the same line as above, you can now specify default attributes across different models and ORMs as well. For example, if you are exposing created_at and updated_at in your views, until Rails 2.3 you needed to specify the same translation for each model. In Rails 3, you can simply do:

en:
  attributes:
    created_at: "Created at"
    updated_at: "Updated at"

3) f.submit

f.submit in forms now can be called without arguments and it will inspect the form object to set the proper label. Imagine the following form:

<% form_for @post do |f| %>
  <%= f.submit %>
<% end %>

If @post is a new record, it will use “Create Post” as submit button label, otherwise, it uses “Update Post”. You can customize those labels with I18n under the following keys:

en:
  helpers:
    submit:
      create: "Create a {{model}}"
      update: "Confirm changes to {{model}}"

Nonetheless, it also searches for keys under the object name key as well:

en:
  helpers:
    submit:
      post:
        create: "Add {{model}}"

4) Labels with I18n

Labels were also updated to use I18n. Imagine the following form:

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

It will search for a label value at helpers.label.post.title, falling back to the value returned by Post.human_attribute_name(:title). This is useful in case you want more personalized labels instead of just “Title”.

5) Deprecation

Besides those improvements, I18n has two deprecations: the first is the key support.select should now be helpers.select and the other is that AV error messages are now agnostic, so activerecord.errors.template should now be errors.template.

That’s all, enjoy!

  • Pingback: On the way to Rails 3 - a link list : Railslove

  • Pingback: Caffeine Driven Development » Blog Archive » L33t Links #76

  • ABT

    “AV error messages are not agnostic”

    I have the feeling you meant “not gnostic”, but I can’t be sure because the whole ‘gnostic’/’agnostic’ usage is ambiguous (and really out of place), I think ‘specific’/’general’ or ‘coupled’/’decoupled’ is far more better.

    Good work on Rails 3, thanks.

  • ABT

    “AV error messages are not agnostic”

    I have the feeling you meant “not gnostic”, but I can’t be sure because the whole ‘gnostic’/’agnostic’ usage is ambiguous (and really out of place), I think ‘specific’/’general’ or ‘coupled’/’decoupled’ is far more better.

    Good work on Rails 3, thanks.

  • José Valim

    Thanks ABT. It was in fact a typo, it was supposed to “are now agnostic” instead of “are not agnostic”. :)

  • José Valim

    Thanks ABT. It was in fact a typo, it was supposed to “are now agnostic” instead of “are not agnostic”. :)

  • Pingback: Rails 3.0 Beta: 36 Links and Resources To Get You Going

  • Pingback: Rails 3.0 Beta - 36 Links e artigos para você começar

  • http://www.fallenfest.com Jon Atack

    Very helpful, thanks José.

  • http://www.fallenfest.com Jon Atack

    Very helpful, thanks José.