SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
I18n feature in Rails2.2
 Overview of recent Rails support for localization
Who am I?

• Renaud Morvan (http://twitter.com/neleanth)
• 3 years of professional Rails Dev at http://
  feedback20.com
• App localized in 6 languages
• Used Ruby-Gettext-Package then switched
  to custom implementation
Some History

• No built-in support for I18n before Rails 2.2
• Tons of plugin available: http://
  wiki.rubyonrails.org/rails/pages/
  internationalizationcomparison
• Lots of monkey patching needed => huge
  risk of breakage
late ‘07: Turning Point
• Sep ‘07: Change in Rails patch policy
  delegating more power to the community
• Major I18n plugin dev regroup around a
  common patch: Get rid of monkey patch +
  common API
• Then lots of debate and no consensus
• Early ‘08: patch stalled
Eventually...

• Jul ’07 Sven Fuchs (Globalize plugin dev) get
  back to work, rewrite the patch
• Rails edge is patched and I18n gem is
  bundled with it.
• Released in Rails2.2
What is I18n Gem?
• A Common Api:
  module I18n
  # Sets the current locale pseudo-globally (threadsafe)
    def locale=(locale); end

    # Translates, pluralizes and interpolates a given key
    def translate(key, options = {}); end
    alias :t :translate

    # Localizes time, dates and numbers to local formatting.
    def localize(object, options = {}); end
    alias :l :localize
    ...
  end
What is I18n Gem?
• A backend implementing it (you can use
  your own)
  class I18n::Backend:: Simple
    def load_translations(*filenames); end

    def store_translations(locale, data); end

    def translate(locale, key, options = {}); end

    def localize(locale, object, format = :default); end
  end
I18n.t basics 1/2
• Key:    I18n.t 'date.formats.short'
         I18n.t :'date.formats.short'

• Scope:     I18n.t 'date.formats.short'
            I18n.t 'formats.short', :scope => 'date'
            I18n.t 'short', :scope => 'date.formats'
            I18n.t 'short', :scope => ['date','formats']

• Pluralization:    I18n.t :foo, :count => 0 # => 'Foos'
                    I18n.t :foo, :count => 1 # => 'Foo'
                    I18n.t :foo, :count => 2 # => 'Foos'

• Interpolation: given     :foo => quot;foo {{bar}}quot;
  I18n.t :foo, :bar => 'toto' # => 'foo toto'
I18n.t Basics 2/2
• Missing translation:
    I18n.t :missing # raise I18n::MissingTranslationData

• Default (can be translation if :sym):
    I18n.t :missing, :default => 'default' # => 'default'

• Bulk:
    I18n.t :missing, :default => [:missing_too, 'default']
    I18n.t [:'baz.foo', :'baz.bar']

• AllinOne:
  I18n.t ['attr1.blank', :'attr2.blank'], :count => 2,
  :scope => [:activerecord, :attributes], :default =>
  [:'model.blank'], :attribute_name => 'attr1'# yuk !
I18n.l

• Time, TimeZone, DateTime
   I18n.l Time.zone.now, :format => 'long'
  # => quot;dimanche 30 novembre 2008 11:29:49 UTCquot;

• Date
    I18n.l Date.today, :format => 'long'
  # => quot;30 novembre 2008quot;
I18n API
• Simple and efficient for I18n in model code
• Minimal impact on Rails core code
• Provide I18n for any Rails plugin !
• Not that simple to implement backend:
  those features are MANDATORY for rails
• One method to Translate them all! =>
  difficult to parse, optimize
I18n::SimpleBackend
• Implement the API and the features
• Storage YML (nested hash) or ruby
• Yet a bit slow:
  Benchmark.realtime { 10000.times do
  I18n.t [:date, :format, :short]; end; }
  # => 1.50350284576416
  Benchmark.realtime {10000.times do
  _('date.formats.short') end; }
  # => 0.023993968963623
I18n::YourBackend ?
• You have to implement at least the exact
  same features and behavior for Rails I18n
• All extra features won’t be supported in
  other backend and should be implemented
  externaly
• Will make your 118n/L10n plugin work with
  all rails app and plugin
• No more monkey patching !
I18n in Rails
• Currently (Rails2.2):
 • AR Validation errors
 • Form errors
 • Float Number / Currency (output only)
 • Time/Date Format (on demand with
    I18n.l)
 • Time/Date related helpers
I18n in Rails
• Rails in only bundled with :en locale but
  since Rails2.2rc2 it comes with a config/
  locale folder
• To bootstrap localization of you own rails
  app go to http://github.com/svenfuchs/rails-
  i18n/tree/master
• Would need a reference page where all keys
  used in rails are displayed, currently you have
I18n in Rails: niceties
• Time of custom AR validation message in
  class is over:
      class User < ActiveRecord::Base
         validates_presence_of :name
      end
  # will look at the different keys in this order
  'activerecord.errors.messages.models.user.attributes.name.
  blank'
  'activerecord.errors.messages.models.user.blank'
  'activerecord.errors.messages.blank'


  It makes it possible to handle special case
  per langage and not in models
I18n in Rails: views


• In views you have the “t” helper:
  <%= t quot;my.message.herequot; %>
  <%= t quot;my.plural.message.herequot;, :count => 2 %>
  ...

  Its just an alias to I18n.t
Resources
• The I18n gem: http://github.com/svenfuchs/
  i18n/tree
• Rails translation for SimpleBackend: http://
  github.com/svenfuchs/rails-i18n/tree/master
• Example rails App: http://github.com/
  clemens/i18n_demo_app/tree/master
• Live: http://i18n-demo.phusion.nl/
Thank you


• Big big big thanks to Sven Fuchs and all the
  I18n team (matt, ...)
• Any question ?

Weitere ähnliche Inhalte

Andere mochten auch

Please Take It Outside
Please Take It OutsidePlease Take It Outside
Please Take It Outsideguest1bcf9
 
Fl 0111 avicenia marina
Fl 0111 avicenia marinaFl 0111 avicenia marina
Fl 0111 avicenia marinaBoris Irawan
 
Dynamic stories by asela jayarathne
Dynamic stories   by asela jayarathneDynamic stories   by asela jayarathne
Dynamic stories by asela jayarathneAsela Jayarathne
 
Ale Eslava Portfolio 2012
Ale Eslava Portfolio 2012Ale Eslava Portfolio 2012
Ale Eslava Portfolio 2012alees
 
Cards Center Project Approach May 24 2008
Cards Center Project Approach May 24 2008Cards Center Project Approach May 24 2008
Cards Center Project Approach May 24 2008Saeed A Siddiki
 
Discover your spiritual gifts
Discover your spiritual giftsDiscover your spiritual gifts
Discover your spiritual giftsAsela Jayarathne
 
Help children todiscover their gifts
Help children todiscover their giftsHelp children todiscover their gifts
Help children todiscover their giftsAsela Jayarathne
 
Simon's Christmas - Christmas drama for kids
Simon's Christmas - Christmas drama for kids Simon's Christmas - Christmas drama for kids
Simon's Christmas - Christmas drama for kids Asela Jayarathne
 
How to lead a child to Christ
How to lead a child to ChristHow to lead a child to Christ
How to lead a child to ChristAsela Jayarathne
 
Discover Your Spiritual Gifts - by Asela Jayarathne
Discover Your Spiritual Gifts - by Asela Jayarathne Discover Your Spiritual Gifts - by Asela Jayarathne
Discover Your Spiritual Gifts - by Asela Jayarathne Asela Jayarathne
 

Andere mochten auch (11)

Please Take It Outside
Please Take It OutsidePlease Take It Outside
Please Take It Outside
 
Fl 0111 avicenia marina
Fl 0111 avicenia marinaFl 0111 avicenia marina
Fl 0111 avicenia marina
 
Dynamic stories by asela jayarathne
Dynamic stories   by asela jayarathneDynamic stories   by asela jayarathne
Dynamic stories by asela jayarathne
 
Ale Eslava Portfolio 2012
Ale Eslava Portfolio 2012Ale Eslava Portfolio 2012
Ale Eslava Portfolio 2012
 
Cards Center Project Approach May 24 2008
Cards Center Project Approach May 24 2008Cards Center Project Approach May 24 2008
Cards Center Project Approach May 24 2008
 
Discover your spiritual gifts
Discover your spiritual giftsDiscover your spiritual gifts
Discover your spiritual gifts
 
Help children todiscover their gifts
Help children todiscover their giftsHelp children todiscover their gifts
Help children todiscover their gifts
 
Simon's Christmas - Christmas drama for kids
Simon's Christmas - Christmas drama for kids Simon's Christmas - Christmas drama for kids
Simon's Christmas - Christmas drama for kids
 
How to lead a child to Christ
How to lead a child to ChristHow to lead a child to Christ
How to lead a child to Christ
 
Discover Your Spiritual Gifts - by Asela Jayarathne
Discover Your Spiritual Gifts - by Asela Jayarathne Discover Your Spiritual Gifts - by Asela Jayarathne
Discover Your Spiritual Gifts - by Asela Jayarathne
 
Play and pray new book
Play and pray   new bookPlay and pray   new book
Play and pray new book
 

Ähnlich wie I18n in Rails2.2

Internationalization in Rails 2.2
Internationalization in Rails 2.2Internationalization in Rails 2.2
Internationalization in Rails 2.2Belighted
 
Shipping your product overseas!
Shipping your product overseas!Shipping your product overseas!
Shipping your product overseas!Diogo Busanello
 
The Ruby On Rails I18n Core Api
The Ruby On Rails I18n Core ApiThe Ruby On Rails I18n Core Api
The Ruby On Rails I18n Core ApiNTT DATA Americas
 
Ruby i18n - internationalization for ruby
Ruby i18n - internationalization for rubyRuby i18n - internationalization for ruby
Ruby i18n - internationalization for rubyLingoHub
 
symfony : I18n And L10n
symfony : I18n And L10nsymfony : I18n And L10n
symfony : I18n And L10nWildan Maulana
 
Rails Internationalization
Rails InternationalizationRails Internationalization
Rails InternationalizationMike Champion
 
MISC TOPICS #2: I18n Data Programming Pearls Random Records Rpx Now Susher St...
MISC TOPICS #2: I18n Data Programming Pearls Random Records Rpx Now Susher St...MISC TOPICS #2: I18n Data Programming Pearls Random Records Rpx Now Susher St...
MISC TOPICS #2: I18n Data Programming Pearls Random Records Rpx Now Susher St...grosser
 
Plone i18n, LinguaPlone
Plone i18n, LinguaPlonePlone i18n, LinguaPlone
Plone i18n, LinguaPloneQuintagroup
 
Using REST and XML Builder for legacy XML
Using REST and XML Builder for legacy XMLUsing REST and XML Builder for legacy XML
Using REST and XML Builder for legacy XMLKeith Pitty
 
i18n was the missing piece_ make your apps accessible to 70%+ of the users in...
i18n was the missing piece_ make your apps accessible to 70%+ of the users in...i18n was the missing piece_ make your apps accessible to 70%+ of the users in...
i18n was the missing piece_ make your apps accessible to 70%+ of the users in...Arisa Fukuzaki
 
Symfony 2.0
Symfony 2.0Symfony 2.0
Symfony 2.0GrUSP
 
BNC Tech Forum 09: Lexcycle Stanza demo
BNC Tech Forum 09: Lexcycle Stanza demoBNC Tech Forum 09: Lexcycle Stanza demo
BNC Tech Forum 09: Lexcycle Stanza demoBookNet Canada
 
How To Build And Launch A Successful Globalized App From Day One Or All The ...
How To Build And Launch A Successful Globalized App From Day One  Or All The ...How To Build And Launch A Successful Globalized App From Day One  Or All The ...
How To Build And Launch A Successful Globalized App From Day One Or All The ...agileware
 
TAUS USER CONFERENCE 2009, Normalization of translation memories
TAUS USER CONFERENCE 2009, Normalization of translation memoriesTAUS USER CONFERENCE 2009, Normalization of translation memories
TAUS USER CONFERENCE 2009, Normalization of translation memoriesTAUS - The Language Data Network
 
Rails I18n From The Trenches
Rails I18n From The TrenchesRails I18n From The Trenches
Rails I18n From The Trenchesclemensk
 
fast prototyping with sinatra sequel w2tags
fast prototyping with sinatra sequel w2tagsfast prototyping with sinatra sequel w2tags
fast prototyping with sinatra sequel w2tagswidi harsojo
 

Ähnlich wie I18n in Rails2.2 (20)

Internationalization in Rails 2.2
Internationalization in Rails 2.2Internationalization in Rails 2.2
Internationalization in Rails 2.2
 
Shipping your product overseas!
Shipping your product overseas!Shipping your product overseas!
Shipping your product overseas!
 
The Ruby On Rails I18n Core Api
The Ruby On Rails I18n Core ApiThe Ruby On Rails I18n Core Api
The Ruby On Rails I18n Core Api
 
Ruby i18n - internationalization for ruby
Ruby i18n - internationalization for rubyRuby i18n - internationalization for ruby
Ruby i18n - internationalization for ruby
 
Rails i18n
Rails i18nRails i18n
Rails i18n
 
symfony : I18n And L10n
symfony : I18n And L10nsymfony : I18n And L10n
symfony : I18n And L10n
 
Rails Internationalization
Rails InternationalizationRails Internationalization
Rails Internationalization
 
MISC TOPICS #2: I18n Data Programming Pearls Random Records Rpx Now Susher St...
MISC TOPICS #2: I18n Data Programming Pearls Random Records Rpx Now Susher St...MISC TOPICS #2: I18n Data Programming Pearls Random Records Rpx Now Susher St...
MISC TOPICS #2: I18n Data Programming Pearls Random Records Rpx Now Susher St...
 
Plone i18n, LinguaPlone
Plone i18n, LinguaPlonePlone i18n, LinguaPlone
Plone i18n, LinguaPlone
 
Using REST and XML Builder for legacy XML
Using REST and XML Builder for legacy XMLUsing REST and XML Builder for legacy XML
Using REST and XML Builder for legacy XML
 
i18n was the missing piece_ make your apps accessible to 70%+ of the users in...
i18n was the missing piece_ make your apps accessible to 70%+ of the users in...i18n was the missing piece_ make your apps accessible to 70%+ of the users in...
i18n was the missing piece_ make your apps accessible to 70%+ of the users in...
 
Symfony 2.0
Symfony 2.0Symfony 2.0
Symfony 2.0
 
BNC Tech Forum 09: Lexcycle Stanza demo
BNC Tech Forum 09: Lexcycle Stanza demoBNC Tech Forum 09: Lexcycle Stanza demo
BNC Tech Forum 09: Lexcycle Stanza demo
 
How To Build And Launch A Successful Globalized App From Day One Or All The ...
How To Build And Launch A Successful Globalized App From Day One  Or All The ...How To Build And Launch A Successful Globalized App From Day One  Or All The ...
How To Build And Launch A Successful Globalized App From Day One Or All The ...
 
TAUS USER CONFERENCE 2009, Normalization of translation memories
TAUS USER CONFERENCE 2009, Normalization of translation memoriesTAUS USER CONFERENCE 2009, Normalization of translation memories
TAUS USER CONFERENCE 2009, Normalization of translation memories
 
Tugas Pw [6]
Tugas Pw [6]Tugas Pw [6]
Tugas Pw [6]
 
Tugas Pw [6] (2)
Tugas Pw [6] (2)Tugas Pw [6] (2)
Tugas Pw [6] (2)
 
Rails I18n From The Trenches
Rails I18n From The TrenchesRails I18n From The Trenches
Rails I18n From The Trenches
 
Titanium Mobile Intro
Titanium Mobile IntroTitanium Mobile Intro
Titanium Mobile Intro
 
fast prototyping with sinatra sequel w2tags
fast prototyping with sinatra sequel w2tagsfast prototyping with sinatra sequel w2tags
fast prototyping with sinatra sequel w2tags
 

Kürzlich hochgeladen

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 

Kürzlich hochgeladen (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

I18n in Rails2.2

  • 1. I18n feature in Rails2.2 Overview of recent Rails support for localization
  • 2. Who am I? • Renaud Morvan (http://twitter.com/neleanth) • 3 years of professional Rails Dev at http:// feedback20.com • App localized in 6 languages • Used Ruby-Gettext-Package then switched to custom implementation
  • 3. Some History • No built-in support for I18n before Rails 2.2 • Tons of plugin available: http:// wiki.rubyonrails.org/rails/pages/ internationalizationcomparison • Lots of monkey patching needed => huge risk of breakage
  • 4. late ‘07: Turning Point • Sep ‘07: Change in Rails patch policy delegating more power to the community • Major I18n plugin dev regroup around a common patch: Get rid of monkey patch + common API • Then lots of debate and no consensus • Early ‘08: patch stalled
  • 5. Eventually... • Jul ’07 Sven Fuchs (Globalize plugin dev) get back to work, rewrite the patch • Rails edge is patched and I18n gem is bundled with it. • Released in Rails2.2
  • 6. What is I18n Gem? • A Common Api: module I18n # Sets the current locale pseudo-globally (threadsafe) def locale=(locale); end # Translates, pluralizes and interpolates a given key def translate(key, options = {}); end alias :t :translate # Localizes time, dates and numbers to local formatting. def localize(object, options = {}); end alias :l :localize ... end
  • 7. What is I18n Gem? • A backend implementing it (you can use your own) class I18n::Backend:: Simple def load_translations(*filenames); end def store_translations(locale, data); end def translate(locale, key, options = {}); end def localize(locale, object, format = :default); end end
  • 8. I18n.t basics 1/2 • Key: I18n.t 'date.formats.short' I18n.t :'date.formats.short' • Scope: I18n.t 'date.formats.short' I18n.t 'formats.short', :scope => 'date' I18n.t 'short', :scope => 'date.formats' I18n.t 'short', :scope => ['date','formats'] • Pluralization: I18n.t :foo, :count => 0 # => 'Foos' I18n.t :foo, :count => 1 # => 'Foo' I18n.t :foo, :count => 2 # => 'Foos' • Interpolation: given :foo => quot;foo {{bar}}quot; I18n.t :foo, :bar => 'toto' # => 'foo toto'
  • 9. I18n.t Basics 2/2 • Missing translation: I18n.t :missing # raise I18n::MissingTranslationData • Default (can be translation if :sym): I18n.t :missing, :default => 'default' # => 'default' • Bulk: I18n.t :missing, :default => [:missing_too, 'default'] I18n.t [:'baz.foo', :'baz.bar'] • AllinOne: I18n.t ['attr1.blank', :'attr2.blank'], :count => 2, :scope => [:activerecord, :attributes], :default => [:'model.blank'], :attribute_name => 'attr1'# yuk !
  • 10. I18n.l • Time, TimeZone, DateTime I18n.l Time.zone.now, :format => 'long' # => quot;dimanche 30 novembre 2008 11:29:49 UTCquot; • Date I18n.l Date.today, :format => 'long' # => quot;30 novembre 2008quot;
  • 11. I18n API • Simple and efficient for I18n in model code • Minimal impact on Rails core code • Provide I18n for any Rails plugin ! • Not that simple to implement backend: those features are MANDATORY for rails • One method to Translate them all! => difficult to parse, optimize
  • 12. I18n::SimpleBackend • Implement the API and the features • Storage YML (nested hash) or ruby • Yet a bit slow: Benchmark.realtime { 10000.times do I18n.t [:date, :format, :short]; end; } # => 1.50350284576416 Benchmark.realtime {10000.times do _('date.formats.short') end; } # => 0.023993968963623
  • 13. I18n::YourBackend ? • You have to implement at least the exact same features and behavior for Rails I18n • All extra features won’t be supported in other backend and should be implemented externaly • Will make your 118n/L10n plugin work with all rails app and plugin • No more monkey patching !
  • 14. I18n in Rails • Currently (Rails2.2): • AR Validation errors • Form errors • Float Number / Currency (output only) • Time/Date Format (on demand with I18n.l) • Time/Date related helpers
  • 15. I18n in Rails • Rails in only bundled with :en locale but since Rails2.2rc2 it comes with a config/ locale folder • To bootstrap localization of you own rails app go to http://github.com/svenfuchs/rails- i18n/tree/master • Would need a reference page where all keys used in rails are displayed, currently you have
  • 16. I18n in Rails: niceties • Time of custom AR validation message in class is over: class User < ActiveRecord::Base validates_presence_of :name end # will look at the different keys in this order 'activerecord.errors.messages.models.user.attributes.name. blank' 'activerecord.errors.messages.models.user.blank' 'activerecord.errors.messages.blank' It makes it possible to handle special case per langage and not in models
  • 17. I18n in Rails: views • In views you have the “t” helper: <%= t quot;my.message.herequot; %> <%= t quot;my.plural.message.herequot;, :count => 2 %> ... Its just an alias to I18n.t
  • 18. Resources • The I18n gem: http://github.com/svenfuchs/ i18n/tree • Rails translation for SimpleBackend: http:// github.com/svenfuchs/rails-i18n/tree/master • Example rails App: http://github.com/ clemens/i18n_demo_app/tree/master • Live: http://i18n-demo.phusion.nl/
  • 19. Thank you • Big big big thanks to Sven Fuchs and all the I18n team (matt, ...) • Any question ?