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