Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Internationalization in Rails 2.2

4 024 vues

Publié le

These are the slides of my talk about Rails 2.2 i18n (internationalization) which I 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.

Publié dans : Technologie
  • Soyez le premier à commenter

Internationalization in Rails 2.2

  1. 1. Internationalization in Rails 2.2 Nicolas Jacobeus - Belighted sprl FOSDEM ’09 - Ruby and Rails Developer Room February 8th 2009
  2. 2. Summary What is internationalization? Internationalization before Rails 2.2 The Rails i18n framework Short demo Globalize2 Resources
  3. 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. 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. 5. Rails i18n before 2.2 English was hardcoded in the codebase Rails was a framework localized to English (en-US)
  6. 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. 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. 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. 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. 10. The Rails i18n framework Advantages? The backend can easily be swapped No monkey-patching anymore!
  11. 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. 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'
  13. 13. Scopes / namespaces I18n.translate :exclusion, :scope => [:activerecord, :errors, :messages] I18n.translate “activerecord.errors.messages.exclusion” I18n.translate “messages.exclusion”, :scope => “activerecord.errors”
  14. 14. Defaults I18n.t :missing, :default => 'Not here' # => 'Not here' Default value can itself be a key and get translated! Chaining defaults: I18n.t :missing, :default => [:also_missing, 'Not here'] # => 'Not here'
  15. 15. Interpolation All other options will be interpolated I18n.translate :hello, :name => “Nicolas” # => ‘Salut Nicolas !’
  16. 16. Pluralization I18n.translate :inbox, :count => 2 # => '2 messages' Pluralization rules defined by CLDR: [ :zero, :one, :two, :few, :many, :other ]
  17. 17. Localizing dates and times I18n.l Time.now, :locale => :en quot;Sun, 08 Feb 2009 15:42:42 +0100quot; I18n.l Time.now, :locale => :fr => quot;08 février 2009 15:42quot; I18n.l Date.today, :locale => :fr, :format => :short => quot;8 févquot; You have to provide the localizations!
  18. 18. Short demo Basic “hello world” app: Configure the i18n module Set the locale in each request Internationalize the application
  19. 19. Shortcomings The simple backend is... simple Pluralization is basic (singular and plural) Storage only in YAML or Ruby files No model translations
  20. 20. Globalize2 http://github.com/joshmh/globalize2 Like Globalize but sits on top of the Rails i18n API Adds model translations Provides a new backend Provides a new exception handler
  21. 21. Model translations #translates specifies which fields you want to be translatable The accessor will return the localized version transparently
  22. 22. Model translations The translations are stored in a separate table for each model Shortcut: Post.create_translation_table! :title => :string, :text => :text
  23. 23. Globalize::Backend::Static Builds on the Simple backend Locale fallbacks Allows custom pluralization logic
  24. 24. Locale fallbacks Allows you to set fallbacks when a translation is not available in a particular locale I18n.fallbacks[:quot;es-MXquot;] # => [:quot;es-MXquot;, :es, :quot;en-USquot;, :en] You can edit fallback chains I18n.fallbacks.map :ca => :quot;es-ESquot; I18n.fallbacks[:ca] # => [:ca, :quot;es-ESquot;, :es, :quot;en-USquot;, :en]
  25. 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. 26. Missing translations log handler Will log all missing translations in a file require 'globalize/i18n/missing_translations_log_handler’ logger = Logger.new(quot;#{RAILS_ROOT}/log/missing_trans.logquot;) I18n.missing_translations_logger = logger I18n.exception_handler = :missing_translations_log_handler Pretty useful to quickly find what’s missing
  27. 27. Resources Some additional resources to help you start localizing your applications For a comprehensive list see: http://rails-i18n.org/wiki
  28. 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/
  29. 29. 99translations.com Hosted webservice for sharing and maintaining translations Meeting place for developers and translators Admin interface, version control, API
  30. 30. Relevant Git repositories http://github.com/svenfuchs/i18n http://github.com/svenfuchs/rails-i18n http://github.com/joshmh/globalize2 http://github.com/rails/rails
  31. 31. Thank you !