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.


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


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 = {|f| }
    str = str.gsub(/s?_\(["']((?:[^\|"']+\|)*([^"']+))["']\)/) { |s|
      '"' + GetText.s_($2) + '"[]'
    }, "w") {|f| f.write(str) }