+# generate mo files
+rule(%r'^data/locale/.+/LC_MESSAGES/.+\.mo$' => proc {|fn|
+ [ fn.pathmap('%{^data/locale,po;LC_MESSAGES/,}X.po'),
+ # the directory is created if not existing
+ fn.pathmap('%d') ]
+}) do |t|
+ po_file, mo_file = t.source, t.name
+ puts "#{po_file} => #{mo_file}"
+ require 'gettext/utils'
+ GetText.rmsgfmt po_file, mo_file
+end
+
+task :check_po_tools do
+ have = {}
+
+ po_tools = {
+ 'msgmerge' => {
+ :options => %w[--backup= --update],
+ :message => 'Cannot update po files' },
+ 'msginit' => {
+ :options => %w[--locale= --no-translator --input= --output-file=],
+ :message => 'Cannot generate missing po files' },
+ 'msgcomm' => {
+ :options => %w[--unique],
+ :message => 'Pot files may be modified even without message change' },
+ 'msgen' => {
+ :options => %w[--output-file],
+ :message => 'English po files will not be generated' },
+ 'msgcat' => {
+ :options => %w[--width= --sort-by-file],
+ :message => 'Pot files will not be normalized' }
+ }
+
+ po_tools.each_pair do |command, value|
+ path = ENV["#{command.upcase}_PATH"] || command
+ have_it = have[command] = value[:options].all? do |option|
+ `#{path} --help`.include? option
+ end
+ Object.const_set(command.upcase, have_it ? path : false)
+ warn "#{command} not found. #{value[:message]}" unless have_it
+ end
+ abort unless MSGMERGE
+end
+
+PLUGIN_BASENAMES = PLUGIN_FILES.map {|f| f.pathmap('%n')}
+LOCALES = FileList['po/*/'].map {|d| d.pathmap('%n')}
+
+LOCALES.each do |l|
+ directory "data/locale/#{l}/LC_MESSAGES"
+end
+
+desc 'Update po files'
+task :updatepo => [:define_po_rules, :check_po_tools] + LOCALES.map {|l|
+ ["po/#{l}/rbot.po"] +
+ PLUGIN_BASENAMES.map {|n| "po/#{l}/rbot-#{n}.po"}
+}.flatten
+
+desc 'Normalize po files'
+task :normalizepo => :check_po_tools do
+ FileList['po/*/*.po'].each {|fn| normalize_po(fn)}
+end
+
+# this task invokes makemo if ruby-gettext is available, but otherwise succeeds
+# with a warning instead of failing. it is to be used by Gem's extension builder
+# to make installation not fail because of lack of ruby-gettext
+task :buildext do
+ begin
+ require 'gettext/utils'
+ Rake::Task[:makemo].invoke
+ rescue LoadError
+ warn 'Ruby-gettext cannot be located, so mo files cannot be built and installed'
+ end
+end
+
+desc 'Generate mo files'
+task :makemo =>
+ FileList['po/*/*.po'].pathmap('%{^po,data/locale}d/LC_MESSAGES/%n.mo')
+
+Dir['tasks/**/*.rake'].each { |t| load t }