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 ).
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!