summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 7902fa7455e42f917597b387ab65aee2f0a75f7e (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. if (! length $to
  236. && $config{po_link_to} eq "current"
  237. && istranslation($from)
  238. && istranslatable('index')) {
  239. my ($masterpage, $curlang) = ($from =~ /(.*)[.]([a-z]{2})$/);
  240. return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . $curlang . ".$config{htmlext}");
  241. }
  242. return $origsubs{'urlto'}->($to,$from,$absolute);
  243. } #}}}
  244. sub mybestlink ($$) { #{{{
  245. my $page=shift;
  246. my $link=shift;
  247. my $res=$origsubs{'bestlink'}->($page, $link);
  248. if (length $res) {
  249. if ($config{po_link_to} eq "current"
  250. && istranslatable($res)
  251. && istranslation($page)) {
  252. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  253. return $res . "." . $curlang;
  254. }
  255. else {
  256. return $res;
  257. }
  258. }
  259. return "";
  260. } #}}}
  261. # We use filter to convert PO to the master page's format,
  262. # since the rest of ikiwiki should not work on PO files.
  263. sub filter (@) { #{{{
  264. my %params = @_;
  265. my $page = $params{page};
  266. my $destpage = $params{destpage};
  267. my $content = decode_utf8(encode_utf8($params{content}));
  268. return $content if ( ! istranslation($page)
  269. || ( exists $filtered{$page}{$destpage}
  270. && $filtered{$page}{$destpage} eq 1 ));
  271. # CRLF line terminators make poor Locale::Po4a feel bad
  272. $content=~s/\r\n/\n/g;
  273. # Implementation notes
  274. #
  275. # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
  276. # to learn how to disguise a variable as a file.
  277. # 2. There are incompatibilities between some File::Temp versions
  278. # (including 0.18, bundled with Lenny's perl-modules package)
  279. # and others (e.g. 0.20, previously present in the archive as
  280. # a standalone package): under certain circumstances, some
  281. # return a relative filename, whereas others return an absolute one;
  282. # we here use this module in a way that is at least compatible
  283. # with 0.18 and 0.20. Beware, hit'n'run refactorers!
  284. my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
  285. DIR => File::Spec->tmpdir,
  286. UNLINK => 1)->filename;
  287. my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
  288. DIR => File::Spec->tmpdir,
  289. UNLINK => 1)->filename;
  290. writefile(basename($infile), File::Spec->tmpdir, $content);
  291. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  292. my $masterfile = srcfile($pagesources{$masterpage});
  293. my (@pos,@masters);
  294. push @pos,$infile;
  295. push @masters,$masterfile;
  296. my %options = (
  297. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  298. );
  299. my $doc=Locale::Po4a::Chooser::new('text',%options);
  300. $doc->process(
  301. 'po_in_name' => \@pos,
  302. 'file_in_name' => \@masters,
  303. 'file_in_charset' => 'utf-8',
  304. 'file_out_charset' => 'utf-8',
  305. ) or error("[po/filter:$infile]: failed to translate");
  306. $doc->write($outfile) or error("[po/filter:$infile] could not write $outfile");
  307. $content = readfile($outfile) or error("[po/filter:$infile] could not read $outfile");
  308. # Unlinking should happen automatically, thanks to File::Temp,
  309. # but it does not work here, probably because of the way writefile()
  310. # and Locale::Po4a::write() work.
  311. unlink $infile, $outfile;
  312. $filtered{$page}{$destpage}=1;
  313. return $content;
  314. } #}}}
  315. sub htmlize (@) { #{{{
  316. my %params=@_;
  317. my $page = $params{page};
  318. my $content = $params{content};
  319. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  320. my $masterfile = srcfile($pagesources{$masterpage});
  321. # force content to be htmlize'd as if it was the same type as the master page
  322. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  323. } #}}}
  324. sub percenttranslated ($) { #{{{
  325. my $page=shift;
  326. return gettext("N/A") unless (istranslation($page));
  327. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  328. my $file=srcfile($pagesources{$page});
  329. my $masterfile = srcfile($pagesources{$masterpage});
  330. my (@pos,@masters);
  331. push @pos,$file;
  332. push @masters,$masterfile;
  333. my %options = (
  334. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  335. );
  336. my $doc=Locale::Po4a::Chooser::new('text',%options);
  337. $doc->process(
  338. 'po_in_name' => \@pos,
  339. 'file_in_name' => \@masters,
  340. 'file_in_charset' => 'utf-8',
  341. 'file_out_charset' => 'utf-8',
  342. ) or error("[po/percenttranslated:$file]: failed to translate");
  343. my ($percent,$hit,$queries) = $doc->stats();
  344. return $percent;
  345. } #}}}
  346. sub otherlanguages ($) { #{{{
  347. my $page=shift;
  348. my @ret;
  349. if (istranslatable($page)) {
  350. foreach my $lang (sort keys %{$translations{$page}}) {
  351. my $translation = $translations{$page}{$lang};
  352. push @ret, {
  353. url => urlto($translation, $page),
  354. code => $lang,
  355. language => $config{po_slave_languages}{$lang},
  356. percent => percenttranslated($translation),
  357. };
  358. }
  359. }
  360. elsif (istranslation($page)) {
  361. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  362. push @ret, {
  363. url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
  364. code => $config{po_master_language}{code},
  365. language => $config{po_master_language}{name},
  366. master => 1,
  367. };
  368. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  369. push @ret, {
  370. url => urlto($translations{$masterpage}{$lang}, $page),
  371. code => $lang,
  372. language => $config{po_slave_languages}{$lang},
  373. percent => percenttranslated($translations{$masterpage}{$lang}),
  374. } unless ($lang eq $curlang);
  375. }
  376. }
  377. return @ret;
  378. } #}}}
  379. sub pagetemplate (@) { #{{{
  380. my %params=@_;
  381. my $page=$params{page};
  382. my $destpage=$params{destpage};
  383. my $template=$params{template};
  384. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
  385. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  386. $template->param(percenttranslated => percenttranslated($page));
  387. }
  388. if ($template->query(name => "istranslation")) {
  389. $template->param(istranslation => istranslation($page));
  390. }
  391. if ($template->query(name => "istranslatable")) {
  392. $template->param(istranslatable => istranslatable($page));
  393. }
  394. if ($template->query(name => "otherlanguages")) {
  395. $template->param(otherlanguages => [otherlanguages($page)]);
  396. if (istranslatable($page)) {
  397. foreach my $translation (values %{$translations{$page}}) {
  398. add_depends($page, $translation);
  399. }
  400. }
  401. elsif (istranslation($page)) {
  402. add_depends($page, $masterpage);
  403. foreach my $translation (values %{$translations{$masterpage}}) {
  404. add_depends($page, $translation);
  405. }
  406. }
  407. }
  408. # Rely on IkiWiki::Render's genpage() to decide wether
  409. # a discussion link should appear on $page; this is not
  410. # totally accurate, though: some broken links may be generated
  411. # when cgiurl is disabled.
  412. # This compromise avoids some code duplication, and will probably
  413. # prevent future breakage when ikiwiki internals change.
  414. # Known limitations are preferred to future random bugs.
  415. if ($template->param('discussionlink') && istranslation($page)) {
  416. $template->param('discussionlink' => htmllink(
  417. $page,
  418. $destpage,
  419. $masterpage . '/' . gettext("Discussion"),
  420. noimageinline => 1,
  421. forcesubpage => 0,
  422. linktext => gettext("Discussion"),
  423. ));
  424. }
  425. # remove broken parentlink to ./index.html on home page's translations
  426. if ($template->param('parentlinks')
  427. && istranslation($page)
  428. && $masterpage eq "index") {
  429. $template->param('parentlinks' => []);
  430. }
  431. } # }}}
  432. sub change(@) { #{{{
  433. my @rendered=@_;
  434. my $updated_po_files=0;
  435. # Refresh/create POT and PO files as needed.
  436. foreach my $page (map pagename($_), @rendered) {
  437. next unless istranslatable($page);
  438. my $file=srcfile($pagesources{$page});
  439. my $updated_pot_file=0;
  440. if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
  441. || ! -e potfile($file)) {
  442. refreshpot($file);
  443. $updated_pot_file=1;
  444. }
  445. my @pofiles;
  446. foreach my $lang (keys %{$config{po_slave_languages}}) {
  447. my $pofile=pofile($file, $lang);
  448. if ($updated_pot_file || ! -e $pofile) {
  449. push @pofiles, $pofile;
  450. }
  451. }
  452. if (@pofiles) {
  453. refreshpofiles($file, @pofiles);
  454. map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
  455. $updated_po_files=1;
  456. }
  457. }
  458. if ($updated_po_files) {
  459. # Check staged changes in.
  460. if ($config{rcs}) {
  461. IkiWiki::disable_commit_hook();
  462. IkiWiki::rcs_commit_staged(gettext("updated PO files"),
  463. "IkiWiki::Plugin::po::change", "127.0.0.1");
  464. IkiWiki::enable_commit_hook();
  465. IkiWiki::rcs_update();
  466. }
  467. # Reinitialize module's private variables.
  468. undef %filtered;
  469. undef %translations;
  470. # Trigger a wiki refresh.
  471. require IkiWiki::Render;
  472. IkiWiki::refresh();
  473. IkiWiki::saveindex();
  474. }
  475. } #}}}
  476. sub editcontent () { #{{{
  477. my %params=@_;
  478. # as we're previewing or saving a page, the content may have
  479. # changed, so tell the next filter() invocation it must not be lazy
  480. if (exists $filtered{$params{page}}{$params{page}}) {
  481. delete $filtered{$params{page}}{$params{page}};
  482. }
  483. return $params{content};
  484. } #}}}
  485. sub istranslatable ($) { #{{{
  486. my $page=shift;
  487. my $file=$pagesources{$page};
  488. if (! defined $file
  489. || (defined pagetype($file) && pagetype($file) eq 'po')
  490. || $file =~ /\.pot$/) {
  491. return 0;
  492. }
  493. return pagespec_match($page, $config{po_translatable_pages});
  494. } #}}}
  495. sub _istranslation ($) { #{{{
  496. my $page=shift;
  497. my $file=$pagesources{$page};
  498. if (! defined $file) {
  499. return IkiWiki::FailReason->new("no file specified");
  500. }
  501. if (! defined $file
  502. || ! defined pagetype($file)
  503. || ! pagetype($file) eq 'po'
  504. || $file =~ /\.pot$/) {
  505. return 0;
  506. }
  507. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  508. if (! defined $masterpage || ! defined $lang
  509. || ! (length($masterpage) > 0) || ! (length($lang) > 0)
  510. || ! defined $pagesources{$masterpage}
  511. || ! defined $config{po_slave_languages}{$lang}) {
  512. return 0;
  513. }
  514. return istranslatable($masterpage);
  515. } #}}}
  516. sub istranslation ($) { #{{{
  517. my $page=shift;
  518. if (_istranslation($page)) {
  519. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  520. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  521. return 1;
  522. }
  523. return 0;
  524. } #}}}
  525. package IkiWiki::PageSpec;
  526. use warnings;
  527. use strict;
  528. use IkiWiki 2.00;
  529. sub match_istranslation ($;@) { #{{{
  530. my $page=shift;
  531. if (IkiWiki::Plugin::po::istranslation($page)) {
  532. return IkiWiki::SuccessReason->new("is a translation page");
  533. }
  534. else {
  535. return IkiWiki::FailReason->new("is not a translation page");
  536. }
  537. } #}}}
  538. sub match_istranslatable ($;@) { #{{{
  539. my $page=shift;
  540. if (IkiWiki::Plugin::po::istranslatable($page)) {
  541. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  542. }
  543. else {
  544. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  545. }
  546. } #}}}
  547. sub match_lang ($$;@) { #{{{
  548. my $page=shift;
  549. my $wanted=shift;
  550. my $regexp=IkiWiki::glob2re($wanted);
  551. my $lang;
  552. my $masterpage;
  553. if (IkiWiki::Plugin::po::istranslation($page)) {
  554. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  555. }
  556. else {
  557. $lang = $config{po_master_language}{code};
  558. }
  559. if ($lang!~/^$regexp$/i) {
  560. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  561. }
  562. else {
  563. return IkiWiki::SuccessReason->new("file language is $wanted");
  564. }
  565. } #}}}
  566. sub match_currentlang ($$;@) { #{{{
  567. my $page=shift;
  568. shift;
  569. my %params=@_;
  570. my ($currentmasterpage, $currentlang, $masterpage, $lang);
  571. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  572. if (IkiWiki::Plugin::po::istranslation($params{location})) {
  573. ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
  574. }
  575. else {
  576. $currentlang = $config{po_master_language}{code};
  577. }
  578. if (IkiWiki::Plugin::po::istranslation($page)) {
  579. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  580. }
  581. else {
  582. $lang = $config{po_master_language}{code};
  583. }
  584. if ($lang eq $currentlang) {
  585. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  586. }
  587. else {
  588. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  589. }
  590. } #}}}
  591. 1