+sub potfile ($) { #{{{
+ my $masterfile=shift;
+ (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
+ return File::Spec->catfile($dir, $name . ".pot");
+} #}}}
+
+sub pofile ($$) { #{{{
+ my $masterfile=shift;
+ my $lang=shift;
+ (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
+ return File::Spec->catfile($dir, $name . "." . $lang . ".po");
+} #}}}
+
+sub refreshpot ($) { #{{{
+ my $masterfile=shift;
+ my $potfile=potfile($masterfile);
+ my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
+ my $doc=Locale::Po4a::Chooser::new('text',%options);
+ $doc->read($masterfile);
+ $doc->{TT}{utf_mode} = 1;
+ $doc->{TT}{file_in_charset} = 'utf-8';
+ $doc->{TT}{file_out_charset} = 'utf-8';
+ # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
+ # this is undocument use of internal Locale::Po4a::TransTractor's data,
+ # compulsory since this module prevents us from using the porefs option.
+ my %po_options = ('porefs' => 'none');
+ $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
+ $doc->{TT}{po_out}->set_charset('utf-8');
+ # do the actual work
+ $doc->parse;
+ $doc->writepo($potfile);
+} #}}}
+
+sub refreshpofiles ($@) { #{{{
+ my $masterfile=shift;
+ my @pofiles=@_;
+
+ my $potfile=potfile($masterfile);
+ error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
+
+ foreach my $pofile (@pofiles) {
+ if (-e $pofile) {
+ my $cmd = "msgmerge -U --backup=none $pofile $potfile";
+ system ($cmd) == 0
+ or error("[po/refreshpofiles:$pofile] failed to update");
+ }
+ else {
+ File::Copy::syscopy($potfile,$pofile)
+ or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
+ }
+ }
+} #}}}
+