+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");
+ }
+ }
+} #}}}
+
+sub needsbuild () { #{{{
+ my $needsbuild=shift;
+
+ # build %translations, using istranslation's side-effect
+ foreach my $page (keys %pagesources) {
+ istranslation($page);
+ }
+
+ # refresh/create POT and PO files as needed
+ my $updated_po_files=0;
+ foreach my $page (keys %pagesources) {
+ my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
+ if (istranslatable($page)) {
+ my $file=srcfile($pagesources{$page});
+ if ($pageneedsbuild || ! -e potfile($file)) {
+ refreshpot($file);
+ }
+ my @pofiles;
+ foreach my $lang (keys %{$config{po_slave_languages}}) {
+ my $pofile=pofile($file, $lang);
+ if ($pageneedsbuild || ! -e $pofile) {
+ push @pofiles, $pofile;
+ }
+ }
+ if (@pofiles) {
+ refreshpofiles($file, @pofiles) ;
+ map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
+ $updated_po_files = 1;
+ }
+ }
+ }
+
+ # check staged changes in and trigger a wiki refresh.
+ if ($updated_po_files) {
+ if ($config{rcs}) {
+ IkiWiki::disable_commit_hook();
+ IkiWiki::rcs_commit_staged(gettext("updated PO files"),
+ "refreshpofiles", "127.0.0.1");
+ IkiWiki::enable_commit_hook();
+ IkiWiki::rcs_update();
+ }
+ IkiWiki::refresh();
+ IkiWiki::saveindex();
+ # refresh module's private variables
+ undef %filtered;
+ undef %translations;
+ foreach my $page (keys %pagesources) {
+ istranslation($page);
+ }
+ }
+
+
+ # make existing translations depend on the corresponding master page
+ foreach my $master (keys %translations) {
+ foreach my $slave (values %{$translations{$master}}) {
+ add_depends($slave, $master);
+ }
+ }
+} #}}}
+
+sub mytargetpage ($$) { #{{{
+ my $page=shift;
+ my $ext=shift;