+ if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
+ return $lang;
+ }
+ return $master_language_code;
+}
+
+sub islanguagecode ($) {
+ my $code=shift;
+
+ return $code =~ /^[a-z]{2}$/;
+}
+
+sub otherlanguage_page ($$) {
+ my $page=shift;
+ my $code=shift;
+
+ return masterpage($page) if $code eq $master_language_code;
+ return masterpage($page) . '.' . $code;
+}
+
+# Returns the list of other languages codes: the master language comes first,
+# then the codes are ordered the same way as in po_slave_languages, if it is
+# an array, or in the language name lexical order, if it is a hash.
+sub otherlanguages_codes ($) {
+ my $page=shift;
+
+ my @ret;
+ return \@ret unless istranslation($page) || istranslatable($page);
+ my $curlang=lang($page);
+ foreach my $lang
+ ($master_language_code, @slavelanguages) {
+ next if $lang eq $curlang;
+ if ($lang eq $master_language_code ||
+ istranslatedto(masterpage($page), $lang)) {
+ push @ret, $lang;
+ }
+ }
+ return \@ret;
+}
+
+sub otherlanguages_pages ($) {
+ my $page=shift;
+
+ my %ret;
+ map {
+ $ret{$_} = otherlanguage_page($page, $_)
+ } @{otherlanguages_codes($page)};
+
+ 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, $_), @slavelanguages;
+}
+
+sub refreshpot ($) {
+ my $masterfile=shift;
+
+ my $potfile=potfile($masterfile);
+ my $doc=Locale::Po4a::Chooser::new(po4a_type($masterfile),
+ po4a_options($masterfile));
+ $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);