Ruby-Locale and Ruby on Rails 2.2 sample

[source code]

The values from Rails

VariablesValues
HTTP_ACCEPT_LANGUAGE en-us,en;q=0.5
HTTP_ACCEPT_CHARSET ISO-8859-1,utf-8;q=0.7,*;q=0.7
QUERY_STRING
params[:lang]ja_JP
cookies[:lang]

Get the current locale

I18n.candidates(with no option)

Requested Locales order by the priority. This method is added by Ruby-Locale for Ruby on Rails.

[#<Locale::Tag::Common: ja_JP>, #<Locale::Tag::Common: ja>, #<Locale::Tag::Common: en>]

I18n.locale (with inspect)

I18n.locale is set the result(Locale::TagList) of I18n.candidates(:type => :rfc).
This method is extended by Ruby-Locale for Ruby on Rails and returns all candidates as the Locale::TagList.

[#<Locale::Tag::Rfc: ja-JP>, #<Locale::Tag::Rfc: ja>, #<Locale::Tag::Rfc: en>]

I18n.locale (without inspect)

Locale::TagList(the result of I18n.locale) can behave like a single tag string to follow I18n.locale specification.

ja-JP

Auto-Detect the locale from the WWW browser

Reload this URL

Set locale as the "lang" parameter

  1. /locale_rails_sample/samples?lang=ja_JP [ja_JP]
  2. /locale_rails_sample/samples?lang=fr [fr]
  3. /locale_rails_sample/samples?lang=pt_BR [pt_BR]
  4. /locale_rails_sample/samples?lang=zh_CN [zh_CN]
  5. /locale_rails_sample/samples?lang=en_Latn_US_NYNORSK [en_Latn_US_NYNORSK]

Set "lang" as the cookie value.

Click one of the link below, and then click "Auto-Detect the locale from the WWW browser".

  1. /locale_rails_sample/ja_JP/samples/set_cookie/ja_JP [ja_JP]
  2. /locale_rails_sample/ja_JP/samples/set_cookie/fr [fr]
  3. /locale_rails_sample/ja_JP/samples/set_cookie/pt_BR [pt_BR]
  4. /locale_rails_sample/ja_JP/samples/set_cookie/zh_CN [zh_CN]
  5. /locale_rails_sample/ja_JP/samples/set_cookie/en_Latn_US_NYNORSK [en_Latn_US_NYNORSK]
  6. Clear cookie

Localized Routes

See config/routes.rb

  1. /locale_rails_sample/ja_JP/samples [ja_JP]
  2. /locale_rails_sample/fr/samples [fr]
  3. /locale_rails_sample/pt_BR/samples [pt_BR]
  4. /locale_rails_sample/zh_CN/samples [zh_CN]
  5. /locale_rails_sample/en_Latn_US_NYNORSK/samples [en_Latn_US_NYNORSK]

Caching

Fragment/Action caching is supported. Here is the sample for action caching.
Click the links then check %{RAILS_ROOT}/tmp/cache/ where Cached file is stored.

  1. /locale_rails_sample/ja_JP/samples/cached_action [ja_JP]
  2. /locale_rails_sample/fr/samples/cached_action [fr]
  3. /locale_rails_sample/pt_BR/samples/cached_action [pt_BR]
  4. /locale_rails_sample/zh_CN/samples/cached_action [zh_CN]
  5. /locale_rails_sample/en_Latn_US_NYNORSK/samples/cached_action [en_Latn_US_NYNORSK]

Separate Localized View template file in each languages

View template can be separeted with language tags.

  1. uz_UZ has special index page(index_uz_UZ.html.erb)
  2. uz uses normal index page
  3. nl has special _part page(_part_nl.html.erb)
  4. nl_BE also uses the special _part page(_part_nl.html.erb)