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