+sub percenttranslated ($) { #{{{
+ my $page=shift;
+ return "N/A" unless (istranslation($page));
+ my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ my $file=srcfile($pagesources{$page});
+ my $masterfile = srcfile($pagesources{$masterpage});
+ 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:$file]: failed to translate");
+ my ($percent,$hit,$queries) = $doc->stats();
+ return $percent;
+} #}}}
+
+sub otherlanguages ($) { #{{{
+ my $page=shift;
+ my @ret;
+ if (istranslatable($page)) {
+ foreach my $lang (sort keys %{$translations{$page}}) {
+ my $translation = $translations{$page}{$lang};
+ push @ret, {
+ url => urlto($translation, $page),
+ code => $lang,
+ language => $config{po_slave_languages}{$lang},
+ percent => percenttranslated($translation),
+ };
+ }
+ }
+ elsif (istranslation($page)) {
+ my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ push @ret, {
+ url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
+ code => $config{po_master_language}{code},
+ language => $config{po_master_language}{name},
+ master => 1,
+ };
+ foreach my $lang (sort keys %{$translations{$masterpage}}) {
+ push @ret, {
+ url => urlto($translations{$masterpage}{$lang}, $page),
+ code => $lang,
+ language => $config{po_slave_languages}{$lang},
+ percent => percenttranslated($translations{$masterpage}{$lang}),
+ } unless ($lang eq $curlang);
+ }
+ }
+ return @ret;
+} #}}}
+
+sub pagetemplate (@) { #{{{
+ my %params=@_;
+ my $page=$params{page};
+ my $destpage=$params{destpage};
+ my $template=$params{template};
+
+ if (istranslation($page) && $template->query(name => "percenttranslated")) {
+ $template->param(percenttranslated => percenttranslated($page));
+ }
+ if ($template->query(name => "istranslation")) {
+ $template->param(istranslation => istranslation($page));
+ }
+ if ($template->query(name => "istranslatable")) {
+ $template->param(istranslatable => istranslatable($page));
+ }
+ if ($template->query(name => "otherlanguages")) {
+ $template->param(otherlanguages => [otherlanguages($page)]);
+ if (istranslatable($page)) {
+ foreach my $translation (values %{$translations{$page}}) {
+ add_depends($page, $translation);
+ }
+ }
+ elsif (istranslation($page)) {
+ my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ add_depends($page, $masterpage);
+ foreach my $translation (values %{$translations{$masterpage}}) {
+ add_depends($page, $translation);
+ }
+ }
+ }
+ # Rely on IkiWiki::Render's genpage() to decide wether
+ # a discussion link should appear on $page; this is not
+ # totally accurate, though: some broken links may be generated
+ # when cgiurl is disabled.
+ # This compromise avoids some code duplication, and will probably
+ # prevent future breakage when ikiwiki internals change.
+ # Known limitations are preferred to future random bugs.
+ if ($template->param('discussionlink') && istranslation($page)) {
+ my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ $template->param('discussionlink' => htmllink(
+ $page,
+ $destpage,
+ $masterpage . '/' . gettext("Discussion"),
+ noimageinline => 1,
+ forcesubpage => 0,
+ linktext => gettext("Discussion"),
+ ));
+ }
+} # }}}
+