# Very strange problem with Gettext and Haml

**URL:** <https://www.ruby-forum.com/t/very-strange-problem-with-gettext-and-haml/196987>\
**Category:** Rails I18n\
**Created:** [August 5, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/very-strange-problem-with-gettext-and-haml/196987 "2009-08-05T19:30:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [August 5, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/very-strange-problem-with-gettext-and-haml/196987/1 "2009-08-05T19:30:11Z")

</div>

Hi,

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  
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(/$/)  
GetText::RubyParser.parse\_lines(file, code, ary)  
end  
end

GetText::RGetText.add\_parser(HamlParser)

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”)  
end

desc “Update pot/po files to match new version.”  
task :updatepo do  
require ‘gettext\_rails/tools’  
require ‘haml\_parser’  
MY\_APP\_TEXT\_DOMAIN = “APP”  
MY\_APP\_VERSION = “APP 1.1.0”  
GetText.update\_pofiles(MY\_APP\_TEXT\_DOMAIN,  
Dir.glob(“{app,lib}/\*\*/\*.{rb,rhtml,html.erb,haml,html.haml,rjs}”),  
MY\_APP\_VERSION)  
end

This follows the known approach for parsing Haml files (  
[http://www.paulgillard.me.uk/2008/3/8/rails-haml-and-gettext](http://www.paulgillard.me.uk/2008/3/8/rails-haml-and-gettext) ).

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.new(IO.readlines(file).join)  
> > =\> #\<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!

Thanks!

---

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [October 16, 2009, 8:47am UTC](https://www.ruby-forum.com/t/very-strange-problem-with-gettext-and-haml/196987/2 "2009-10-16T08:47:17Z")

</div>

Try modifying your haml\_parser.rb to see where the parser gets stuck.

def parse(file, ary = [])  
haml = Haml::Engine.new(IO.readlines(file).join)  
code = haml.precompiled.split(/$/)  
puts “Parsing Haml File: #{file}”  
RubyParser.parse\_lines(file, code, ary)  
end

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  
RubyParser.

I have tried the gems  
retoo-ruby\_gettext\_extractor (0.2.1)  
that depends on  
ruby\_parser  
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  
module\_function

def target?(file)  
File.extname(file) == ‘.haml’  
end

def parse(file, ary = [])  
bypass = ! File.basename(file,  
‘.haml’).match(/(vi|zh|zh\_HK|id|th)$/).nil?  
puts “HamlParser:#{file}:bypass:#{bypass}”  
return ary if bypass

```
haml = Haml::Engine.new(IO.readlines(file).join)
result = nil
begin
  #result = GetText::RubyParser.parse_lines(file, 

```

haml.precompiled.split(/$/), ary)  
result = RubyGettextExtractor.parse\_string(haml.precompiled, file,  
ary)  
rescue Exception =\> e  
puts “Error:#{file}”  
raise e  
end  
result  
end  
end

GetText::RGetText.add\_parser(HamlParser)
