]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/po.pm
a6342c74f93db9f882c043467339b99b2cf04375
[ikiwiki.git] / IkiWiki / Plugin / po.pm
1 #!/usr/bin/perl
2 # .po as a wiki page type
3 # Licensed under GPL v2 or greater
4 # Copyright (C) 2008 intrigeri <intrigeri@boum.org>
5 # inspired by the GPL'd po4a-translate,
6 # which is Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
7 package IkiWiki::Plugin::po;
8
9 use warnings;
10 use strict;
11 use IkiWiki 2.00;
12 use Encode;
13 use Locale::Po4a::Chooser;
14 use Locale::Po4a::Po;
15 use File::Basename;
16 use File::Copy;
17 use File::Spec;
18 use File::Temp;
19 use Memoize;
20 use UNIVERSAL;
21
22 my %translations;
23 my @origneedsbuild;
24 my %origsubs;
25
26 memoize("istranslatable");
27 memoize("_istranslation");
28 memoize("percenttranslated");
29
30 sub import { #{{{
31         hook(type => "getsetup", id => "po", call => \&getsetup);
32         hook(type => "checkconfig", id => "po", call => \&checkconfig);
33         hook(type => "needsbuild", id => "po", call => \&needsbuild);
34         hook(type => "scan", id => "po", call => \&scan, last =>1);
35         hook(type => "filter", id => "po", call => \&filter);
36         hook(type => "htmlize", id => "po", call => \&htmlize);
37         hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
38         hook(type => "change", id => "po", call => \&change);
39         hook(type => "editcontent", id => "po", call => \&editcontent);
40
41         $origsubs{'bestlink'}=\&IkiWiki::bestlink;
42         inject(name => "IkiWiki::bestlink", call => \&mybestlink);
43         $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
44         inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
45         $origsubs{'targetpage'}=\&IkiWiki::targetpage;
46         inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
47         $origsubs{'urlto'}=\&IkiWiki::urlto;
48         inject(name => "IkiWiki::urlto", call => \&myurlto);
49 } #}}}
50
51
52 # ,----
53 # | Table of contents
54 # `----
55
56 # 1. Hooks
57 # 2. Injected functions
58 # 3. Blackboxes for private data
59 # 4. Helper functions
60 # 5. PageSpec's
61
62
63 # ,----
64 # | Hooks
65 # `----
66
67 sub getsetup () { #{{{
68         return
69                 plugin => {
70                         safe => 0,
71                         rebuild => 1,
72                 },
73                 po_master_language => {
74                         type => "string",
75                         example => {
76                                 'code' => 'en',
77                                 'name' => 'English'
78                         },
79                         description => "master language (non-PO files)",
80                         safe => 1,
81                         rebuild => 1,
82                 },
83                 po_slave_languages => {
84                         type => "string",
85                         example => {
86                                 'fr' => 'Français',
87                                 'es' => 'Castellano',
88                                 'de' => 'Deutsch'
89                         },
90                         description => "slave languages (PO files)",
91                         safe => 1,
92                         rebuild => 1,
93                 },
94                 po_translatable_pages => {
95                         type => "pagespec",
96                         example => "!*/Discussion",
97                         description => "PageSpec controlling which pages are translatable",
98                         link => "ikiwiki/PageSpec",
99                         safe => 1,
100                         rebuild => 1,
101                 },
102                 po_link_to => {
103                         type => "string",
104                         example => "current",
105                         description => "internal linking behavior (default/current/negotiated)",
106                         safe => 1,
107                         rebuild => 1,
108                 },
109 } #}}}
110
111 sub checkconfig () { #{{{
112         foreach my $field (qw{po_master_language po_slave_languages}) {
113                 if (! exists $config{$field} || ! defined $config{$field}) {
114                         error(sprintf(gettext("Must specify %s"), $field));
115                 }
116         }
117         if (! (keys %{$config{po_slave_languages}})) {
118                 error(gettext("At least one slave language must be defined in po_slave_languages"));
119         }
120         map {
121                 islanguagecode($_)
122                         or error(sprintf(gettext("%s is not a valid language code"), $_));
123         } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
124         if (! exists $config{po_translatable_pages} ||
125             ! defined $config{po_translatable_pages}) {
126                 $config{po_translatable_pages}="";
127         }
128         if (! exists $config{po_link_to} ||
129             ! defined $config{po_link_to}) {
130                 $config{po_link_to}='default';
131         }
132         elsif (! grep {
133                         $config{po_link_to} eq $_
134                 } ('default', 'current', 'negotiated')) {
135                 warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
136                                 $config{po_link_to}));
137                 $config{po_link_to}='default';
138         }
139         elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
140                 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
141                 $config{po_link_to}='default';
142         }
143         push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
144 } #}}}
145
146 sub needsbuild () { #{{{
147         my $needsbuild=shift;
148
149         # backup @needsbuild content so that change() can know whether
150         # a given master page was rendered because its source file was changed
151         @origneedsbuild=(@$needsbuild);
152
153         buildtranslationscache();
154
155         # make existing translations depend on the corresponding master page
156         foreach my $master (keys %translations) {
157                 map add_depends($_, $master), values %{otherlanguages($master)};
158         }
159 } #}}}
160
161 # Massage the recorded state of internal links so that:
162 # - it matches the actually generated links, rather than the links as written
163 #   in the pages' source
164 # - backlinks are consistent in all cases
165 sub scan (@) { #{{{
166         my %params=@_;
167         my $page=$params{page};
168         my $content=$params{content};
169
170         return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
171
172         if (istranslation($page)) {
173                 foreach my $destpage (@{$links{$page}}) {
174                         if (istranslatable($destpage)) {
175                                 # replace one occurence of $destpage in $links{$page}
176                                 # (we only want to replace the one that was added by
177                                 # IkiWiki::Plugin::link::scan, other occurences may be
178                                 # there for other reasons)
179                                 for (my $i=0; $i<@{$links{$page}}; $i++) {
180                                         if (@{$links{$page}}[$i] eq $destpage) {
181                                                 @{$links{$page}}[$i] = $destpage . '.' . lang($page);
182                                                 last;
183                                         }
184                                 }
185                         }
186                 }
187         }
188         elsif (! istranslatable($page) && ! istranslation($page)) {
189                 foreach my $destpage (@{$links{$page}}) {
190                         if (istranslatable($destpage)) {
191                                 # make sure any destpage's translations has
192                                 # $page in its backlinks
193                                 push @{$links{$page}},
194                                         values %{otherlanguages($destpage)};
195                         }
196                 }
197         }
198 } #}}}
199
200 # We use filter to convert PO to the master page's format,
201 # since the rest of ikiwiki should not work on PO files.
202 sub filter (@) { #{{{
203         my %params = @_;
204
205         my $page = $params{page};
206         my $destpage = $params{destpage};
207         my $content = decode_utf8(encode_utf8($params{content}));
208
209         return $content if ( ! istranslation($page)
210                              || alreadyfiltered($page, $destpage) );
211
212         # CRLF line terminators make poor Locale::Po4a feel bad
213         $content=~s/\r\n/\n/g;
214
215         # Implementation notes
216         #
217         # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
218         #    to learn how to disguise a variable as a file.
219         # 2. There are incompatibilities between some File::Temp versions
220         #    (including 0.18, bundled with Lenny's perl-modules package)
221         #    and others (e.g. 0.20, previously present in the archive as
222         #    a standalone package): under certain circumstances, some
223         #    return a relative filename, whereas others return an absolute one;
224         #    we here use this module in a way that is at least compatible
225         #    with 0.18 and 0.20. Beware, hit'n'run refactorers!
226         my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
227                                     DIR => File::Spec->tmpdir,
228                                     UNLINK => 1)->filename;
229         my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
230                                      DIR => File::Spec->tmpdir,
231                                      UNLINK => 1)->filename;
232
233         writefile(basename($infile), File::Spec->tmpdir, $content);
234
235         my $masterfile = srcfile($pagesources{masterpage($page)});
236         my (@pos,@masters);
237         push @pos,$infile;
238         push @masters,$masterfile;
239         my %options = (
240                 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
241         );
242         my $doc=Locale::Po4a::Chooser::new('text',%options);
243         $doc->process(
244                 'po_in_name'    => \@pos,
245                 'file_in_name'  => \@masters,
246                 'file_in_charset'  => 'utf-8',
247                 'file_out_charset' => 'utf-8',
248         ) or error("[po/filter:$page]: failed to translate");
249         $doc->write($outfile) or error("[po/filter:$page] could not write $outfile");
250         $content = readfile($outfile) or error("[po/filter:$page] could not read $outfile");
251
252         # Unlinking should happen automatically, thanks to File::Temp,
253         # but it does not work here, probably because of the way writefile()
254         # and Locale::Po4a::write() work.
255         unlink $infile, $outfile;
256
257         setalreadyfiltered($page, $destpage);
258         return $content;
259 } #}}}
260
261 sub htmlize (@) { #{{{
262         my %params=@_;
263
264         my $page = $params{page};
265         my $content = $params{content};
266
267         # ignore PO files this plugin did not create
268         return $content unless istranslation($page);
269
270         # force content to be htmlize'd as if it was the same type as the master page
271         return IkiWiki::htmlize($page, $page,
272                                 pagetype(srcfile($pagesources{masterpage($page)})),
273                                 $content);
274 } #}}}
275
276 sub pagetemplate (@) { #{{{
277         my %params=@_;
278         my $page=$params{page};
279         my $destpage=$params{destpage};
280         my $template=$params{template};
281
282         my ($masterpage, $lang) = istranslation($page);
283
284         if (istranslation($page) && $template->query(name => "percenttranslated")) {
285                 $template->param(percenttranslated => percenttranslated($page));
286         }
287         if ($template->query(name => "istranslation")) {
288                 $template->param(istranslation => scalar istranslation($page));
289         }
290         if ($template->query(name => "istranslatable")) {
291                 $template->param(istranslatable => istranslatable($page));
292         }
293         if ($template->query(name => "HOMEPAGEURL")) {
294                 $template->param(homepageurl => homepageurl($page));
295         }
296         if ($template->query(name => "otherlanguages")) {
297                 $template->param(otherlanguages => [otherlanguagesloop($page)]);
298                 map add_depends($page, $_), (values %{otherlanguages($page)});
299         }
300         # Rely on IkiWiki::Render's genpage() to decide wether
301         # a discussion link should appear on $page; this is not
302         # totally accurate, though: some broken links may be generated
303         # when cgiurl is disabled.
304         # This compromise avoids some code duplication, and will probably
305         # prevent future breakage when ikiwiki internals change.
306         # Known limitations are preferred to future random bugs.
307         if ($template->param('discussionlink') && istranslation($page)) {
308                 $template->param('discussionlink' => htmllink(
309                                                         $page,
310                                                         $destpage,
311                                                         $masterpage . '/' . gettext("Discussion"),
312                                                         noimageinline => 1,
313                                                         forcesubpage => 0,
314                                                         linktext => gettext("Discussion"),
315                                                         ));
316         }
317         # Remove broken parentlink to ./index.html on home page's translations.
318         # It works because this hook has the "last" parameter set, to ensure it
319         # runs after parentlinks' own pagetemplate hook.
320         if ($template->param('parentlinks')
321             && istranslation($page)
322             && $masterpage eq "index") {
323                 $template->param('parentlinks' => []);
324         }
325 } # }}}
326
327 sub change(@) { #{{{
328         my @rendered=@_;
329
330         my $updated_po_files=0;
331
332         # Refresh/create POT and PO files as needed.
333         foreach my $page (map pagename($_), @rendered) {
334                 next unless istranslatable($page);
335                 my $file=srcfile($pagesources{$page});
336                 my $updated_pot_file=0;
337                 # Only refresh Pot file if it does not exist, or if
338                 # $pagesources{$page} was changed: don't if only the HTML was
339                 # refreshed, e.g. because of a dependency.
340                 if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
341                     || ! -e potfile($file)) {
342                         refreshpot($file);
343                         $updated_pot_file=1;
344                 }
345                 my @pofiles;
346                 map {
347                         push @pofiles, $_ if ($updated_pot_file || ! -e $_);
348                 } (pofiles($file));
349                 if (@pofiles) {
350                         refreshpofiles($file, @pofiles);
351                         map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
352                         $updated_po_files=1;
353                 }
354         }
355
356         if ($updated_po_files) {
357                 # Check staged changes in.
358                 if ($config{rcs}) {
359                         IkiWiki::disable_commit_hook();
360                         IkiWiki::rcs_commit_staged(gettext("updated PO files"),
361                                 "IkiWiki::Plugin::po::change", "127.0.0.1");
362                         IkiWiki::enable_commit_hook();
363                         IkiWiki::rcs_update();
364                 }
365                 # Reinitialize module's private variables.
366                 resetalreadyfiltered();
367                 resettranslationscache();
368                 flushmemoizecache();
369                 # Trigger a wiki refresh.
370                 require IkiWiki::Render;
371                 # without preliminary saveindex/loadindex, refresh()
372                 # complains about a lot of uninitialized variables
373                 IkiWiki::saveindex();
374                 IkiWiki::loadindex();
375                 IkiWiki::refresh();
376                 IkiWiki::saveindex();
377         }
378 } #}}}
379
380 # As we're previewing or saving a page, the content may have
381 # changed, so tell the next filter() invocation it must not be lazy.
382 sub editcontent () { #{{{
383         my %params=@_;
384
385         unsetalreadyfiltered($params{page}, $params{page});
386         return $params{content};
387 } #}}}
388
389
390 # ,----
391 # | Injected functions
392 # `----
393
394 # Implement po_link_to 'current' and 'negotiated' settings.
395 sub mybestlink ($$) { #{{{
396         my $page=shift;
397         my $link=shift;
398
399         my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
400         if (length $res
401             && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
402             && istranslatable($res)
403             && istranslation($page)) {
404                 return $res . "." . lang($page);
405         }
406         return $res;
407 } #}}}
408
409 sub mybeautify_urlpath ($) { #{{{
410         my $url=shift;
411
412         my $res=$origsubs{'beautify_urlpath'}->($url);
413         if ($config{po_link_to} eq "negotiated") {
414                 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
415                 $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
416                 map {
417                         $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
418                 } (keys %{$config{po_slave_languages}});
419         }
420         return $res;
421 } #}}}
422
423 sub mytargetpage ($$) { #{{{
424         my $page=shift;
425         my $ext=shift;
426
427         if (istranslation($page) || istranslatable($page)) {
428                 my ($masterpage, $lang) = (masterpage($page), lang($page));
429                 if (! $config{usedirs} || $masterpage eq 'index') {
430                         return $masterpage . "." . $lang . "." . $ext;
431                 }
432                 else {
433                         return $masterpage . "/index." . $lang . "." . $ext;
434                 }
435         }
436         return $origsubs{'targetpage'}->($page, $ext);
437 } #}}}
438
439 sub myurlto ($$;$) { #{{{
440         my $to=shift;
441         my $from=shift;
442         my $absolute=shift;
443
444         # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
445         if (! length $to
446             && $config{po_link_to} eq "current"
447             && istranslatable('index')) {
448                 return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
449         }
450         return $origsubs{'urlto'}->($to,$from,$absolute);
451 } #}}}
452
453
454 # ,----
455 # | Blackboxes for private data
456 # `----
457
458 {
459         my %filtered;
460
461         sub alreadyfiltered($$) { #{{{
462                 my $page=shift;
463                 my $destpage=shift;
464
465                 return ( exists $filtered{$page}{$destpage}
466                          && $filtered{$page}{$destpage} eq 1 );
467         } #}}}
468
469         sub setalreadyfiltered($$) { #{{{
470                 my $page=shift;
471                 my $destpage=shift;
472
473                 $filtered{$page}{$destpage}=1;
474         } #}}}
475
476         sub unsetalreadyfiltered($$) { #{{{
477                 my $page=shift;
478                 my $destpage=shift;
479
480                 if (exists $filtered{$page}{$destpage}) {
481                         delete $filtered{$page}{$destpage};
482                 }
483         } #}}}
484
485         sub resetalreadyfiltered() { #{{{
486                 undef %filtered;
487         } #}}}
488 }
489
490
491 # ,----
492 # | Helper functions
493 # `----
494
495 sub maybe_add_leading_slash ($;$) { #{{{
496         my $str=shift;
497         my $add=shift;
498         $add=1 unless defined $add;
499         return '/' . $str if $add;
500         return $str;
501 } #}}}
502
503 sub istranslatable ($) { #{{{
504         my $page=shift;
505
506         $page=~s#^/##;
507         my $file=$pagesources{$page};
508
509         return 0 unless defined $file;
510         return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
511         return 0 if $file =~ /\.pot$/;
512         return 1 if  pagespec_match($page, $config{po_translatable_pages});
513         return;
514 } #}}}
515
516 sub _istranslation ($) { #{{{
517         my $page=shift;
518
519         my $hasleadingslash = ($page=~s#^/##);
520         my $file=$pagesources{$page};
521         return 0 unless (defined $file
522                          && defined pagetype($file)
523                          && pagetype($file) eq 'po');
524         return 0 if $file =~ /\.pot$/;
525
526         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
527         return 0 unless (defined $masterpage && defined $lang
528                          && length $masterpage && length $lang
529                          && defined $pagesources{$masterpage}
530                          && defined $config{po_slave_languages}{$lang});
531
532         return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
533                 if istranslatable($masterpage);
534 } #}}}
535
536 sub istranslation ($) { #{{{
537         my $page=shift;
538
539         if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
540                 my $hasleadingslash = ($masterpage=~s#^/##);
541                 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
542                 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
543         }
544         return;
545 } #}}}
546
547 sub masterpage ($) { #{{{
548         my $page=shift;
549
550         if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
551                 return $masterpage;
552         }
553         return $page;
554 } #}}}
555
556 sub lang ($) { #{{{
557         my $page=shift;
558
559         if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
560                 return $lang;
561         }
562         return $config{po_master_language}{code};
563 } #}}}
564
565 sub islanguagecode ($) { #{{{
566         my $code=shift;
567
568         return ($code =~ /^[a-z]{2}$/);
569 } #}}}
570
571 sub otherlanguages($) { #{{{
572         my $page=shift;
573
574         my %ret;
575         if (istranslatable($page)) {
576                 %ret = %{$translations{$page}} if defined $translations{$page};
577         }
578         elsif (istranslation($page)) {
579                 my $masterpage = masterpage($page);
580                 $ret{$config{po_master_language}{code}} = $masterpage;
581                 foreach my $lang (sort keys %{$translations{$masterpage}}) {
582                         next if $lang eq lang($page);
583                         $ret{$lang} = $translations{$masterpage}{$lang};
584                 }
585         }
586         return \%ret;
587 } #}}}
588
589 sub potfile ($) { #{{{
590         my $masterfile=shift;
591
592         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
593         $dir='' if $dir eq './';
594         return File::Spec->catpath('', $dir, $name . ".pot");
595 } #}}}
596
597 sub pofile ($$) { #{{{
598         my $masterfile=shift;
599         my $lang=shift;
600
601         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
602         $dir='' if $dir eq './';
603         return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
604 } #}}}
605
606 sub pofiles ($) { #{{{
607         my $masterfile=shift;
608
609         return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
610 } #}}}
611
612 sub refreshpot ($) { #{{{
613         my $masterfile=shift;
614
615         my $potfile=potfile($masterfile);
616         my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
617         my $doc=Locale::Po4a::Chooser::new('text',%options);
618         $doc->{TT}{utf_mode} = 1;
619         $doc->{TT}{file_in_charset} = 'utf-8';
620         $doc->{TT}{file_out_charset} = 'utf-8';
621         $doc->read($masterfile);
622         # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
623         # this is undocument use of internal Locale::Po4a::TransTractor's data,
624         # compulsory since this module prevents us from using the porefs option.
625         my %po_options = ('porefs' => 'none');
626         $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
627         $doc->{TT}{po_out}->set_charset('utf-8');
628         # do the actual work
629         $doc->parse;
630         IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
631         $doc->writepo($potfile);
632 } #}}}
633
634 sub refreshpofiles ($@) { #{{{
635         my $masterfile=shift;
636         my @pofiles=@_;
637
638         my $potfile=potfile($masterfile);
639         error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
640
641         foreach my $pofile (@pofiles) {
642                 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
643                 if (-e $pofile) {
644                         system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
645                                 or error("[po/refreshpofiles:$pofile] failed to update");
646                 }
647                 else {
648                         File::Copy::syscopy($potfile,$pofile)
649                                 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
650                 }
651         }
652 } #}}}
653
654 sub buildtranslationscache() { #{{{
655         # use istranslation's side-effect
656         map istranslation($_), (keys %pagesources);
657 } #}}}
658
659 sub resettranslationscache() { #{{{
660         undef %translations;
661 } #}}}
662
663 sub flushmemoizecache() { #{{{
664         Memoize::flush_cache("istranslatable");
665         Memoize::flush_cache("_istranslation");
666         Memoize::flush_cache("percenttranslated");
667 } #}}}
668
669 sub urlto_with_orig_beautiful_urlpath($$) { #{{{
670         my $to=shift;
671         my $from=shift;
672
673         inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
674         my $res=urlto($to, $from);
675         inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
676
677         return $res;
678 } #}}}
679
680 sub percenttranslated ($) { #{{{
681         my $page=shift;
682
683         return gettext("N/A") unless istranslation($page);
684         my $file=srcfile($pagesources{$page});
685         my $masterfile = srcfile($pagesources{masterpage($page)});
686         my (@pos,@masters);
687         push @pos,$file;
688         push @masters,$masterfile;
689         my %options = (
690                 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
691         );
692         my $doc=Locale::Po4a::Chooser::new('text',%options);
693         $doc->process(
694                 'po_in_name'    => \@pos,
695                 'file_in_name'  => \@masters,
696                 'file_in_charset'  => 'utf-8',
697                 'file_out_charset' => 'utf-8',
698         ) or error("[po/percenttranslated:$page]: failed to translate");
699         my ($percent,$hit,$queries) = $doc->stats();
700         return $percent;
701 } #}}}
702
703 sub languagename ($) { #{{{
704         my $code=shift;
705
706         return $config{po_master_language}{name}
707                 if $code eq $config{po_master_language}{code};
708         return $config{po_slave_languages}{$code}
709                 if defined $config{po_slave_languages}{$code};
710         return;
711 } #}}}
712
713 sub otherlanguagesloop ($) { #{{{
714         my $page=shift;
715
716         my @ret;
717         my %otherpages=%{otherlanguages($page)};
718         while (my ($lang, $otherpage) = each %otherpages) {
719                 if (istranslation($page) && masterpage($page) eq $otherpage) {
720                         push @ret, {
721                                 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
722                                 code => $lang,
723                                 language => languagename($lang),
724                                 master => 1,
725                         };
726                 }
727                 else {
728                         push @ret, {
729                                 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
730                                 code => $lang,
731                                 language => languagename($lang),
732                                 percent => percenttranslated($otherpage),
733                         }
734                 }
735         }
736         return sort {
737                         return -1 if $a->{code} eq $config{po_master_language}{code};
738                         return 1 if $b->{code} eq $config{po_master_language}{code};
739                         return $a->{language} cmp $b->{language};
740                 } @ret;
741 } #}}}
742
743 sub homepageurl (;$) { #{{{
744         my $page=shift;
745
746         return urlto('', $page);
747 } #}}}
748
749 # ,----
750 # | PageSpec's
751 # `----
752
753 package IkiWiki::PageSpec;
754 use warnings;
755 use strict;
756 use IkiWiki 2.00;
757
758 sub match_istranslation ($;@) { #{{{
759         my $page=shift;
760
761         if (IkiWiki::Plugin::po::istranslation($page)) {
762                 return IkiWiki::SuccessReason->new("is a translation page");
763         }
764         else {
765                 return IkiWiki::FailReason->new("is not a translation page");
766         }
767 } #}}}
768
769 sub match_istranslatable ($;@) { #{{{
770         my $page=shift;
771
772         if (IkiWiki::Plugin::po::istranslatable($page)) {
773                 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
774         }
775         else {
776                 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
777         }
778 } #}}}
779
780 sub match_lang ($$;@) { #{{{
781         my $page=shift;
782         my $wanted=shift;
783
784         my $regexp=IkiWiki::glob2re($wanted);
785         my $lang=IkiWiki::Plugin::po::lang($page);
786         if ($lang!~/^$regexp$/i) {
787                 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
788         }
789         else {
790                 return IkiWiki::SuccessReason->new("file language is $wanted");
791         }
792 } #}}}
793
794 sub match_currentlang ($$;@) { #{{{
795         my $page=shift;
796         shift;
797         my %params=@_;
798
799         return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
800
801         my $currentlang=IkiWiki::Plugin::po::lang($params{location});
802         my $lang=IkiWiki::Plugin::po::lang($page);
803
804         if ($lang eq $currentlang) {
805                 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
806         }
807         else {
808                 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
809         }
810 } #}}}
811
812 1