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