These are the slides of Nicolas' talk about Rails 2.2 i18n (internationalization) which he gave at the Fosdem '09 Ruby and Rails DevRoom on February 8th 2009.
See http://fosdem.org/2009/schedule/events/ror_i18n_rails_2_2 for more details.
3. What we are talking about
Internationalization (i18n): “designing a software
application so that it can be adapted to various
languages and regions without engineering changes”
Localization (L10n): “the process of adapting
software for a specific region or language by adding
locale-specific components and translating text”
4. What it means
This emcompasses
Language
Culture
Writing conventions
Why does it matter?
Not everybody speaks english, even on the web
In Europe: dozens of cultures and languages
5. Rails i18n before 2.2
English was hardcoded in the codebase
Rails was a framework localized to English (en-US)
6. Rails i18n before 2.2
i18n plugins had to monkey-patch Rails everywhere
Remove english defaults
Enhance business logic so that it handles translation
7. The Rails i18n framework
Started in Sep ’07 by several i18n plugin developers
Aim:
eliminate the need to monkey-patch Rails for i18n
implement a minimal, common API for all solutions
8. The Rails i18n framework
A gem by Sven Fuchs and other contributors
http://github.com/svenfuchs/i18n
Shipped with Rails since 2.2 (ActiveSupport vendor dir)
2 parts:
An API
A minimal “Simple” backend implementing the API
9. The Rails i18n framework
The API is now used by Rails instead of hardcoded
strings
The Simple Backend implements the API to re-localize
Rails back to en-US
Rails is still a framework localized to English, but it’s
now globalized too
Doesn’t remove the need for plugins!
10. The Rails i18n framework
Advantages?
The backend can easily be swapped
No monkey-patching anymore!
11. The i18n module in details
Defines #translate / #t and #localize / #l
Stores the current locale in Thread.current
Store a default locale
Stores a backend
Stores an exception handler
12. So how do I translate?
Put your translations in config/locales (YAML or Ruby),
or use I18n.backend.store_translations in the console:
I18n.backend.store_translations :en, :hi => “Hi!”
I18n.backend.store_translations :fr, :hi => “Salut!”
Set the current locale:
I18n.locale = :fr
Keys can be strings or symbols:
I18n.t :message
I18n.t 'message'
17. Localizing dates and times
I18n.l Time.now, :locale => :en
"Sun, 08 Feb 2009 15:42:42 +0100"
I18n.l Time.now, :locale => :fr
=> "08 février 2009 15:42"
I18n.l Date.today, :locale => :fr, :format => :short
=> "8 fév"
You have to provide the localizations!
18. Short demo
Basic “hello world” app:
Configure the i18n module
Set the locale in each request
Internationalize the application
19. Shortcomings
The simple backend is... simple
Pluralization is basic (singular and plural)
Storage only in YAML or Ruby files
No model translations
21. Model translations
#translates specifies
which fields you want to be
translatable
The accessor will return the
localized version
transparently
22. Model translations
The translations are stored
in a separate table for each
model
Shortcut:
Post.create_translation_table!
:title => :string,
:text => :text
24. Locale fallbacks
Allows you to set fallbacks when a translation is not
available in a particular locale
I18n.fallbacks[:"es-MX"]
# => [:"es-MX", :es, :"en-US", :en]
You can edit fallback chains
I18n.fallbacks.map :ca => :"es-ES"
I18n.fallbacks[:ca]
# => [:ca, :"es-ES", :es, :"en-US", :en]
25. Custom pluralization logic
For languages not behaving like English
Globalize2’s pluralization logic is not hardcoded
New rules can by added with lambdas
@backend.add_pluralizer :cz, lambda { |c|
c == 1 ? :one : (2..4).include?(c) ? :few : :other
}
26. Missing translations log
handler
Will log all missing translations in a file
require 'globalize/i18n/missing_translations_log_handler’
logger = Logger.new("#{RAILS_ROOT}/log/missing_trans.log")
I18n.missing_translations_logger = logger
I18n.exception_handler = :missing_translations_log_handler
Pretty useful to quickly find what’s missing
28. The Translate plugin
Adds a translation UI to
your app in seconds
Allows you or your
translators to easily
translate all your strings
Available on Github:
http://github.com/
newsdesk/translate/