summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: e7f36e968361c21e9679a80ad5d112258c8e3e1f (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 otherlanguages($) { #{{{
  132. my $page=shift;
  133. my %ret;
  134. if (istranslatable($page)) {
  135. %ret = %{$translations{$page}};
  136. }
  137. elsif (istranslation($page)) {
  138. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  139. $ret{$config{po_master_language}{code}} = $masterpage;
  140. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  141. next if $lang eq $curlang;
  142. $ret{$lang} = $translations{$masterpage}{$lang};
  143. }
  144. }
  145. return \%ret;
  146. } #}}}
  147. sub potfile ($) { #{{{
  148. my $masterfile=shift;
  149. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  150. $dir='' if $dir eq './';
  151. return File::Spec->catpath('', $dir, $name . ".pot");
  152. } #}}}
  153. sub pofile ($$) { #{{{
  154. my $masterfile=shift;
  155. my $lang=shift;
  156. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  157. $dir='' if $dir eq './';
  158. return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
  159. } #}}}
  160. sub pofiles ($) { #{{{
  161. my $masterfile=shift;
  162. return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
  163. } #}}}
  164. sub refreshpot ($) { #{{{
  165. my $masterfile=shift;
  166. my $potfile=potfile($masterfile);
  167. my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
  168. my $doc=Locale::Po4a::Chooser::new('text',%options);
  169. $doc->{TT}{utf_mode} = 1;
  170. $doc->{TT}{file_in_charset} = 'utf-8';
  171. $doc->{TT}{file_out_charset} = 'utf-8';
  172. $doc->read($masterfile);
  173. # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
  174. # this is undocument use of internal Locale::Po4a::TransTractor's data,
  175. # compulsory since this module prevents us from using the porefs option.
  176. my %po_options = ('porefs' => 'none');
  177. $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
  178. $doc->{TT}{po_out}->set_charset('utf-8');
  179. # do the actual work
  180. $doc->parse;
  181. IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
  182. $doc->writepo($potfile);
  183. } #}}}
  184. sub refreshpofiles ($@) { #{{{
  185. my $masterfile=shift;
  186. my @pofiles=@_;
  187. my $potfile=potfile($masterfile);
  188. error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
  189. foreach my $pofile (@pofiles) {
  190. IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
  191. if (-e $pofile) {
  192. system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
  193. or error("[po/refreshpofiles:$pofile] failed to update");
  194. }
  195. else {
  196. File::Copy::syscopy($potfile,$pofile)
  197. or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
  198. }
  199. }
  200. } #}}}
  201. sub buildtranslationscache() { #{{{
  202. # use istranslation's side-effect
  203. map istranslation($_), (keys %pagesources);
  204. } #}}}
  205. sub resettranslationscache() { #{{{
  206. undef %translations;
  207. } #}}}
  208. sub needsbuild () { #{{{
  209. my $needsbuild=shift;
  210. # backup @needsbuild content so that change() can know whether
  211. # a given master page was rendered because its source file was changed
  212. @origneedsbuild=(@$needsbuild);
  213. buildtranslationscache();
  214. # make existing translations depend on the corresponding master page
  215. foreach my $master (keys %translations) {
  216. map add_depends($_, $master), values %{otherlanguages($master)};
  217. }
  218. } #}}}
  219. sub scan (@) { #{{{
  220. my %params=@_;
  221. my $page=$params{page};
  222. my $content=$params{content};
  223. return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
  224. if (istranslation($page)) {
  225. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  226. foreach my $destpage (@{$links{$page}}) {
  227. if (istranslatable($destpage)) {
  228. # replace one occurence of $destpage in $links{$page}
  229. # (we only want to replace the one that was added by
  230. # IkiWiki::Plugin::link::scan, other occurences may be
  231. # there for other reasons)
  232. for (my $i=0; $i<@{$links{$page}}; $i++) {
  233. if (@{$links{$page}}[$i] eq $destpage) {
  234. @{$links{$page}}[$i] = $destpage . '.' . $curlang;
  235. last;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. elsif (! istranslatable($page) && ! istranslation($page)) {
  242. foreach my $destpage (@{$links{$page}}) {
  243. if (istranslatable($destpage)) {
  244. map {
  245. push @{$links{$page}}, $destpage . '.' . $_;
  246. } (keys %{$config{po_slave_languages}});
  247. }
  248. }
  249. }
  250. } #}}}
  251. sub mytargetpage ($$) { #{{{
  252. my $page=shift;
  253. my $ext=shift;
  254. if (istranslation($page)) {
  255. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  256. if (! $config{usedirs} || $masterpage eq 'index') {
  257. return $masterpage . "." . $lang . "." . $ext;
  258. }
  259. else {
  260. return $masterpage . "/index." . $lang . "." . $ext;
  261. }
  262. }
  263. elsif (istranslatable($page)) {
  264. if (! $config{usedirs} || $page eq 'index') {
  265. return $page . "." . $config{po_master_language}{code} . "." . $ext;
  266. }
  267. else {
  268. return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
  269. }
  270. }
  271. return $origsubs{'targetpage'}->($page, $ext);
  272. } #}}}
  273. sub mybeautify_urlpath ($) { #{{{
  274. my $url=shift;
  275. my $res=$origsubs{'beautify_urlpath'}->($url);
  276. if ($config{po_link_to} eq "negotiated") {
  277. $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
  278. }
  279. return $res;
  280. } #}}}
  281. sub urlto_with_orig_beautiful_urlpath($$) { #{{{
  282. my $to=shift;
  283. my $from=shift;
  284. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
  285. my $res=urlto($to, $from);
  286. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  287. return $res;
  288. } #}}}
  289. sub myurlto ($$;$) { #{{{
  290. my $to=shift;
  291. my $from=shift;
  292. my $absolute=shift;
  293. # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
  294. if (! length $to
  295. && $config{po_link_to} eq "current"
  296. && istranslation($from)
  297. && istranslatable('index')) {
  298. my ($masterpage, $curlang) = ($from =~ /(.*)[.]([a-z]{2})$/);
  299. return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . $curlang . ".$config{htmlext}");
  300. }
  301. return $origsubs{'urlto'}->($to,$from,$absolute);
  302. } #}}}
  303. sub mybestlink ($$) { #{{{
  304. my $page=shift;
  305. my $link=shift;
  306. my $res=$origsubs{'bestlink'}->($page, $link);
  307. if (length $res) {
  308. if ($config{po_link_to} eq "current"
  309. && istranslatable($res)
  310. && istranslation($page)) {
  311. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  312. return $res . "." . $curlang;
  313. }
  314. else {
  315. return $res;
  316. }
  317. }
  318. return "";
  319. } #}}}
  320. sub alreadyfiltered($$) { #{{{
  321. my $page=shift;
  322. my $destpage=shift;
  323. return ( exists $filtered{$page}{$destpage}
  324. && $filtered{$page}{$destpage} eq 1 );
  325. } #}}}
  326. sub setalreadyfiltered($$) { #{{{
  327. my $page=shift;
  328. my $destpage=shift;
  329. $filtered{$page}{$destpage}=1;
  330. } #}}}
  331. sub unsetalreadyfiltered($$) { #{{{
  332. my $page=shift;
  333. my $destpage=shift;
  334. if (exists $filtered{$page}{$destpage}) {
  335. delete $filtered{$page}{$destpage};
  336. }
  337. } #}}}
  338. sub resetalreadyfiltered() { #{{{
  339. undef %filtered;
  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 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  371. my $masterfile = srcfile($pagesources{$masterpage});
  372. my (@pos,@masters);
  373. push @pos,$infile;
  374. push @masters,$masterfile;
  375. my %options = (
  376. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  377. );
  378. my $doc=Locale::Po4a::Chooser::new('text',%options);
  379. $doc->process(
  380. 'po_in_name' => \@pos,
  381. 'file_in_name' => \@masters,
  382. 'file_in_charset' => 'utf-8',
  383. 'file_out_charset' => 'utf-8',
  384. ) or error("[po/filter:$page]: failed to translate");
  385. $doc->write($outfile) or error("[po/filter:$page] could not write $outfile");
  386. $content = readfile($outfile) or error("[po/filter:$page] could not read $outfile");
  387. # Unlinking should happen automatically, thanks to File::Temp,
  388. # but it does not work here, probably because of the way writefile()
  389. # and Locale::Po4a::write() work.
  390. unlink $infile, $outfile;
  391. setalreadyfiltered($page, $destpage);
  392. return $content;
  393. } #}}}
  394. sub htmlize (@) { #{{{
  395. my %params=@_;
  396. my $page = $params{page};
  397. my $content = $params{content};
  398. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  399. my $masterfile = srcfile($pagesources{$masterpage});
  400. # force content to be htmlize'd as if it was the same type as the master page
  401. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  402. } #}}}
  403. sub percenttranslated ($) { #{{{
  404. my $page=shift;
  405. return gettext("N/A") unless (istranslation($page));
  406. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  407. my $file=srcfile($pagesources{$page});
  408. my $masterfile = srcfile($pagesources{$masterpage});
  409. my (@pos,@masters);
  410. push @pos,$file;
  411. push @masters,$masterfile;
  412. my %options = (
  413. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  414. );
  415. my $doc=Locale::Po4a::Chooser::new('text',%options);
  416. $doc->process(
  417. 'po_in_name' => \@pos,
  418. 'file_in_name' => \@masters,
  419. 'file_in_charset' => 'utf-8',
  420. 'file_out_charset' => 'utf-8',
  421. ) or error("[po/percenttranslated:$page]: failed to translate");
  422. my ($percent,$hit,$queries) = $doc->stats();
  423. return $percent;
  424. } #}}}
  425. sub otherlanguagesloop ($) { #{{{
  426. my $page=shift;
  427. my @ret;
  428. if (istranslatable($page)) {
  429. foreach my $lang (sort keys %{$translations{$page}}) {
  430. my $translation = $translations{$page}{$lang};
  431. push @ret, {
  432. url => urlto($translation, $page),
  433. code => $lang,
  434. language => $config{po_slave_languages}{$lang},
  435. percent => percenttranslated($translation),
  436. };
  437. }
  438. }
  439. elsif (istranslation($page)) {
  440. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  441. push @ret, {
  442. url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
  443. code => $config{po_master_language}{code},
  444. language => $config{po_master_language}{name},
  445. master => 1,
  446. };
  447. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  448. push @ret, {
  449. url => urlto($translations{$masterpage}{$lang}, $page),
  450. code => $lang,
  451. language => $config{po_slave_languages}{$lang},
  452. percent => percenttranslated($translations{$masterpage}{$lang}),
  453. } unless ($lang eq $curlang);
  454. }
  455. }
  456. return @ret;
  457. } #}}}
  458. sub pagetemplate (@) { #{{{
  459. my %params=@_;
  460. my $page=$params{page};
  461. my $destpage=$params{destpage};
  462. my $template=$params{template};
  463. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
  464. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  465. $template->param(percenttranslated => percenttranslated($page));
  466. }
  467. if ($template->query(name => "istranslation")) {
  468. $template->param(istranslation => istranslation($page));
  469. }
  470. if ($template->query(name => "istranslatable")) {
  471. $template->param(istranslatable => istranslatable($page));
  472. }
  473. if ($template->query(name => "otherlanguages")) {
  474. $template->param(otherlanguages => [otherlanguagesloop($page)]);
  475. map add_depends($page, $_), (values %{otherlanguages($page)});
  476. }
  477. # Rely on IkiWiki::Render's genpage() to decide wether
  478. # a discussion link should appear on $page; this is not
  479. # totally accurate, though: some broken links may be generated
  480. # when cgiurl is disabled.
  481. # This compromise avoids some code duplication, and will probably
  482. # prevent future breakage when ikiwiki internals change.
  483. # Known limitations are preferred to future random bugs.
  484. if ($template->param('discussionlink') && istranslation($page)) {
  485. $template->param('discussionlink' => htmllink(
  486. $page,
  487. $destpage,
  488. $masterpage . '/' . gettext("Discussion"),
  489. noimageinline => 1,
  490. forcesubpage => 0,
  491. linktext => gettext("Discussion"),
  492. ));
  493. }
  494. # Remove broken parentlink to ./index.html on home page's translations.
  495. # It works because this hook has the "last" parameter set, to ensure it
  496. # runs after parentlinks' own pagetemplate hook.
  497. if ($template->param('parentlinks')
  498. && istranslation($page)
  499. && $masterpage eq "index") {
  500. $template->param('parentlinks' => []);
  501. }
  502. } # }}}
  503. sub change(@) { #{{{
  504. my @rendered=@_;
  505. my $updated_po_files=0;
  506. # Refresh/create POT and PO files as needed.
  507. foreach my $page (map pagename($_), @rendered) {
  508. next unless istranslatable($page);
  509. my $file=srcfile($pagesources{$page});
  510. my $updated_pot_file=0;
  511. # Only refresh Pot file if it does not exist, or if
  512. # $pagesources{$page} was changed: don't if only the HTML was
  513. # refreshed, e.g. because of a dependency.
  514. if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
  515. || ! -e potfile($file)) {
  516. refreshpot($file);
  517. $updated_pot_file=1;
  518. }
  519. my @pofiles;
  520. foreach my $lang (keys %{$config{po_slave_languages}}) {
  521. my $pofile=pofile($file, $lang);
  522. if ($updated_pot_file || ! -e $pofile) {
  523. push @pofiles, $pofile;
  524. }
  525. }
  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