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