HOWTO write your own message parsers
ruby-gettext-howto-poparser
Since Ruby-GetText-2.1.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 PoMessages.
def parse(file)
:
:
:
ary = []
# Simple message
po = PoMessage.new(:normal)
po.msgid = "hello"
po.sources = ["foo.rb:200", "bar.rb:300"]
po.add_comment("Comment for the message")
ary << po
# Plural message
po = PoMessage.new(:plural)
po.msgid = "An apple"
po.msgid_plural = "Apples"
po.sources = ["foo.rb:200", "bar.rb:300"]
ary << po
# Simple message with the message context
po = PoMessage.new(:msgctxt)
po.msgctxt = "context"
po.msgid = "hello"
po.sources = ["foo.rb:200", "bar.rb:300"]
ary << po
# Plural message with the message context.
po = PoMessage.new(:msgctxt_plural)
po.msgctxt = "context"
po.msgid = "An apple"
po.msgid_plural = "Apples"
po.sources = ["foo.rb:200", "bar.rb:300"]
ary << po
return 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
- 2010-01-02 Updated for 2.1.0 - Masao
- 2006-12-19 Created - Masao
Keyword(s):
References:[HOWTO write your own message parsers(old)] [Ruby-GetText for Ruby on Rails] [Ruby-GetText-Package document for Developers]