# In config/environment.rb
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de

Getting and Setting Locale

# Getting and setting the current locale
I18n.locale # => :en
I18n.locale = :sv
I18n.locale # => :sv

Translation files


I18n.translate :'home.page_title' # => Welcome
# You can use just t as a shorthand for translate
I18n.t :'home.page_title' # => Welcome

# Defaults
I18n.t :'home.foobar', :default => 'my default text' # => my default text

# Pluralization
I18n.t :'home.articles' # =>  {:one=>"Article", :other=>"Articles"}
I18n.t :'home.articles', :count => 1 # => Article
I18n.t :'home.articles', :count => 2 # => Articles

Variable Interpolation

# In config/locales/en.yml
    page_title: Welcome {{user_name}}

I18n.t 'home.page_title' # => raises I18n::MissingInterpolationArgument
I18n.t 'home.page_title', :user_name => 'Peter' # => 'Welcome Peter'


I18n.localize Time.now # => "Wed, 18 Feb 2009 14:38:07 +0100" 
I18n.locale = :sv
I18n.localize Time.now # => "Onsdag, 18. Februari 2009, 14:37 Uhr" 

The translate helper

# In views and controllers you can use the t helper method instead of I18n.t
<%=h t(:'home.page_title') %

Error Messages

# In config/locales/sv.yml
      # The values :model, :attribute and :value are always available for interpolation
      # The value :count is available when applicable. Can be used for pluralization.
        exclusion: "är reserverad" 
        invalid: "har ett ogiltigt värde" 
      pressrelease: # The ActiveRecord model
        body: "Brödtexten" # The model attribute translation  

Time Zones

# Rails has Time Zone support as of version 2.1
# In config/environment.rb
config.time_zone = 'Central Time (US & Canada)' # UTC -6

Time.now # => Wed Feb 18 14:58:54 +0100 2009
Time.zone.now # => Wed, 18 Feb 2009 07:59:16 CST -06:00

# ActiveRecord attributes are in the current timezone
Article.first.created_at # => Wed, 18 Feb 2009 07:59:41 CST -06:00
# Dates in the database are stored in UTC:
Article.first.created_at_before_type_cast # => "2009-02-18 13:59:41" 

Time Zones and Conditions Gotcha

# In ActiveRecord::Base.find conditions, the following expressions work:
# Time.zone.now, 1.days.ago, or Time.parse("2008-12-23").utc,
# The following don't work:
# Time.now, Time.parse("2008-12-23")
# Examples:
Event.all(:conditions => ["start_time > ?", Time.zone.now])
Event.all(:conditions => ["start_time > ?", Time.parse("2008-12-23").utc])

Setting Current Timezone

# You can use a before filter to set the time zone of the current user like this:
before_filter :set_timezone

def set_timezone
  # current_user.time_zone #=> 'London'
  Time.zone = current_user.time_zone

Converting Texts to I18n Lookups

If you have an application with a lot of hard coded texts that need to be converted to I18n.translate lookups, then you can use Sven Fuch’s Rails I18n TextMate Bundle. Mark the text you want to translate, hit command-shift-E or alt-shift-E and the text will be converted to an I18n translate lookup and the text moved to a YAML file at log/translations.yml. This can speed up internationalization of your application quite a bit.

Translation UI

The Translate plugin allows you to do your translations via a web UI instead of directly in YAML files. The plugin will read from and write to the YAML files for you in the background. You can choose locale to translate from and to, search translations by key or by text, list missing translations etc.