summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: eabc96977b4e63ab04d1899df53b801bac3c4871 (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 otherlanguagesloop ($) { #{{{
  423. my $page=shift;
  424. my @ret;
  425. if (istranslatable($page)) {
  426. my %otherpages=%{otherlanguages($page)};
  427. while (my ($lang, $translation) = each %otherpages) {
  428. push @ret, {
  429. url => urlto($translation, $page),
  430. code => $lang,
  431. language => $config{po_slave_languages}{$lang},
  432. percent => percenttranslated($translation),
  433. };
  434. }
  435. }
  436. elsif (istranslation($page)) {
  437. my ($masterpage, $curlang) = (masterpage($page), lang($page));
  438. push @ret, {
  439. url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
  440. code => $config{po_master_language}{code},
  441. language => $config{po_master_language}{name},
  442. master => 1,
  443. };
  444. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  445. push @ret, {
  446. url => urlto($translations{$masterpage}{$lang}, $page),
  447. code => $lang,
  448. language => $config{po_slave_languages}{$lang},
  449. percent => percenttranslated($translations{$masterpage}{$lang}),
  450. } unless ($lang eq $curlang);
  451. }
  452. }
  453. return @ret;
  454. } #}}}
  455. sub pagetemplate (@) { #{{{
  456. my %params=@_;
  457. my $page=$params{page};
  458. my $destpage=$params{destpage};
  459. my $template=$params{template};
  460. my ($masterpage, $lang) = istranslation($page);
  461. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  462. $template->param(percenttranslated => percenttranslated($page));
  463. }
  464. if ($template->query(name => "istranslation")) {
  465. $template->param(istranslation => scalar istranslation($page));
  466. }
  467. if ($template->query(name => "istranslatable")) {
  468. $template->param(istranslatable => istranslatable($page));
  469. }
  470. if ($template->query(name => "otherlanguages")) {
  471. $template->param(otherlanguages => [otherlanguagesloop($page)]);
  472. map add_depends($page, $_), (values %{otherlanguages($page)});
  473. }
  474. # Rely on IkiWiki::Render's genpage() to decide wether
  475. # a discussion link should appear on $page; this is not
  476. # totally accurate, though: some broken links may be generated
  477. # when cgiurl is disabled.
  478. # This compromise avoids some code duplication, and will probably
  479. # prevent future breakage when ikiwiki internals change.
  480. # Known limitations are preferred to future random bugs.
  481. if ($template->param('discussionlink') && istranslation($page)) {
  482. $template->param('discussionlink' => htmllink(
  483. $page,
  484. $destpage,
  485. $masterpage . '/' . gettext("Discussion"),
  486. noimageinline => 1,
  487. forcesubpage => 0,
  488. linktext => gettext("Discussion"),
  489. ));
  490. }
  491. # Remove broken parentlink to ./index.html on home page's translations.
  492. # It works because this hook has the "last" parameter set, to ensure it
  493. # runs after parentlinks' own pagetemplate hook.
  494. if ($template->param('parentlinks')
  495. && istranslation($page)
  496. && $masterpage eq "index") {
  497. $template->param('parentlinks' => []);
  498. }
  499. } # }}}
  500. sub change(@) { #{{{
  501. my @rendered=@_;
  502. my $updated_po_files=0;
  503. # Refresh/create POT and PO files as needed.
  504. foreach my $page (map pagename($_), @rendered) {
  505. next unless istranslatable($page);
  506. my $file=srcfile($pagesources{$page});
  507. my $updated_pot_file=0;
  508. # Only refresh Pot file if it does not exist, or if
  509. # $pagesources{$page} was changed: don't if only the HTML was
  510. # refreshed, e.g. because of a dependency.
  511. if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
  512. || ! -e potfile($file)) {
  513. refreshpot($file);
  514. $updated_pot_file=1;
  515. }
  516. my @pofiles;
  517. map {
  518. push @pofiles, $_ if ($updated_pot_file || ! -e $_);
  519. } (pofiles($file));
  520. if (@pofiles) {
  521. refreshpofiles($file, @pofiles);
  522. map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
  523. $updated_po_files=1;
  524. }
  525. }
  526. if ($updated_po_files) {
  527. # Check staged changes in.
  528. if ($config{rcs}) {
  529. IkiWiki::disable_commit_hook();
  530. IkiWiki::rcs_commit_staged(gettext("updated PO files"),
  531. "IkiWiki::Plugin::po::change", "127.0.0.1");
  532. IkiWiki::enable_commit_hook();
  533. IkiWiki::rcs_update();
  534. }
  535. # Reinitialize module's private variables.
  536. resetalreadyfiltered();
  537. resettranslationscache();
  538. # Trigger a wiki refresh.
  539. require IkiWiki::Render;
  540. IkiWiki::refresh();
  541. IkiWiki::saveindex();
  542. }
  543. } #}}}
  544. sub editcontent () { #{{{
  545. my %params=@_;
  546. # as we're previewing or saving a page, the content may have
  547. # changed, so tell the next filter() invocation it must not be lazy
  548. unsetalreadyfiltered($params{page}, $params{page});
  549. return $params{content};
  550. } #}}}
  551. sub istranslatable ($) { #{{{
  552. my $page=shift;
  553. my $file=$pagesources{$page};
  554. if (! defined $file
  555. || (defined pagetype($file) && pagetype($file) eq 'po')
  556. || $file =~ /\.pot$/) {
  557. return 0;
  558. }
  559. return pagespec_match($page, $config{po_translatable_pages});
  560. } #}}}
  561. sub _istranslation ($) { #{{{
  562. my $page=shift;
  563. my $file=$pagesources{$page};
  564. if (! defined $file) {
  565. return IkiWiki::FailReason->new("no file specified");
  566. }
  567. if (! defined $file
  568. || ! defined pagetype($file)
  569. || ! pagetype($file) eq 'po'
  570. || $file =~ /\.pot$/) {
  571. return 0;
  572. }
  573. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  574. if (! defined $masterpage || ! defined $lang
  575. || ! (length($masterpage) > 0) || ! (length($lang) > 0)
  576. || ! defined $pagesources{$masterpage}
  577. || ! defined $config{po_slave_languages}{$lang}) {
  578. return 0;
  579. }
  580. return ($masterpage, $lang) if istranslatable($masterpage);
  581. } #}}}
  582. sub istranslation ($) { #{{{
  583. my $page=shift;
  584. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  585. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  586. return ($masterpage, $lang);
  587. }
  588. return;
  589. } #}}}
  590. sub masterpage ($) { #{{{
  591. my $page=shift;
  592. if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
  593. return $masterpage;
  594. }
  595. return $page;
  596. } #}}}
  597. sub lang ($) { #{{{
  598. my $page=shift;
  599. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  600. return $lang;
  601. }
  602. return $config{po_master_language}{code};
  603. } #}}}
  604. package IkiWiki::PageSpec;
  605. use warnings;
  606. use strict;
  607. use IkiWiki 2.00;
  608. sub match_istranslation ($;@) { #{{{
  609. my $page=shift;
  610. if (IkiWiki::Plugin::po::istranslation($page)) {
  611. return IkiWiki::SuccessReason->new("is a translation page");
  612. }
  613. else {
  614. return IkiWiki::FailReason->new("is not a translation page");
  615. }
  616. } #}}}
  617. sub match_istranslatable ($;@) { #{{{
  618. my $page=shift;
  619. if (IkiWiki::Plugin::po::istranslatable($page)) {
  620. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  621. }
  622. else {
  623. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  624. }
  625. } #}}}
  626. sub match_lang ($$;@) { #{{{
  627. my $page=shift;
  628. my $wanted=shift;
  629. my $regexp=IkiWiki::glob2re($wanted);
  630. my $lang;
  631. my $masterpage;
  632. if (IkiWiki::Plugin::po::istranslation($page)) {
  633. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  634. }
  635. else {
  636. $lang = $config{po_master_language}{code};
  637. }
  638. if ($lang!~/^$regexp$/i) {
  639. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  640. }
  641. else {
  642. return IkiWiki::SuccessReason->new("file language is $wanted");
  643. }
  644. } #}}}
  645. sub match_currentlang ($$;@) { #{{{
  646. my $page=shift;
  647. shift;
  648. my %params=@_;
  649. my ($currentmasterpage, $currentlang, $masterpage, $lang);
  650. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  651. if (IkiWiki::Plugin::po::istranslation($params{location})) {
  652. ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
  653. }
  654. else {
  655. $currentlang = $config{po_master_language}{code};
  656. }
  657. if (IkiWiki::Plugin::po::istranslation($page)) {
  658. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  659. }
  660. else {
  661. $lang = $config{po_master_language}{code};
  662. }
  663. if ($lang eq $currentlang) {
  664. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  665. }
  666. else {
  667. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  668. }
  669. } #}}}
  670. 1