HOWTO write your own message parsers
ruby-gettext-howto-poparser
Since Ruby-GetText-1.8.0
You can add your own message parsers to extract msgid and create a po-file. In this article, I introduce how write the parser and how use it.
Write a parser
Define a parser module which includes two methods(target?, parser), then add the parser to GetText::RGetText.
require 'gettext/rgettext'
module TestParser
module_function
# If the file is the target of your parser, then return true, otherwise false.
def target?(file)
File.extname(file) == ".csv" # This parser targets csv files only.
end
# Parse a file and return the array of [msgid, file1, file2, ...].
# ary includes the result of other parsers. The new results should be
# added to this ary.
# And this method is required to keep a msgid as unique. Check the
# msgid becomes unique before adding the msgid to ary.
#
# Return value format is:
# [["msgid1", "file1:line1", "file2:line2",...],
# ["msgid2", "file3:line3",...]]
#
def parse(file, ary)
:
:
:
ary << ["aaa", "foo.rb:200"]
ary << ["bbb", "bar.rb:300", "baz.rb:400"]
ary
end
end
# Add this parser to GetText::RGetText
GetText::RGetText.add_parser(TestParser)
Save this code as testparser.rb for the next step.
How to use testparser.rb
With rgettext
$ rgettext -rtestparser targetfiles1.rb targetfiles2.rb .... -o foo.pot
With Rake
Only add "require 'testparser'" to the updatepo task.
desc "Update pot/po files."
task :updatepo do
require 'gettext/utils' #It may be better to move this into testparser.rb
require 'testparser'
GetText.update_pofiles("myapp", Dir.glob("app/**/*.{rb,rhtml}"), "myapp 0.0.1")
end
ChangeLog
- 2006-12-19 Created - Masao
Keyword(s):
References:[Ruby-GetText-Package document for Developers]