Register now and start sharing your code snippets.
-->

How to use the new internationalization (I18n) API available in Rails 2.2

Ruby posted 3 months ago by christian

Not yet released, but you can try it out by first installing the i18n gem:

   1  sudo gem install i18n 

And then testing the following code:

   1  require 'rubygems'
   2  require 'i18n'
   3  
   4  I18n.store_translations 'en-US',
   5  	:yes => "yes",
   6  	:no => "no", 
   7  	:inbox => {
   8  	  :one => '1 message', 
   9  	  :other => '{{count}} messages'
  10  	}
  11  
  12  I18n.store_translations 'sv',
  13  	:yes => "ja",
  14  	:no => "nej", 
  15  	:inbox => {
  16  	  :one => '1 meddelande', 
  17  	  :other => '{{count}} meddelanden'
  18  	}
  19  
  20  I18n.locale = 'en-US'
  21  
  22  puts I18n.translate(:yes)
  23  puts I18n.translate(:inbox, :count => 1)
  24  puts I18n.translate(:inbox, :count => 2)
  25  puts I18n.localize Time.now
  26  
  27  
  28  I18n.locale = 'sv'
  29  
  30  
  31  puts I18n.translate(:yes)
  32  puts I18n.translate(:inbox, :count => 1)
  33  puts I18n.translate(:inbox, :count => 2)
  34  puts I18n.localize Time.now

Troubleshooting

Note that the gem doesn’t contain localization data, so you’ll get the following error:

   1  translation missing: en-US, time, formats (I18n::MissingTranslationData)

To fix this, simply tell the I18n gem where to find the locales you want to use:

   1  I18n.load_translations "#{RAILS_ROOT}/locales/#{locale}.rb"

Creating a shortcut for i18n.translate

I recommend you create a shortcut for I18n.translate to the Symbol and String classes:

   1  class Symbol
   2  	def t(params = {})
   3  		I18n.t(self, params)
   4  	end
   5  end
   6  
   7  class String
   8  	def t(params = {})
   9  		I18n.t(self.to_s, params)
  10  	end
  11  end
  12  

Now instead of this:

   1  puts I18n.translate(:yes)
   2  puts I18n.translate(:inbox, :count => 1)
   3  puts I18n.translate(:inbox, :count => 2)

You can type:

   1  puts :yes.t
   2  puts :inbox.t(:count => 1)
   3  puts :inbox.t(:count => 2)

Handling missing translations

You can use this code to change the default behavior for missing translations, instead of showing “Missing translation” this code allows you to log the missing translation:

   1  class Symbol
   2    def t(params = {})
   3      params.update({:raise => true})
   4  
   5      begin
   6        I18n.t(self, params)
   7      rescue I18n::MissingTranslationData
   8        RAILS_DEFAULT_LOGGER.info("Translation for '#{self}' is missing")
   9        self
  10      end
  11    end
  12  end
  13  
  14  
  15  class String
  16    def t(params = {})
  17      params.update({:raise => true})
  18      key = self.downcase.to_s
  19        
  20      begin
  21        I18n.t(key, params)
  22      rescue I18n::MissingTranslationData
  23        RAILS_DEFAULT_LOGGER.info("Translation for '#{key}' is missing")
  24        self
  25      end
  26    end
  27  end

References
  1. http://github.com/svenfuchs/i18n/
  2. http://github.com/clemens/i18n_demo_app/
  3. http://www.artweb-design.de/2008/7/18/the-ruby-on-rails-i18n-core-api
  4. How to use the new I18n API with Rails

Tagged ruby, rails, i18n