Forum: Rails I18n Very strange problem with Gettext and Haml

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
53832d2238f805ba62c143fa77a3bce0?d=identicon&s=25 Olaf S. (olafson)
on 2009-08-05 19:30

I'm on Rails 2.3.3 and using Haml 2.0.9 for my templates and
Gettext-Rails 2.0.4 for the translation. Haml works like a charm and
gettext is also working like it should.

But I cant get Gettext to parse Haml Files when using "rake updatepo". I
created a custom parser like this:

# lib/haml_parser.rb
require 'gettext_rails/tools'
require 'haml'
# Haml gettext parser
module HamlParser

  def target?(file)
    File.extname(file) == ".haml"

  def parse(file, ary = [])
    haml =
    code = haml.precompiled.split(/$/)
    GetText::RubyParser.parse_lines(file, code, ary)


My Rakefile looks like this:

# Rakefile
require(File.join(File.dirname(__FILE__), 'config', 'boot'))

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require 'tasks/rails'

desc "Create mo-files for L10n"
task :makemo do
  require 'gettext_rails/tools'
  GetText.create_mofiles(true)  #(true, "po", "locale")

desc "Update pot/po files to match new version."
task :updatepo do
  require 'gettext_rails/tools'
  require 'haml_parser'
  MY_APP_VERSION     = "APP 1.1.0"

This follows the known approach for parsing Haml files ( ).

The problem: No MessageIds are recognized from my Haml files. I checked
with "puts" in the Haml-Parser if it tried the right files, could parse
them and so on. Everything seemed to be fine, it just recognize anything
and always returned only the already found msgids and for the Haml file
an empty Array.

The strange thing: When I enter this in my console, everything works:

$$ script/console
Loading development environment (Rails 2.3.3)
>> require 'gettext_rails/tools'
=> []
>> require 'haml'
=> []
>> file = "app/views/sessions/new.haml"
=> "app/views/sessions/new.haml"
>> haml =
=> #<Haml::Engine:0x4254104 @tab_change=0, @block_opened=false, @inden
>> code = haml.precompiled.split(/$/)
=> [" content_for :head do;", "\nhaml_temp =  stylesheet_link [...]
>> GetText::RubyParser.parse_lines(file, code, [])
=> [["Login", "app/views/sessions/new.haml:4"], [...]

As you can see everything works here (I left out the long returns). I'm
kind of freaking out why this isn't the case in my Rake Task.

Anyone has an idea? You would really make me a happy Nerd!

D2d502d902f4106951218c98e2ff3a8d?d=identicon&s=25 Sean T. (seantan)
on 2009-10-16 08:47
Try modifying your haml_parser.rb to see where the parser gets stuck.

  def parse(file, ary = [])
    haml =
    code = haml.precompiled.split(/$/)
    puts "Parsing Haml File: #{file}"
    RubyParser.parse_lines(file, code, ary)

I'm having problems of my own in upgrading my setup.
Rails 2.3.4
gettext 2.0.4
haml 2.2.8

From what I have gathered it is largely due to the change of in HAML 2.2
Engine that made it's output unparseable by the gettext's default

I have tried the gems
retoo-ruby_gettext_extractor (0.2.1)
that depends on
without success.

This ruby_parser has a RubyParser class of it's own and conflicts with
Gettext's RubyParser module. So here's a classical case of why we should
namespace our code. -_-

After hacking it to work by changing the names of the RubyParser in the
gems, I can make it work to parse en template files, using retoo's
gettext_extractor/rubyparser gems. However, the parser doesn't work on
my non-latin template files. My HamlParser class as below. Anyone can
have a go and fix this? A million thanks!

# haml_parser.rb
require 'rubygems'
require 'haml'
require 'gettext_rails/tools'
require 'ruby_gettext_extractor'

module HamlParser

  def target?(file)
    File.extname(file) == '.haml'

  def parse(file, ary = [])
    bypass = ! File.basename(file,
    puts "HamlParser:#{file}:bypass:#{bypass}"
    return ary if bypass

    haml =
    result = nil
      #result = GetText::RubyParser.parse_lines(file,
haml.precompiled.split(/$/), ary)
      result = RubyGettextExtractor.parse_string(haml.precompiled, file,
    rescue Exception => e
      puts "Error:#{file}"
      raise e

This topic is locked and can not be replied to.