rambling on rails’ i18n
  décousue discussion à propos de rails i18n
SRC 2012 Reject Conf
          Starring Alan Gardner


TICKETS AVAILABLE NOW!
  http://j.mp/scotruby-reject
rambling on rails’ i18n
  décousue discussion à propos de rails i18n
i18n l10n- wtf?

internationalisation is the process of preparing your
application for localisation
localisation is the process of providing locale / language
specific content within your internationalised application
you internationalise so that you can localise
Internationalisation in Rails

baked in, which is great!
easy to get started with
plenty of documentation
Contrived Overview!
Where the magic happens

Rails automatically loads any .rb or .yml files in config/
locales and makes them available to the i18n and l10n APIs.
rails-i18n gem will give you locale files for a fair few
languages including translations of Active Record’s error
messages, saving you effort
Boilerplate
     #in config/locales/en.yml

     en:
       good_afternoon: good afternoon


     #in config/locales/fr.yml

     fr:
       good_afternoon: bonjour


     #in config/locales/ja.yml

     ja:
       good_afternoon: こにちわ
Boilerplate
 #in config/routes.rb

 Example::Application.routes.draw do
   root :to => ‘example#greet`
 end

 #in app/controller/example_controller

 class ExampleController < ApplicationController

  def greet
    render :text => t(:good_afternoon)
  end

 end
What’s that given us?

Three different locales, each with a localised value for the
internationalised key ‘:good_afternoon’
A controller which will serve up this localised value when
someone visits it
The default locale is ‘en’ so the user will see ‘good
afternoon’
Make them go

 #in app/controller/example_controller

 class ExampleController < ApplicationController

  def greet
    I18n.locale = params[:locale] || ‘en’
    render :text => t(:good_afternoon)
  end

 end
Make them go

 #in app/controller/example_controller

 class ExampleController < ApplicationController

  def greet
    I18n.locale = params[:locale] || ‘en’
    render :text => t(:good_afternoon)
  end

 end
So now what?

http://localhost:3000/?locale=fr would see “bonjour”
http://localhost:3000/?locale=jp would see “こにちわ”

Anyone else would see “good afternoon”
So that’s the basics
It’s actually quite hard
Here’s the fluffy bit
Whys and Whatfors

more people don’t speak English than do - why limit your
audience?
being forced to think about how information will be
presented in various locales is a fun-fun challenge
it forces you to move view-level detail out into dictionaries
and away from your models and such - this is good!
Whys and Whatfors

it’s hard work to internationalise everything but really
rewarding if you have the need to do it
you’ll know yourself if the project you’re working with needs
to be i18n’d
personally, I always default to i18ning everything, but that’s
because I’m a masochist
Further Reading

Rails i18n guide: http://guides.rubyonrails.org/i18n.html
Rails i18n locales repository https://github.com/svenfuchs/
rails-i18n
Ruby i18n wiki: http://ruby-i18n.org/wiki
Got any questions?
I’ve not covered a lot, ask about specifics!
ありがとう!

@ryanstenhouse on Twitter
@HHRy on GitHub
http://ryanstenhouse.eu

Rails i18n

  • 1.
    rambling on rails’i18n décousue discussion à propos de rails i18n
  • 2.
    SRC 2012 RejectConf Starring Alan Gardner TICKETS AVAILABLE NOW! http://j.mp/scotruby-reject
  • 3.
    rambling on rails’i18n décousue discussion à propos de rails i18n
  • 4.
    i18n l10n- wtf? internationalisationis the process of preparing your application for localisation localisation is the process of providing locale / language specific content within your internationalised application you internationalise so that you can localise
  • 5.
    Internationalisation in Rails bakedin, which is great! easy to get started with plenty of documentation
  • 6.
  • 7.
    Where the magichappens Rails automatically loads any .rb or .yml files in config/ locales and makes them available to the i18n and l10n APIs. rails-i18n gem will give you locale files for a fair few languages including translations of Active Record’s error messages, saving you effort
  • 8.
    Boilerplate #in config/locales/en.yml en: good_afternoon: good afternoon #in config/locales/fr.yml fr: good_afternoon: bonjour #in config/locales/ja.yml ja: good_afternoon: こにちわ
  • 9.
    Boilerplate #in config/routes.rb Example::Application.routes.draw do root :to => ‘example#greet` end #in app/controller/example_controller class ExampleController < ApplicationController def greet render :text => t(:good_afternoon) end end
  • 10.
    What’s that givenus? Three different locales, each with a localised value for the internationalised key ‘:good_afternoon’ A controller which will serve up this localised value when someone visits it The default locale is ‘en’ so the user will see ‘good afternoon’
  • 11.
    Make them go #in app/controller/example_controller class ExampleController < ApplicationController def greet I18n.locale = params[:locale] || ‘en’ render :text => t(:good_afternoon) end end
  • 12.
    Make them go #in app/controller/example_controller class ExampleController < ApplicationController def greet I18n.locale = params[:locale] || ‘en’ render :text => t(:good_afternoon) end end
  • 13.
    So now what? http://localhost:3000/?locale=frwould see “bonjour” http://localhost:3000/?locale=jp would see “こにちわ” Anyone else would see “good afternoon”
  • 14.
  • 15.
  • 16.
  • 17.
    Whys and Whatfors morepeople don’t speak English than do - why limit your audience? being forced to think about how information will be presented in various locales is a fun-fun challenge it forces you to move view-level detail out into dictionaries and away from your models and such - this is good!
  • 18.
    Whys and Whatfors it’shard work to internationalise everything but really rewarding if you have the need to do it you’ll know yourself if the project you’re working with needs to be i18n’d personally, I always default to i18ning everything, but that’s because I’m a masochist
  • 19.
    Further Reading Rails i18nguide: http://guides.rubyonrails.org/i18n.html Rails i18n locales repository https://github.com/svenfuchs/ rails-i18n Ruby i18n wiki: http://ruby-i18n.org/wiki
  • 20.
    Got any questions? I’venot covered a lot, ask about specifics!
  • 21.
    ありがとう! @ryanstenhouse on Twitter @HHRyon GitHub http://ryanstenhouse.eu