+ return 0 unless defined $masterpage && defined $lang
+ && length $masterpage && length $lang
+ && defined $pagesources{$masterpage}
+ && defined $config{po_slave_languages}{$lang};
+
+ return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
+ if istranslatable($masterpage);
+}
+
+sub istranslation ($) {
+ my $page=shift;
+
+ if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
+ my $hasleadingslash = ($masterpage=~s#^/##);
+ $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
+ return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
+ }
+ return "";
+}
+
+sub masterpage ($) {
+ my $page=shift;
+
+ if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
+ return $masterpage;
+ }
+ return $page;
+}
+
+sub lang ($) {
+ my $page=shift;
+
+ if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
+ return $lang;
+ }
+ return $config{po_master_language}{code};
+}
+
+sub islanguagecode ($) {
+ my $code=shift;
+
+ return $code =~ /^[a-z]{2}$/;
+}
+
+sub otherlanguage ($$) {
+ my $page=shift;
+ my $code=shift;
+
+ return masterpage($page) if $code eq $config{po_master_language}{code};
+ return masterpage($page) . '.' . $code;
+}
+
+sub otherlanguages ($) {
+ my $page=shift;
+
+ my %ret;
+ return \%ret unless istranslation($page) || istranslatable($page);
+ my $curlang=lang($page);
+ foreach my $lang
+ ($config{po_master_language}{code}, keys %{$config{po_slave_languages}}) {
+ next if $lang eq $curlang;
+ $ret{$lang}=otherlanguage($page, $lang);
+ }
+ return \%ret;
+}
+
+sub potfile ($) {
+ my $masterfile=shift;
+
+ (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
+ $dir='' if $dir eq './';
+ return File::Spec->catpath('', $dir, $name . ".pot");
+}
+
+sub pofile ($$) {
+ my $masterfile=shift;
+ my $lang=shift;
+
+ (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
+ $dir='' if $dir eq './';
+ return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
+}
+
+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.
+ $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
+ $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);
+ if (! -e $potfile) {
+ error("po(refreshpofiles) ".sprintf(gettext("POT file (%s) does not exist"), $potfile));
+ }
+
+ foreach my $pofile (@pofiles) {
+ IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
+
+ if (! -e $pofile) {
+ # If the po file exists in an underlay, copy it
+ # from there.
+ my ($pobase)=$pofile=~/^\Q$config{srcdir}\E\/?(.*)$/;
+ foreach my $dir (@{$config{underlaydirs}}) {
+ if (-e "$dir/$pobase") {
+ File::Copy::syscopy("$dir/$pobase",$pofile)
+ or error("po(refreshpofiles) ".
+ sprintf(gettext("failed to copy underlay PO file to %s"),
+ $pofile));
+ }
+ }
+ }
+
+ if (-e $pofile) {
+ system("msgmerge", "--previous", "-q", "-U", "--backup=none", $pofile, $potfile) == 0
+ or error("po(refreshpofiles) ".
+ sprintf(gettext("failed to update %s"),
+ $pofile));
+ }
+ else {
+ File::Copy::syscopy($potfile,$pofile)
+ or error("po(refreshpofiles) ".
+ sprintf(gettext("failed to copy the POT file to %s"),
+ $pofile));
+ }
+ }
+}
+
+sub buildtranslationscache() {
+ # use istranslation's side-effect
+ map istranslation($_), (keys %pagesources);
+}
+
+sub resettranslationscache() {
+ undef %translations;
+}
+
+sub flushmemoizecache() {
+ Memoize::flush_cache("istranslatable");
+ Memoize::flush_cache("_istranslation");
+ Memoize::flush_cache("percenttranslated");
+}
+
+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;
+
+ $page=~s/^\///;
+ return gettext("N/A") unless istranslation($page);
+ my $file=srcfile($pagesources{$page});
+ my $masterfile = srcfile($pagesources{masterpage($page)});