+sub pofiles ($) { #{{{
+ my $masterfile=shift;
+
+ return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
+} #}}}
+
+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->{TT}{utf_mode} = 1;
+ $doc->{TT}{file_in_charset} = 'utf-8';
+ $doc->{TT}{file_out_charset} = 'utf-8';
+ $doc->read($masterfile);
+ # 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;
+ IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
+ $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) {
+ IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
+ if (-e $pofile) {
+ system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 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 buildtranslationscache() { #{{{
+ # use istranslation's side-effect
+ map istranslation($_), (keys %pagesources);
+} #}}}
+
+sub resettranslationscache() { #{{{
+ undef %translations;
+} #}}}
+
+sub urlto_with_orig_beautiful_urlpath($$) { #{{{
+ my $to=shift;
+ my $from=shift;
+
+ inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
+ my $res=urlto($to, $from);
+ inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
+
+ return $res;
+} #}}}
+
+sub percenttranslated ($) { #{{{
+ my $page=shift;
+
+ return gettext("N/A") unless istranslation($page);
+ my $file=srcfile($pagesources{$page});
+ my $masterfile = srcfile($pagesources{masterpage($page)});
+ my (@pos,@masters);
+ push @pos,$file;
+ push @masters,$masterfile;
+ my %options = (
+ "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
+ );
+ my $doc=Locale::Po4a::Chooser::new('text',%options);
+ $doc->process(
+ 'po_in_name' => \@pos,
+ 'file_in_name' => \@masters,
+ 'file_in_charset' => 'utf-8',
+ 'file_out_charset' => 'utf-8',
+ ) or error("[po/percenttranslated:$page]: failed to translate");
+ my ($percent,$hit,$queries) = $doc->stats();
+ return $percent;
+} #}}}
+
+sub languagename ($) { #{{{
+ my $code=shift;
+
+ return $config{po_master_language}{name}
+ if $code eq $config{po_master_language}{code};
+ return $config{po_slave_languages}{$code}
+ if defined $config{po_slave_languages}{$code};
+ return;
+} #}}}
+
+sub otherlanguagesloop ($) { #{{{
+ my $page=shift;
+
+ my @ret;
+ my %otherpages=%{otherlanguages($page)};
+ while (my ($lang, $otherpage) = each %otherpages) {
+ if (istranslation($page) && masterpage($page) eq $otherpage) {
+ push @ret, {
+ url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
+ code => $lang,
+ language => languagename($lang),
+ master => 1,
+ };
+ }
+ else {
+ push @ret, {
+ url => urlto($otherpage, $page),
+ code => $lang,
+ language => languagename($lang),
+ percent => percenttranslated($otherpage),
+ }
+ }
+ }
+ return sort {
+ return -1 if $a->{code} eq $config{po_master_language}{code};
+ return 1 if $b->{code} eq $config{po_master_language}{code};
+ return $a->{language} cmp $b->{language};
+ } @ret;
+} #}}}
+
+sub homepageurl (;$) { #{{{
+ my $page=shift;
+
+ return urlto('', $page);
+} #}}}
+
+# ,----
+# | PageSpec's
+# `----
+
+package IkiWiki::PageSpec;
+use warnings;
+use strict;
+use IkiWiki 2.00;
+
+sub match_istranslation ($;@) { #{{{
+ my $page=shift;
+
+ if (IkiWiki::Plugin::po::istranslation($page)) {
+ return IkiWiki::SuccessReason->new("is a translation page");
+ }
+ else {
+ return IkiWiki::FailReason->new("is not a translation page");
+ }
+} #}}}
+
+sub match_istranslatable ($;@) { #{{{
+ my $page=shift;
+
+ if (IkiWiki::Plugin::po::istranslatable($page)) {
+ return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
+ }
+ else {
+ return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
+ }
+} #}}}
+
+sub match_lang ($$;@) { #{{{
+ my $page=shift;
+ my $wanted=shift;
+
+ my $regexp=IkiWiki::glob2re($wanted);
+ my $lang=IkiWiki::Plugin::po::lang($page);
+ if ($lang!~/^$regexp$/i) {
+ return IkiWiki::FailReason->new("file language is $lang, not $wanted");
+ }
+ else {
+ return IkiWiki::SuccessReason->new("file language is $wanted");
+ }
+} #}}}
+
+sub match_currentlang ($$;@) { #{{{
+ my $page=shift;
+ shift;
+ my %params=@_;
+
+ return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
+
+ my $currentlang=IkiWiki::Plugin::po::lang($params{location});
+ my $lang=IkiWiki::Plugin::po::lang($page);
+
+ if ($lang eq $currentlang) {
+ return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
+ }
+ else {
+ return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
+ }