Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (37 sloc) 1.53 KB


Translate using i18n and scope accoring to the object's place in Ruby's hierarchial structure.

To use, extend the class:

class Admin < User
  extend ObjectScopedI18n

You can now call translations on the object:


Which calls I18n like this:

I18n.translate(:"", :default => [:"", :""])

Which looks up “” first; if it didn't found that, it looks up “”, including any included modules, all way up.

Namespaces are introduced as extra scope.

class SomeModule::SomeClass


Will be the same as:

I18n.translate(:"", :default => {...})

You can off course use all the options you would normally use with I18n.


Because I wanted something that was like ActiveRecord's human_attribute_name, but consitently over other objects too.

So what about human_attribute_name?

You can override it, so it's use object_scoped_i18n:

class ActiveRecord::Base
  extend ObjectScopedI18n
  def self.human_attribute_name(key, options = {})
    translate(key, {:default => key.to_s.humanize, :scope => [:activerecord, :attributes]}.merge(options))

You now change the scope if you like. Also, you'll get global translations for free, for columns like “created_at” and “updated_at”.


Just gem install object_scoped_i18n and require it in your project, like you're used to.


Copyright © 2009 Iain Hecker. Released under the MIT license.

Something went wrong with that request. Please try again.