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