Rails, Haml and gettext 0

Posted by Paul
on Saturday, March 08

The other day I started looking at altering an application I've written to be internationalised. This led me to decide on using gettext as my method of internationalisation. I had a few problems getting gettext up and running on my application and then getting it to play well with Haml. I thought I'd write a few notes about it.

My gettext and Rails Gotcha

Make sure that you have the gettext libraries installed on your machine. It would seem that the gettext gem does not do the work itself but palms it off to your system. This had me for a bit.

My gettext and Haml Gotcha

I found the solution for this through the Google cache, the original link looks dead.

The gettext gem has parsers for different types of files ($GETTEXT_GEM_FOLDER/lib/gettext/parser/), Haml is not included. I have the following in $APP/lib/haml_parser.rb.

# haml_parser.rb
require 'gettext/rgettext'
 
module HamlParser
  module_function
 
  def target?(file)
    File.extname(file) == ".haml"
  end
 
  def parse(file, ary = [])
    haml = Haml::Engine.new(IO.readlines(file).join)
    code = haml.precompiled.split(/$/)
    RubyParser.parse_lines(file, code, ary)
  end
end
 
GetText::RGetText.add_parser(HamlParser)

My updatepo rake task looks like:

desc "Update pot/po files to match new version."
task :updatepo do
  require 'gettext/utils'
  require 'haml_parser'
  GetText.update_pofiles("socialclub", Dir.glob("{app,lib}/**/*.{rb,haml}"), "socialclub 0.1.0")
end