Upload
renaud-morvan
View
4.005
Download
0
Embed Size (px)
DESCRIPTION
English slide introducing the i18n gem in Rails2.2
Citation preview
I18n feature in Rails2.2Overview 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); endend
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 => "foo {{bar}}"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'# => "dimanche 30 novembre 2008 11:29:49 UTC"
• Date I18n.l Date.today, :format => 'long' # => "30 novembre 2008"
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.50350284576416Benchmark.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 "my.message.here" %><%= t "my.plural.message.here", :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 ?