Home arrow Ruby-on-Rails arrow Page 3 - 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 - Implementing the Delete Translation User Story
(Page 3 of 5 )

We have one user story left to implement: the Delete Translation user story, which is triggered by clicking the Delete link (see Figure 10-5).

Add the following code for the Delete Translation user story to the controller ( app/ controllers/admin/translate_controller.rb ):

  def destroy
    render :update do |page|

This uses the ViewTranslation model to find and delete the specified translation from the database. Like the two actions used by the in-place editor, this action is called asynchronously. After the action has deleted the translation, it uses an .rjs script to remove the tr element having the specified id from the calling page. This deletes, for example, the <tr id="row_1">...</tr> HTML element, including the contents, from the view. Recall that the .rjs template, introduced in Chapter 5, generates a JavaScript snippet that is executed within the browser.

Now that everything is in place, you can start translating your views and Book models. We’ll first show you how to translate the view.

Translating the View and the Book Model

In this section, we’ll show you how to translate the book catalog we created in Chapter 4. We’ll translate the view and then the Book model.

Translating the View

To prepare a view for translation, you need to change the text to a Ruby string, surround it with <%= %>, and append a call to .translate (or its alias .t ) to the string. For example, to prepare the text “Type a question for help” for translation from the base language to other languages, change it to this:  

<%= 'Type a question for help'.t %>

Note  Globalize inserts a new record into the database (the base language text) for each string that is appended with a .t (or .translate ) method call when that particular code is executed. Code that is located inside an if statement that doesn’t get executed won’t be inserted into the database.

After changing the text, you should switch from the base locale to the locale to which you want to translate. In the Emporium application, this is done by appending ?locale=iso_code to the URL.

To recap, the required steps for translating a view are as follows:

  1. Change the base language text into Ruby strings and add a call to the Globalize translate method. For example, Page title to <%= 'Page title'.t %> .
  2. Change to the locale you want to translate the page into by accessing any given page with the correct locale parameter. For example, use ?locale=sv to change the locale to Swedish. 
  3. Access the page you want to translate. This inserts a row into the globalize_translations table for each translatable Ruby string on the page. 
  4. Access the translation view ( http://localhost:3000/admin/translate?locale=sv ) and add a translation for the text by changing the text column of the rows that were inserted in the previous step. 
  5. Repeat steps 2 through 4 for each locale and page. You could also write a Ruby script that copies empty translations to all locales. That way, you wouldn’t need to repeat the steps for each locale.

If you have a complex page or controller that uses a lot of if/else branches, it might be difficult to insert all of the text into the database with the preceding steps. In this case, it is better to manually enter the translations using the translation view we just created. Another option is to write a script that extracts all strings that end with .t ( .translate ) and then inserts them into the database. But there are still validation error messages that wouldn’t be translated. This is because error messages are generated by the Rails framework, and they are shown only when there’s a validation error. To prepare error messages for translation, you could submit an empty form that generates validation errors. This will insert the error messages to the database, so you can translate them.

We are now ready to start modifying the views. First, change the view app/views/catalog/_ books.rhtml partial as shown here:

<dl id="books">
  <% for book in @books %>
    <dt><%= link_to book.title, :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 %>

Notice that the only thing we changed is the text Publisher , which now reads <%= 'Publisher'.t %> .

Next, change the pagination links in app/views/catalog/index.rhtml as follows:

<%= link_to 'Previous page'.t, { :page => @book_pages.current.previous } ?
if @book_pages.current.previous % >
<%= link_to 'Next page'.t, { :page => @book_pages.current.next } ?
if @book_pages.current.next % >

Notice that we added .t , the abbreviation for translate , to the previous and next links. This is all you need to do to enable translation for a piece of text in the view.

In the next section, we’ll show you how to translate the book title, which is retrieved from the database. This doesn’t require any changes in the view; you need to modify only the ActiveRecord model.

Now let’s continue by translating the text into Swedish. Access localhost:3000/catalog?locale=sv in your browser. (You specify the locale for Swedish by appending ?locale=sv to the URL.) By accessing the page, we made Globalize insert two new rows: one for “Publisher” and one for “Next page,” which we already translated to Swedish. Navigate to the next page by clicking the link. Now you should have one row for “Previous page” also. The three new rows can be found by executing the following query:

select * from globalize_translations g order by id desc limit 0,100

Note  The next and previous links are visible only if there are more than ten rows in the database. This means you need to have at least ten books in the database, because Globalize inserts the rows only when 'Next page'.t and 'Previous page'.t are executed. If you haven’t already added the fixture files, download them from the Source Code/Downloads section of www.apress.com. Once you have copied the fixtures to the test/fixtures directory, you can load them into the development database by executing rake db:fixtures:load FIXTURES=authors,publishers,books,authors_books .

You can start translating the text by accessing the translation page we created earlier. Open your browser and go to localhost:3000/admin/translate?locale=sv . You should see the translation page shown in Figure 10-6. At the top of the translation list, you can see the two rows that are highlighted with the text “Click here to translate.”

Figure 10-6.  Translation view showing two new rows

Click the first row’s “Click here to translate” text. This opens an in-place-editor, as shown in Figure 10-7. Type in the translation “Förlag,” and then click OK. This stores the translation in the database. If you go back to the catalog page, or any other page using the same text, you will see that the text is now displayed in Swedish.

Figure 10-7.  Translation page showing three in-place editors

Note  If the Scandinavian characters åäö are not displayed correctly, you need to specify a character encoding when starting WEBrick. This is done by specifying the charset parameter at startup: script/server webrick --charset=utf-8 .

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-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials