-desc "Update pot/po files."
-task :updatepo do
- require 'gettext/utils'
- plugin_files = Dir.glob('data/rbot/plugins/**/*.rb')
- # all except plugin files use the rbot textdomain
- GetText.update_pofiles("rbot",
- Dir.glob("{lib,bin,data}/**/*.{rb,rhtml}") - plugin_files, "rbot")
- # each plugin uses its own textdomain
- plugin_files.each do |f|
- basename = File.basename(f, '.rb')
- GetText.update_pofiles("rbot-#{basename}", f, 'rbot')
+# normalize a po/pot file
+def normalize_po(fn)
+ content = File.read(fn)
+
+ # sort the messages by file location
+ if MSGCAT
+ sorted = `#{MSGCAT} --width=79 --sort-by-file #{fn}`
+ if sorted != content
+ content = sorted
+ modified = true
+ end
+ end
+
+ # replace project-id-version placholder
+ modified |= content.sub!(/^("Project-Id-Version: )PACKAGE VERSION(\\n")$/) {
+ "#{$1}rbot#{$2}"
+ }
+
+ if modified
+ File.open(fn, 'w') {|f| f.write content}
+ end
+end
+
+PLUGIN_FILES = FileList['data/rbot/plugins/**/*.rb']
+NON_PLUGIN_FILES = FileList["{lib,bin,data}/**/*.{rb,rhtml}"] - PLUGIN_FILES
+
+# this task defines how po files and pot files are made. those rules are not defined
+# normally because po and pot files should be only updated in the updatepo task,
+# but po files are also prereqs for makemo
+task :define_po_rules do
+ # generate pot file from rb files
+ rgettext_proc = proc do |t|
+ require 'gettext/utils'
+ source_files, pot_file = t.prerequisites, t.name
+ new_pot_file = "#{pot_file}.new"
+ puts "#{source_files.join(', ')} => #{pot_file}"
+ GetText.rgettext(source_files, new_pot_file)
+
+ # only use the new pot file if it contains unique messages
+ if File.exists?(pot_file) && MSGCOMM && `#{MSGCOMM} --unique #{pot_file} #{new_pot_file}`.empty?
+ rm new_pot_file
+ else
+ mv new_pot_file, pot_file
+ end
+
+ normalize_po(pot_file)
+
+ # save all this work until rb files are updated again
+ touch pot_file
+ end
+
+ # generate pot file for non-plugin files
+ file('po/rbot.pot' => NON_PLUGIN_FILES, &rgettext_proc)
+
+ # generate pot files for plugin files
+ rule(%r'^po/.+\.pot$' => proc {|fn|
+ PLUGIN_FILES.select {|f| f.pathmap('rbot-%n') == fn.pathmap('%n')}
+ }, &rgettext_proc)
+
+ # map the po file to its source pot file
+ pot_for_po = proc {|fn| fn.pathmap '%{^po/.+/,po/}X.pot'}
+
+ # update po file from pot file
+ msgmerge_proc = proc do |t|
+ require 'gettext/utils'
+ po_file, pot_file = t.name, t.source
+ puts "#{pot_file} => #{po_file}"
+ if File.exists? po_file
+ sh "#{MSGMERGE} --backup=off --update #{po_file} #{pot_file}"
+ elsif MSGINIT
+ locale = po_file[%r'^po/(.+)/.+\.po$', 1]
+ sh "#{MSGINIT} --locale=#{locale} --no-translator --input=#{pot_file} --output-file=#{po_file}"
+ else
+ warn "#{po_file} is missing and cannot be generated without msginit"
+ next
+ end
+ normalize_po(po_file)
+ touch po_file
+ end
+
+ # generate English po files
+ file(%r'^po/en_US/.+\.po$' => pot_for_po) do |t|
+ po_file, pot_file = t.name, t.source
+ if MSGEN
+ sh "#{MSGEN} --output-file=#{po_file} #{pot_file}"
+ normalize_po(po_file)
+ touch po_file
+ else
+ msgmerge_proc.call t
+ end