A plugin that mimics ActiveRecord 2.2 I18n behavior

This article is deprecated.I've marked this article as deprecated. It is either because the gem or plugin discussed in this article is no longer maintained, or I've changed my opinion on it. This article is here only as an historic reference.

acts_as_translatable_model plugin

After a long day in the train today, I extracted the I18n functionality from ActiveRecord so its applicable to any class. This can be especially handy if the source of the data is different, e.g. LDAP. Just install the plugin and add one line to your class to make it translatable:

./script/plugin install git://github.com/iain/acts_as_translatable_model
class User

  attr_accessor :name, :password

Now you can do familiar things with the class, just as you would do with normal ActiveRecord models. Supply a translation:

      user: gebruiker
        name: naam
        password: wachtwoord
# => "gebruiker"
# => "naam"

Of course, you can use inheritance, just as you would use Single Table Inheritance (STI).

It also works nicely together with i18n_label. A simple example:

%h1= translate :please_login
- form_for(User.new) do |f|
  = f.label :name
  = f.text_field :name
  = f.label :password
  = f.password_field :password
  = submit_tag( translate(:login) )

Hopefully, this will make your life a little bit easier.

comments powered byDisqus