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