Slide 1

Slide 1 text

Globalizing Rails Jeremy Voorhis Lead Architect, www.jvoorhis.com www.planetargon.com 1

Slide 2

Slide 2 text

What is Globalization? • Internationalization • Localization • A cross-cutting concern that will transform your application 2

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Locales • Operating definition: a combination of a language and a region • Can be identified with a locale tag • Globalize understands these standards 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Example Productions • en-US • fr-CA • es-419 8

Slide 9

Slide 9 text

What is Globalize? • A Rails plugin providing internationalization services to a Rails application • Model translations • View translations • Date, Time, Number formatting 9

Slide 10

Slide 10 text

Getting Started With Globalize 10

Slide 11

Slide 11 text

Installing Globalize $ script/plugin install http://svn.globalize-rails.org/svn/globalize/ globalize/trunk $ rake globalize:setup Locale.set_base_language ‘en-US’ 11

Slide 12

Slide 12 text

Setting the Base Language # Include your application configuration below Locale.set_base_language 'en-US' 12

Slide 13

Slide 13 text

Capturing Locales map.connect ':locale/:controller/:action/:permalink' 13

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Using View Translations

<%= 'FEATURES'.t -%>

or

<%= :title.t -%>

16

Slide 17

Slide 17 text

Advanced Techniques • Predicated blocks • Translation availability helper 17

Slide 18

Slide 18 text

The Predicated Block <% base_language_only do %> <%= link_to 'Destroy', { :action => 'destroy', :id => record.id }, { :post => true, :confirm => 'Are you sure?' } %> <% end %> 18

Slide 19

Slide 19 text

The Predicated Block Defined def base_language_only yield if Locale.base? end def not_base_language yield unless Locale.base? end 19

Slide 20

Slide 20 text

Notify users when a translation is not available 20

Slide 21

Slide 21 text

A Translation Availability Helper <%= translation_availability_for :page, :body %> <%= markdown @page.body %> 21

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

A Gallery of Practical Interface Design • Switching locales • Providing reference text • Bidirectional views • View translations 23

Slide 24

Slide 24 text

Make It Easy for Users to Switch Locales 24

Slide 25

Slide 25 text

Locale selector for public site 25

Slide 26

Slide 26 text

The same helper is used in the admin site 26

Slide 27

Slide 27 text

Translation Admin With English Reference 27

Slide 28

Slide 28 text

Base language administration screen params[:locale] # => ‘en-US’ 28

Slide 29

Slide 29 text

Hide untranslated fields and provide a reference params[:locale] # => ‘zh-Hant’ 29

Slide 30

Slide 30 text

Bidirectional Views 30

Slide 31

Slide 31 text

English text is read left to right http://www.rashgash.co.il/en 31

Slide 32

Slide 32 text

Hebrew text is read right to left http://www.rashgash.co.il/ 32

Slide 33

Slide 33 text

Make it Easy for Translators to Update String Literals in Your Views 33

Slide 34

Slide 34 text

Admin users can switch locales and update strings Translations provided by Google ;) 34

Slide 35

Slide 35 text

Globalize Something • www.globalize-rails.org • Trac • Wiki • Mailing list • globalize.rubyforge.org • API Docs 35

Slide 36

Slide 36 text

Acknowledgments • Joshua Harvey • Joshua Sierles 36