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