I18n in Rails2.2

4,287 views

Published on

English slide introducing the i18n gem in Rails2.2

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,287
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
18
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

I18n in Rails2.2

  1. 1. I18n feature in Rails2.2 Overview of recent Rails support for localization
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 19. Thank you • Big big big thanks to Sven Fuchs and all the I18n team (matt, ...) • Any question ?

×