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!

10 responses to “Rails 3 I18n changes”

  1. ABT says:

    “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.

  2. ABT says:

    “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.

  3. José Valim says:

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

  4. José Valim says:

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

  5. […] Rails 3 I18N Changes – José Valim looks at some internationalization (i18n) changes in Rails 3.0 over 2.x – along with basic examples. […]

  6. […] Mudanças no I18N do Rails 3 – José Valim mostra as alterações no sistema de internacionalização do (i18n) do Rails 3.0 em relação ao 2.x, mostrando alguns exemplos básicos. […]

  7. Jon Atack says:

    Very helpful, thanks José.

  8. Jon Atack says:

    Very helpful, thanks José.