No commit activity in last 3 years
No release in over 3 years
Translate using i18n and scope it according to the object's place in Ruby's hierarchial structure. Using ancestors to build up the default option of I18n.translate. Works in much the same way as ActiveRecords human_attribute_name
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.2.9

Runtime

>= 0
 Project Readme

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 © 2009 Iain Hecker. Released under the MIT license.