{"id":669,"date":"2010-02-02T11:23:41","date_gmt":"2010-02-02T13:23:41","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=669"},"modified":"2010-02-05T13:16:59","modified_gmt":"2010-02-05T15:16:59","slug":"rails-3-i18n-changes","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2010\/02\/rails-3-i18n-changes\/","title":{"rendered":"Rails 3 I18n changes"},"content":{"rendered":"

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<\/a> by itself.<\/p>\n

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:<\/p>\n

1) Error messages defaults<\/h3>\n

With the addition of ActiveModel, it’s easy to add I18n behavior to any object by simply including ActiveModel::Translation<\/code> and ActiveModel::Validations<\/code>. 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<\/code>. This means that if you are using two ORMs in your app, you would have to translate the same messages twice.<\/p>\n

To handle this situation, error messages now has errors.attributes<\/code> and errors.messages<\/code> 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<\/code>.<\/p>\n

2) Attributes default<\/h3>\n

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<\/code> and updated_at<\/code> in your views, until Rails 2.3 you needed to specify the same translation for each model. In Rails 3, you can simply do:<\/p>\n

en:\r\n  attributes:\r\n    created_at: \"Created at\"\r\n    updated_at: \"Updated at\"<\/pre>\n

3) f.submit<\/h3>\n

f.submit<\/code> in forms now can be called without arguments and it will inspect the form object to set the proper label. Imagine the following form:<\/p>\n

\r\n<% form_for @post do |f| %>\r\n  <%= f.submit %>\r\n<% end %>\r\n<\/pre>\n

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:<\/p>\n

en:\r\n  helpers:\r\n    submit:\r\n      create: \"Create a {{model}}\"\r\n      update: \"Confirm changes to {{model}}\"<\/pre>\n

Nonetheless, it also searches for keys under the object name key as well:<\/p>\n

en:\r\n  helpers:\r\n    submit:\r\n      post:\r\n        create: \"Add {{model}}\"<\/pre>\n

4) Labels with I18n<\/h3>\n

Labels were also updated to use I18n. Imagine the following form:<\/p>\n

\r\n<% form_for @post do |f| %>\r\n  <%= f.label :title %>\r\n  <%= f.text_field :title %>\r\n  <%= f.submit %>\r\n<% end %>\r\n<\/pre>\n

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

5) Deprecation<\/h3>\n

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

That’s all, enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"

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) … \u00bb<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[58,7],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/669"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=669"}],"version-history":[{"count":11,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/669\/revisions"}],"predecessor-version":[{"id":680,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/669\/revisions\/680"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}