Rails 2.2.2 I18n helper

Since Gettext plugin does not work with Rails 2.2.2 any more, and I don’t like the syntax of bundled I18n, I’ve made a mix-in 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)