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