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 - Using the Globalize Plugin (Page 2 of 4 )
Because of its close integration with Rails, the Globalize plugin provides a simple way of localizing the following in your application:
Dates, times, numbers, and currencies. For example, 100000000.00 localized to US standards is 100,000,000.0.
Text stored in a database. For example, the book The Old Man and the Sea is Der alte Mann und das Meer in German.
Text used in views. For example, the link Next Page is Nästa Sida in Swedish.
But that’s not all. Currently, Globalize comes with built-in data for 7599 languages and 239 countries, including pluralization rules and language names. Globalize knows, for example, that Tuesday is tisdag in Swedish. Pluralization is done by specifying both the singular and plural form for a translation; for example, “Displaying one book” and “Displaying many books.”
Note Globalize is not the only alternative for localizing Ruby on Rails applications. There’s also the GetText library, which allows you to externalize text into resource bundles and translate them into multiple languages. GetText doesn’t have as many features as Globalize and lacks support for things like date and currency formatting. For more information about GetText and other options, see http://wiki.rubyonrails.org/rails/pages/ InternationalizationComparison .
To install Globalize, execute the script/plugin script:
The script checks out the latest version of the plugin from the Subversion repository and installs it in the vendor/plugins directory. By default, the directory is named trunk . You should give the directory a more descriptive name. For example, we renamed it to globalize-for-1.1 .
Note At the time of writing, the latest version of Globalize didn’t support Rails 1.1. Instead of installing the latest version, we had to install the Rails 1.1 specific branch of Globalize with the following command: script/plugin install http://svn.globalize-rails.org/svn/globalize/globalize/ branches/for-1.1 .
Globalize extends the Rails framework’s ActionView, ActiveRecord, and ActionMailer modules, allowing it to provide the tools used for translating text. Globalize uses three database tables containing predefined data for thousands of languages and hundreds of countries:
globalize_countries : Holds countries and the country-specific localization rules, including ISO 3166 country code, date formats, and currency formats.
globalize_languages : Holds languages and the language-specific localization rules, including ISO 639 language code, English name, and native name.
globalize_translations : Holds translated text. The table is prepopulated with data for hundreds of languages. The tr_key column contains text in the base language. The text column is where the actual translation is stored.