Localization with Globalize

Installation and configuration

Installing Globalize is pretty straightforward. See the documentation online:

http://www.globalize-rails.org/wiki/

Do not forget to:

  • include the following lines at the end of your rails_apps/yourAppName/config/environment.rb file:
include Globalize
Locale.set_base_language 'nl-NL'

(or any other base language if you're not in the Netherlands, of course).

  • before any filters, add the following line in your file rails_apps/yourAppName/app/controllers/application.rb:
Locale.set("nl-NL")

I have made it a habit to store my translation strings in app/helpers/translations.rb

Using Globalize to output plain string dates

Using Globalize is also pretty easy. It just melds in with the various date_select functions already present in Rails. These methods allow you to output select boxes where you can select and edit dates.

Outputting a plain formatted date string, however, is not really documented. Here is how you do it:

  <p><label for="account_logdate">Laatst ingelogd</label><br/>  
  <span id="account_logdate"><%= @account.logdate.localize("%d %B %Y") %></p>

This outputs:

Laatst ingelogd
18 januari 2006

Personal Tools