README

Path: vendor/plugins/gettext_localize/README
Last Update: Thu Nov 29 12:14:52 +0100 2007

Gettext Localize

Authors:Ramon Salvadó <rsalvado@gnuine.com>, Miquel Ibero , Pau Colomer

RubyForge_Project_Page

Any questions/comments can be addressed to Ramon Salvadó <rsalvado@gnuine.com>

This plugin is intended to help you localize your rails app using Gettext, it doesn‘t try to cover localization of models. It works with Rails 2.0 and has been tested with the following languages: catalan, spanish, english.

Installation

Needs to have the gettext gem installed:

sudo gem install gettext

To install the plugin:

script/plugin install svn://rubyforge.org/var/svn/gettextlocalize/trunk/gettext_localize

Features

  • works with rails 1.2 and loads the correct ruby-gettext
  • loads ruby-gettext with meaningful defaults, that can be overwritten at different levels
  • simplifies translation with rake tasks
  • set the locale in the controllers by query param, session, cookie or browser header
  • translates dates and date helpers
  • translates currencies and other text helpers
  • translation entirely done with gettext, no per language ruby code

Configuration

  1. install gettext: gem install gettext
  2. localize your app texts adding the gettext function _("translate this")
  3. generate the application pot files: rake gettext:updatepo creates a po/app.pot
  4. translate the messages creating a directory for every language: po/lang1/app.po
  5. generate the mo files : rake gettext:makemo
  6. define the default language (by default takes your $LANG environment variable) in your environment.rb

Needed things

  • ruby gettext: gem install gettext

Priorities

locale:

  1. locale set in controller with set_locale(lang)
  2. locale set in environment with GettextLocalize::default_locale = lang
  3. locale set in environment variable $LANG
  4. locale set in this plugin init.rb with GettextLocalize::fallback_locale = lang

country:

  1. country set in controller with set_country(country)
  2. country part of locale
  3. country set in environment with GettextLocalize::default_country = country
  4. country set in this plugins init.rb with GettextLocalize::fallback_country = country

textdomain:

  1. textdomain set in plugin using plugin_bindtextdomain(textdomain), uses loadpath RAILS_ROOT/vendor/plugins/$plugin/locale
  2. textdomain set in controller with init_gettext(textdomain), uses loadpath RAILS_ROOT/locale
  3. textdomain set in environment.rb with GettextLocalize::default_textdomain = textdomain
  4. app_name set in environment.rb with GettextLocalize::app_name = app
  5. name of the directory the rails app is in

Before filters in controller

Possible locale find methods are:

param
looks for a GET or POST parameter named by default ‘lang’ or with the name specified
cookie
looks for a cookie named by default ‘lang‘
session
looks for a session variable named by default ‘lang‘
header
looks for a HTTP user header named by default HTTP_ACCEPT_LANGUAGES, sent by the web browser

Don‘t put set_locale(lang) directly in the controller, because it will set the locale in all the aplication, use always before_filter.

Sample Configuration in environment.rb

 if defined? GettextLocalize
   GettextLocalize::app_name = 'app'
   GettextLocalize::app_version = '1.2.0'
   GettextLocalize::default_locale = 'ca_ES'
   GettextLocalize::default_methods = [:param, :session, :header]
 end

Samples of controller filters

Tries to read the session from a param named ‘locale’, then from a session var named ‘locale’ and the from the header, then falls back to fallback_locale.

 before_filter(:except=>"feed"){|c| c.set_locale_by [:param, :session, :header], 'locale' }

Sets the locale fixed but only in the controller.

 before_filter{|c| c.set_default_locale 'ca_ES' }

Sets the country fixed in the controller.

 before_filter{|c| c.set_default_country 'es' }

Sets the locale by cookie and if not by session.

 before_filter :set_locale_by_session
 before_filter :set_locale_by_cookie

Times and Dates

The classes Time, Date and DateTime are overwritten to localize to_s and strftime

strftime

The formats are localized using gettext, so to add a locale you only need to translate po/gettext_localize.pot the standard way

The following formats are overwritten:

%A
full day of the week
%a
abbreviated day of the week
%B
full name of the month
%b
abbreviated name of the month

The following formats are added:

%V
day of the month and monthname, like ‘11 of September‘
%v
day of the month with zeros and monthname, like ‘04 of July‘

The %V format is added because there are languages that vary the structure depending of the monthname, for example in Catalan you say "5 de Desembre" and "11 d‘Abril".

to_s

Rails adds standart to_s formats. You can say Time.now.to_s(:db) or 1.week.ago.to_s(:short). These formats are also translated using gettext. We suppose :db and :rfc822 are not translatable since they are used for data saving.

ActiveRecord is overwritten to save Dates using strftime("%Y-%m-%d") and not to_s wich can be localized and would crash.

Helpers

The countries.yml YAML file defines per country currency and date order options.

To set the year-month-day order depending on the locale:

  • DateHelper::date_select
  • DateHelper::select_date
  • DateHelper::datetime_select
  • DateHelper::select_datetime
  • InstanceTag::to_datetime_select_tag

To set the currency depending on the country:

  • NumberHelper::number_to_currency

To set the connector depending on the locale:

  • Array::to_sentence

Acknowledgments:

  • Jester for Localization simplified plugin from which we borrowed some code/concepts.
  • Masao Mutoh for ruby-gettext.

[Validate]