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