Home arrow Ruby-on-Rails arrow Page 4 - Multiple Language Support for an Ecommerce Application

Multiple Language Support for an Ecommerce Application

If you're building an ecommerce application that will do business in more than one country, you will probably need to support multiple languages. Indeed, you might face this problem even if you're doing business in just one state in the US, such as Florida or Texas. This four-part article series will show you how to add support for multiple languages to a Ruby-on-Rails ecomerce application. It 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 / 4
June 25, 2010
  1. · Multiple Language Support for an Ecommerce Application
  2. · Using the Globalize Plugin
  3. · Localizing with Globalize
  4. · Localizing the Model

print this article

Multiple Language Support for an Ecommerce Application - Localizing the Model
(Page 4 of 4 )

You can translate text stored in your database by adding a call to the translates method to your model, followed by the column names that should be translatable, as follows:

  class Book < ActiveRecord::Base
translates :title, :blurb

This tells Globalize that you want to be able to translate the title and blurb columns in the books table. After you have translated the text and stored the translations in your database, you can execute, for example, the following code with the script/console command to print out the text in different languages:

book = Book.find_by_name('Bodo Bear on the Farm')
puts book.title

--------------------------------------------Bodo Bär auf dem Bauernhof

In this example, the base language is English. The first line finds the record using the base language, and then sets the locale to German and prints out the book title in German. Notice that you don’t need to add the .translates method call.

As with view translations, Globalize works behind the scenes and modifies the SQL query on the fly by intercepting calls to the find methods (except for the find_by_sql method). The modified SQL query uses a left outer join to pull in the translations from the globalize_translations table, as follows (this is just part of the query):

...LEFT OUTER JOIN globalize_translations AS...

This can potentially have a serious impact on the performance, so watch the MySQL slow query log for problems. Note that Globalize doesn’t cache the model translations, in contrast to the view translations, which are cached.

Note  The MySQL slow query log is your friend when you’re having problems with slow queries. Enable it and watch the log for queries that can be optimized. For more information about the MySQL slow query log, see http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html .

Globalize also has a piggybacking feature that allows you to retrieve translations for associated objects in the same query. To enable this feature, use the include_translated option:

Book.find(:all, :include_translated => Product)

Please check back next week for the continuation of this article.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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