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