In this third article in a four-part series on adding support for multiple languages to a Ruby-on-Rails ecomerce application, you will learn how to add a translation to a site, edit the translation, and more. This article is excerpted from chapter 10 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
We added the :title, :blurb parameters to the translates method. With these, we specify that the title and blurb database columns should be translated. The translation process for ActiveRecord models works similarly to the view layer, so you need to change app/views/catalog/_books.rhtml as highlighted here:
<dl id="books"> <% for book in @books %> <dt><%= link_to book.title.t, :action => "show", :id => book %></dt> <% for author in book.authors %> <dd><%= author.last_name %>, <%= author.first_name %></dd> <% end %> <dd> <strong> <%= add_book_link("+", book) %> </strong> </dd> <dd><%= pluralize(book.page_count, "page") %></dd> <dd>Price: $<%= sprintf("%.2f", book.price) %></dd> <dd><small><%= 'Publisher'.t %>: <%= book.publisher.name %></small></dd> <% end %> </dl>
Note You shouldn’t need to add a call to the translates method in your view, as in our example: book.title.t . Adding the translates method call to your model should be enough, but at the time of writing, Globalize didn’t translate the view, or even add a translation record to the database, until we also changed the view. This is probably a bug and should be fixed.