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

Globalizing Rails–Railsconf edition

Globalizing Rails–Railsconf edition

Jeremy Voorhis

June 25, 2008
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 static • Bidirectional strings • Dates, times and numbers • is it 123,456.78 or 123.456,78?
  2. Localization (L10n) • Providing translated content for a specific language

    • Implementing designs from right to left • Formatting dates, times and numbers for a specific region • Content that is sensitive to your audience’s culture
  3. Locales • Operating definition: a combination of a language and

    a region • Can be identified with a locale tag • Globalize understands locales
  4. 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
  5. 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!
  6. What is Globalize? • A Rails plugin providing internationalization services

    to a Rails application • Model translations • View translations • Date and time translations and formatting • Number formatting
  7. Setting an active locale class ApplicationController < ActionController::Base before_filter :set_locale

    private def set_locale Locale.set params[:locale] rescue ArgumentError redirect_to params.merge( 'locale' => 'en-US' ) end end
  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
  9. Using view translations <%# GLobalize style %> <h2><%= 'FEATURES'.t -%></h2>

    <%# gettext style %> <h2><%= :features.t -%></h2>
  10. The predicated block <% base_language_only do %> <%= link_to 'Destroy',

    { :action => 'destroy', :id => record.id }, { :post => true, :confirm => 'Are you sure?' } %> <% end %>
  11. The predicated block defined def base_language_only yield if Locale.base? end

    def not_base_language yield unless Locale.base? end
  12. 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 and not object.send( facet ).blank? and object.send( "#{facet}_is_base?" ) end end
  13. A gallery of practical interface design • Switching locales •

    Providing reference text • Bidirectional views • View translations