Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Rails L10n | 257

Note that unlike the Money library, which handles basic currency exchange, you
will need to handle any exchange calculations yourself when using Globalize. The
Globalize library only takes care of formatting, not the semantics of the currency.


Model Translation


Model translation is the most complicated, least standardized, and most application-
dependent part of localization.Model translationorcontent translationrefers to the
translation of the content stored within an application for multiple locales. This kind
of localization is the most difficult because it is application-specific. Often, the trans-
lation cycles are much tighter than when only the application itself is localized.


The premier example of an application with a need for content translation is a web
content-management system. Often, web pages and documents need to be main-
tained in parallel in many languages. One of the key selling points of enterprise
content-management systems over their open source brethren is rock-solid support
for managing translations and their workflow.


In these applications, workflow is the key application-dependent factor. The actual
technical practice of selecting and displaying the proper content for a user’s locale is
dead simple compared to the work of coordinating to make sure the content is trans-
lated and available.


On the other hand, it is perfectly possible for an application to require international-
ization but not require any content translation at all. Applications with geographi-
cally localized clients (such as web-based applications with many small clients
located in different countries) may have a need for interface translation, and they cer-
tainly need to handle UTF-8 text properly, but they may not need to interchange
data between different languages. Again, the need is highly application-dependent.


Globalize provides facilities for model translation, closely integrated with
ActiveRecord. There are a few easy steps to follow after installing Globalize:



  1. Set a base language, which is the default locale assumed for data without a trans-
    lation. This is best done inenvironment.rb, after Rails is loaded but before the
    application serves requests:
    Locale.set_base_language 'en-US'

  2. Provide abefore_filteron your localized actions that sets the locale. This part
    is application-specific; the locale can be provided in the URL, a cookie, the ses-
    sion, or a user preference in the database. Here is a simplebefore_filterthat
    sets the locale based on alocale request parameter:
    class ApplicationController < ActionController::Base
    before_filter :set_locale


protected

def set_locale
Free download pdf