On the other hand, it is perfectly possible for an application to require internationalization
but not require any content translation at all. Applications with geographically
localized clients (such as web-based applications with many small clients
located in different countries) may have a need for interface translation, and they certainly
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 translation.
This is best done in environment.rb, after Rails is loaded but before the
application serves requests:
Locale.set_base_language 'en-US'
2. Provide a before_filter on your localized actions that sets the locale. This part
is application-specific; the locale can be provided in the URL, a cookie, the session,
or a user preference in the database. Here is a simple before_filter that
sets the locale based on a locale request parameter:
class ApplicationController < ActionController::Base
before_filter :set_locale
protected
def set_locale
258 | Chapter 8: i18n and L10n
Locale.set(params[:locale] || Locale.base_language.code)
end
end
After this filter is set up, you can specify the locale for any action within the application
by adding the appropriate parameter to the URL (http://example.
Pages:
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402