Aug 08

GetText to Gibberish rake task

Here is a rake task to update your models and controllers to use Gibberish instead of GetText. It would change _s(“Title|MyTitle”) to “MyTitle”[] . It would use GetText default translation so if Title|MyTitle is translated to “My Beautiful Title” it would add this instead.

to_gibberish would overwrite files so backup files before.

Usage:

rake to_gibberish FILES=app/views/controller/index.rhtml rake to_gibberish FILES=app/views/*/.rhtml

Code


desc "Simple script to migrate gettext default lang to gibberish." 
task :to_gibberish do
  require 'rubygems'
  require 'gettext'
  GetText.bindtextdomain('myapp', :path => '/home/rails/myapp/locale')
  Dir.glob(ENV["FILES"]).each do |filename|
    str = File.open(filename) {|f| f.read }
    str = str.gsub(/s?_\(["']((?:[^\|"']+\|)*([^"']+))["']\)/) { |s|
      '"' + GetText.s_($2) + '"[]'
    }
    File.open(filename, "w") {|f| f.write(str) }
  end
end