Articles // Rails 2.2.2 I18n helper

Since Gettext plugin does not work with Rails 2.2.2 anymore, and I don't like the syntax of bundled I18n, I've made a mixin to extend functionality of String class with .t method:

module Ppds
  module CoreExtensions
    module String
      module TextHelper
        def t(*args)
          I18n.t self, :default => self.to_s, :value => args
        end
      end
    end
  end
end

class String
  include Ppds::CoreExtensions::String::TextHelper
end

Now I can write

"Hello, world!".t

and get either a translation if one exists, or the original phrase, like Gettext behaves.

It also let you translate along with substitution:

first_name = "Paul"
"My name is %s".t(first_name)

Comments // 0

Leave a comment

There are no comments yet. Be the first to leave one.

Tags

ruby, rails, gettext, i18n

Technorati Delicious Flickr

Published on December 03, 2008

over 1 year ago

This article has 0 comments