Home arrow Ruby-on-Rails arrow Page 4 - Translating a Site for Multiple Language Support

Translating a Site for Multiple Language Support

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

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
July 01, 2010
  1. · Translating a Site for Multiple Language Support
  2. · Implementing the Edit Translation User Story
  3. · Implementing the Delete Translation User Story
  4. · Translating the Model
  5. · Localizing Dates, Numbers, and Currency

print this article

Translating a Site for Multiple Language Support - Translating the Model
(Page 4 of 5 )

You can prepare an ActiveRecord model for translation by using the translates method. Change app/models/book.rb as follows:  

class Book < ActiveRecord::Base
  has_and_belongs_to_many :authors
  belongs_to :publisher

  translates :title, :blurb
  acts_as_ferret :fields => [:title, :authornames]

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 %>
          <%= add_book_link("+", book) %>
      <dd><%= pluralize(book.page_count, "page") %></dd>
      <dd>Price: $<%= sprintf("%.2f", book.price) %></dd>
     <dd><small><%= 'Publisher'.t %>: <%= book.publisher.name %></small></dd>
<% end %>

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.

Now if you access http://localhost:3000/admin/translate , you should see the book titles in the list, as shown in Figure 10-8.

Figure 10-8.  Translation view showing book titles

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials