This document summarizes the history and features of internationalization (i18n) support in Ruby on Rails. It discusses how i18n was initially implemented through plugins with monkey patching, leading Rails 2.2 to bundle the i18n gem for a common API without patching. The gem provides translation and localization methods, along with backends like SimpleBackend for storage. Rails now supports i18n for validation errors, forms, numbers, dates and more through the i18n helper and configuration. Resources are provided for using i18n in Rails applications.
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
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 ?