Skip to content
Translate using i18n and scope it according to the object's place in Ruby's hierarchial structure
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
spec
.document
.gitignore
README.rdoc
Rakefile
VERSION
object_scoped_i18n.gemspec

README.rdoc

object_scoped_i18n

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
end

You can now call translations on the object:

Admin.translate(:name)

Which calls I18n like this:

I18n.translate(:"admin.name", :default => [:"user.name", :"object.name"])

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

Namespaces are introduced as extra scope.

class SomeModule::SomeClass
end

SomeModule::SomeClass.translate(:name)

Will be the same as:

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

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

Why?

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))
  end
end

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

Installation

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

Copyright

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

Something went wrong with that request. Please try again.