Localizing a ruby on rails application


Published on

For more information: http://extremia.fi/

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Localizing a ruby on rails application

  1. 1. Localizing a Ruby on Rails Application http://extremia.fi/
  2. 2. Background• In order to reach a wider audience and to improve the usability of your Rails web application, you can offer your site in more than one language• The easiest way to do this is to utilize the built-in internationalization library I18n• To convert an existing Rails application to utilize the library you need to go through a few steps http://extremia.fi/
  3. 3. Steps1. Replace hardcoded locale-specific text in your views with calls to I18n.translate (aka I18n.t) with a key as the parameter. Note that special keys exist for some things like ActiveRecord- models, e.g. “Sign in” on your login-page with t(‘login.sign_in’).2. Add localization for Date and Time objects in your views, using I18n.localize (aka I18n.l), e.g. l Time.now. http://extremia.fi/
  4. 4. Steps (continues)4. Create translation files for each locale in config/locales directory and place your translation keys inside with the corresponding translations for the locale. Locale-specific formats also go in the same file.5. Set I18n.locale before rendering your views, so that I18n knows which locale to use when searching for translations. A good place for doing this is in a before_filter method in your ApplicationController. http://extremia.fi/
  5. 5. Interpolation and Pluralization• Additional variables may be passed when calling I18n.translate for interpolation• You don’t need to break up sentences to multiple keys when something needs to be put in the middle• I18n.translate supports pluralization using an additional :count interpolation variable http://extremia.fi/
  6. 6. Interpolation and Pluralization• The parameter is used to pick a plural form from the translations according to the pluralization rules defined by CLDR (Unicode Common Locale Data Repository) for the current locale• The requested key in the locale therefore must be a hash containing the possible plural form keys (like in the example below) http://extremia.fi/
  7. 7. Backends• I18n uses a backend object stored in I18n.backend whenever you call I18n.translate, which defaults to I18n::Backend::Simple• The simple backend keeps a hash populated with all locales from YAML files in memory• Other backends may be more suitable in large applications, especially when it’s desirable to let users contribute by translating text to their favorite language http://extremia.fi/
  8. 8. Backends• Storing locales in a database can create a problem with performance, but that may be averted by chaining a cache backend (e.g. I18n::Backend::Memoize) in front of your database backend using I18n::Backend::Chain• The database lookups will be stored in the cache, and database will be only used as a fallback if a key can’t be found in the cache