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).
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 end
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') Locale.set('de-DE') book.reload 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.
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.