Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Globalizing Rails

Globalizing Rails

Experience report after extending Rails to support a 19-language custom CMS.

Jeremy Voorhis

April 13, 2006
Tweet

More Decks by Jeremy Voorhis

Other Decks in Programming

Transcript

  1. Internationalization (i18n) • Preparation of software to be useful in

    multiple locales • Strings (dynamic and literal) that can be translated • Strings that may be bidirectional • Dates, times and numbers that can be formatted • is it 123,456.78 or 123.456,78? 3
  2. Localization (L10n) • Providing translated content for a specific language

    • Designs from right to left • Formatting dates, times and numbers for a specific region • Content that is sensitive to your audience’s culture 4
  3. Unicode • Unpopular in Japan, and not natively supported by

    Ruby • Support available through jcode module • require ‘jcode’ $KCODE = ‘u’ • Your database must support unicode too! 5
  4. Locales • Operating definition: a combination of a language and

    a region • Can be identified with a locale tag • Globalize understands these standards 6
  5. Locale Tags • Locale tags follow RFC 3066 • Language-Tag

    = Primary-subtag *( "-" Subtag ) Primary-subtag = 1*8ALPHA Subtag = 1*8(ALPHA / DIGIT) • Primary-subtag is an ISO 639 language code and should be lowercase • Subtag is typically an ISO 3166 country code and should be CAPITALIZED • Sometimes you have to wing it 7
  6. What is Globalize? • A Rails plugin providing internationalization services

    to a Rails application • Model translations • View translations • Date, Time, Number formatting 9
  7. Setting an Active Locale class ApplicationController < ActionController::Base before_filter :set_locale

    def set_locale begin Locale.set params[:locale] rescue ArgumentError redirect_to params.merge( 'locale' => 'en-US' ) end end end 14
  8. Using Model Translations # Table "public.features" # Column | Type

    #-------------+----------------------------- # id | integer # headline | character varying(255) # abstract | text # body | text # permalink | text # created_at | timestamp without time zone # modified_at | timestamp without time zone class Feature < ActiveRecord::Base translates :headline, :abstract, :body end 15
  9. The Predicated Block <% base_language_only do %> <%= link_to 'Destroy',

    { :action => 'destroy', :id => record.id }, { :post => true, :confirm => 'Are you sure?' } %> <% end %> 18
  10. The Predicated Block Defined def base_language_only yield if Locale.base? end

    def not_base_language yield unless Locale.base? end 19
  11. Translation Availability Helper Defined def translation_availability_for object_name, facet, message =

    nil not_base_language do message ||= content_tag 'p', '(Translation not available)'.t object = instance_variable_get "@#{object_name}" message if object && !object.send( facet ).blank? && object.send( "#{facet} _is_base?" ) end end 22
  12. A Gallery of Practical Interface Design • Switching locales •

    Providing reference text • Bidirectional views • View translations 23