summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 96ba467c58d63f4c2ec07a78d281c8d60759d3ef (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 => "HOMEPAGEURL")) {
  255. $template->param(homepageurl => homepageurl($page));
  256. }
  257. if ($template->query(name => "otherlanguages")) {
  258. $template->param(otherlanguages => [otherlanguagesloop($page)]);
  259. map add_depends($page, $_), (values %{otherlanguages($page)});
  260. }
  261. # Rely on IkiWiki::Render's genpage() to decide wether
  262. # a discussion link should appear on $page; this is not
  263. # totally accurate, though: some broken links may be generated
  264. # when cgiurl is disabled.
  265. # This compromise avoids some code duplication, and will probably
  266. # prevent future breakage when ikiwiki internals change.
  267. # Known limitations are preferred to future random bugs.
  268. if ($template->param('discussionlink') && istranslation($page)) {
  269. $template->param('discussionlink' => htmllink(
  270. $page,
  271. $destpage,
  272. $masterpage . '/' . gettext("Discussion"),
  273. noimageinline => 1,
  274. forcesubpage => 0,
  275. linktext => gettext("Discussion"),
  276. ));
  277. }
  278. # Remove broken parentlink to ./index.html on home page's translations.
  279. # It works because this hook has the "last" parameter set, to ensure it
  280. # runs after parentlinks' own pagetemplate hook.
  281. if ($template->param('parentlinks')
  282. && istranslation($page)
  283. && $masterpage eq "index") {
  284. $template->param('parentlinks' => []);
  285. }
  286. } # }}}
  287. sub change(@) { #{{{
  288. my @rendered=@_;
  289. my $updated_po_files=0;
  290. # Refresh/create POT and PO files as needed.
  291. foreach my $page (map pagename($_), @rendered) {
  292. next unless istranslatable($page);
  293. my $file=srcfile($pagesources{$page});
  294. my $updated_pot_file=0;
  295. # Only refresh Pot file if it does not exist, or if
  296. # $pagesources{$page} was changed: don't if only the HTML was
  297. # refreshed, e.g. because of a dependency.
  298. if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
  299. || ! -e potfile($file)) {
  300. refreshpot($file);
  301. $updated_pot_file=1;
  302. }
  303. my @pofiles;
  304. map {
  305. push @pofiles, $_ if ($updated_pot_file || ! -e $_);
  306. } (pofiles($file));
  307. if (@pofiles) {
  308. refreshpofiles($file, @pofiles);
  309. map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
  310. $updated_po_files=1;
  311. }
  312. }
  313. if ($updated_po_files) {
  314. # Check staged changes in.
  315. if ($config{rcs}) {
  316. IkiWiki::disable_commit_hook();
  317. IkiWiki::rcs_commit_staged(gettext("updated PO files"),
  318. "IkiWiki::Plugin::po::change", "127.0.0.1");
  319. IkiWiki::enable_commit_hook();
  320. IkiWiki::rcs_update();
  321. }
  322. # Reinitialize module's private variables.
  323. resetalreadyfiltered();
  324. resettranslationscache();
  325. # Trigger a wiki refresh.
  326. require IkiWiki::Render;
  327. # without preliminary saveindex/loadindex, refresh()
  328. # complains about a lot of uninitialized variables
  329. IkiWiki::saveindex();
  330. IkiWiki::loadindex();
  331. IkiWiki::refresh();
  332. IkiWiki::saveindex();
  333. }
  334. } #}}}
  335. # As we're previewing or saving a page, the content may have
  336. # changed, so tell the next filter() invocation it must not be lazy.
  337. sub editcontent () { #{{{
  338. my %params=@_;
  339. unsetalreadyfiltered($params{page}, $params{page});
  340. return $params{content};
  341. } #}}}
  342. # ,----
  343. # | Injected functions
  344. # `----
  345. # Implement po_link_to=current
  346. sub mybestlink ($$) { #{{{
  347. my $page=shift;
  348. my $link=shift;
  349. my $res=$origsubs{'bestlink'}->($page, $link);
  350. if (length $res
  351. && $config{po_link_to} eq "current"
  352. && istranslatable($res)
  353. && istranslation($page)) {
  354. return $res . "." . lang($page);
  355. }
  356. return $res;
  357. } #}}}
  358. sub mybeautify_urlpath ($) { #{{{
  359. my $url=shift;
  360. my $res=$origsubs{'beautify_urlpath'}->($url);
  361. if ($config{po_link_to} eq "negotiated") {
  362. $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
  363. $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
  364. }
  365. return $res;
  366. } #}}}
  367. sub mytargetpage ($$) { #{{{
  368. my $page=shift;
  369. my $ext=shift;
  370. if (istranslation($page) || istranslatable($page)) {
  371. my ($masterpage, $lang) = (masterpage($page), lang($page));
  372. if (! $config{usedirs} || $masterpage eq 'index') {
  373. return $masterpage . "." . $lang . "." . $ext;
  374. }
  375. else {
  376. return $masterpage . "/index." . $lang . "." . $ext;
  377. }
  378. }
  379. return $origsubs{'targetpage'}->($page, $ext);
  380. } #}}}
  381. sub myurlto ($$;$) { #{{{
  382. my $to=shift;
  383. my $from=shift;
  384. my $absolute=shift;
  385. # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
  386. if (! length $to
  387. && $config{po_link_to} eq "current"
  388. && istranslatable('index')) {
  389. return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
  390. }
  391. return $origsubs{'urlto'}->($to,$from,$absolute);
  392. } #}}}
  393. # ,----
  394. # | Blackboxes for private data
  395. # `----
  396. {
  397. my %filtered;
  398. sub alreadyfiltered($$) { #{{{
  399. my $page=shift;
  400. my $destpage=shift;
  401. return ( exists $filtered{$page}{$destpage}
  402. && $filtered{$page}{$destpage} eq 1 );
  403. } #}}}
  404. sub setalreadyfiltered($$) { #{{{
  405. my $page=shift;
  406. my $destpage=shift;
  407. $filtered{$page}{$destpage}=1;
  408. } #}}}
  409. sub unsetalreadyfiltered($$) { #{{{
  410. my $page=shift;
  411. my $destpage=shift;
  412. if (exists $filtered{$page}{$destpage}) {
  413. delete $filtered{$page}{$destpage};
  414. }
  415. } #}}}
  416. sub resetalreadyfiltered() { #{{{
  417. undef %filtered;
  418. } #}}}
  419. }
  420. # ,----
  421. # | Helper functions
  422. # `----
  423. sub istranslatable ($) { #{{{
  424. my $page=shift;
  425. my $file=$pagesources{$page};
  426. return 0 unless defined $file;
  427. return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
  428. return 0 if $file =~ /\.pot$/;
  429. return pagespec_match($page, $config{po_translatable_pages});
  430. } #}}}
  431. sub _istranslation ($) { #{{{
  432. my $page=shift;
  433. my $file=$pagesources{$page};
  434. return 0 unless (defined $file
  435. && defined pagetype($file)
  436. && pagetype($file) eq 'po');
  437. return 0 if $file =~ /\.pot$/;
  438. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  439. return 0 unless (defined $masterpage && defined $lang
  440. && length $masterpage && length $lang
  441. && defined $pagesources{$masterpage}
  442. && defined $config{po_slave_languages}{$lang});
  443. return ($masterpage, $lang) if istranslatable($masterpage);
  444. } #}}}
  445. sub istranslation ($) { #{{{
  446. my $page=shift;
  447. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  448. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  449. return ($masterpage, $lang);
  450. }
  451. return;
  452. } #}}}
  453. sub masterpage ($) { #{{{
  454. my $page=shift;
  455. if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
  456. return $masterpage;
  457. }
  458. return $page;
  459. } #}}}
  460. sub lang ($) { #{{{
  461. my $page=shift;
  462. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  463. return $lang;
  464. }
  465. return $config{po_master_language}{code};
  466. } #}}}
  467. sub islanguagecode ($) { #{{{
  468. my $code=shift;
  469. return ($code =~ /^[a-z]{2}$/);
  470. } #}}}
  471. sub otherlanguages($) { #{{{
  472. my $page=shift;
  473. my %ret;
  474. if (istranslatable($page)) {
  475. %ret = %{$translations{$page}};
  476. }
  477. elsif (istranslation($page)) {
  478. my $masterpage = masterpage($page);
  479. $ret{$config{po_master_language}{code}} = $masterpage;
  480. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  481. next if $lang eq lang($page);
  482. $ret{$lang} = $translations{$masterpage}{$lang};
  483. }
  484. }
  485. return \%ret;
  486. } #}}}
  487. sub potfile ($) { #{{{
  488. my $masterfile=shift;
  489. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  490. $dir='' if $dir eq './';
  491. return File::Spec->catpath('', $dir, $name . ".pot");
  492. } #}}}
  493. sub pofile ($$) { #{{{
  494. my $masterfile=shift;
  495. my $lang=shift;
  496. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  497. $dir='' if $dir eq './';
  498. return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
  499. } #}}}
  500. sub pofiles ($) { #{{{
  501. my $masterfile=shift;
  502. return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
  503. } #}}}
  504. sub refreshpot ($) { #{{{
  505. my $masterfile=shift;
  506. my $potfile=potfile($masterfile);
  507. my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
  508. my $doc=Locale::Po4a::Chooser::new('text',%options);
  509. $doc->{TT}{utf_mode} = 1;
  510. $doc->{TT}{file_in_charset} = 'utf-8';
  511. $doc->{TT}{file_out_charset} = 'utf-8';
  512. $doc->read($masterfile);
  513. # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
  514. # this is undocument use of internal Locale::Po4a::TransTractor's data,
  515. # compulsory since this module prevents us from using the porefs option.
  516. my %po_options = ('porefs' => 'none');
  517. $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
  518. $doc->{TT}{po_out}->set_charset('utf-8');
  519. # do the actual work
  520. $doc->parse;
  521. IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
  522. $doc->writepo($potfile);
  523. } #}}}
  524. sub refreshpofiles ($@) { #{{{
  525. my $masterfile=shift;
  526. my @pofiles=@_;
  527. my $potfile=potfile($masterfile);
  528. error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
  529. foreach my $pofile (@pofiles) {
  530. IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
  531. if (-e $pofile) {
  532. system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
  533. or error("[po/refreshpofiles:$pofile] failed to update");
  534. }
  535. else {
  536. File::Copy::syscopy($potfile,$pofile)
  537. or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
  538. }
  539. }
  540. } #}}}
  541. sub buildtranslationscache() { #{{{
  542. # use istranslation's side-effect
  543. map istranslation($_), (keys %pagesources);
  544. } #}}}
  545. sub resettranslationscache() { #{{{
  546. undef %translations;
  547. } #}}}
  548. sub urlto_with_orig_beautiful_urlpath($$) { #{{{
  549. my $to=shift;
  550. my $from=shift;
  551. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
  552. my $res=urlto($to, $from);
  553. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  554. return $res;
  555. } #}}}
  556. sub percenttranslated ($) { #{{{
  557. my $page=shift;
  558. return gettext("N/A") unless istranslation($page);
  559. my $file=srcfile($pagesources{$page});
  560. my $masterfile = srcfile($pagesources{masterpage($page)});
  561. my (@pos,@masters);
  562. push @pos,$file;
  563. push @masters,$masterfile;
  564. my %options = (
  565. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  566. );
  567. my $doc=Locale::Po4a::Chooser::new('text',%options);
  568. $doc->process(
  569. 'po_in_name' => \@pos,
  570. 'file_in_name' => \@masters,
  571. 'file_in_charset' => 'utf-8',
  572. 'file_out_charset' => 'utf-8',
  573. ) or error("[po/percenttranslated:$page]: failed to translate");
  574. my ($percent,$hit,$queries) = $doc->stats();
  575. return $percent;
  576. } #}}}
  577. sub languagename ($) { #{{{
  578. my $code=shift;
  579. return $config{po_master_language}{name}
  580. if $code eq $config{po_master_language}{code};
  581. return $config{po_slave_languages}{$code}
  582. if defined $config{po_slave_languages}{$code};
  583. return;
  584. } #}}}
  585. sub otherlanguagesloop ($) { #{{{
  586. my $page=shift;
  587. my @ret;
  588. my %otherpages=%{otherlanguages($page)};
  589. while (my ($lang, $otherpage) = each %otherpages) {
  590. if (istranslation($page) && masterpage($page) eq $otherpage) {
  591. push @ret, {
  592. url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
  593. code => $lang,
  594. language => languagename($lang),
  595. master => 1,
  596. };
  597. }
  598. else {
  599. push @ret, {
  600. url => urlto($otherpage, $page),
  601. code => $lang,
  602. language => languagename($lang),
  603. percent => percenttranslated($otherpage),
  604. }
  605. }
  606. }
  607. return sort {
  608. return -1 if $a->{code} eq $config{po_master_language}{code};
  609. return 1 if $b->{code} eq $config{po_master_language}{code};
  610. return $a->{language} cmp $b->{language};
  611. } @ret;
  612. } #}}}
  613. sub homepageurl (;$) { #{{{
  614. my $page=shift;
  615. return urlto('', $page);
  616. } #}}}
  617. # ,----
  618. # | PageSpec's
  619. # `----
  620. package IkiWiki::PageSpec;
  621. use warnings;
  622. use strict;
  623. use IkiWiki 2.00;
  624. sub match_istranslation ($;@) { #{{{
  625. my $page=shift;
  626. if (IkiWiki::Plugin::po::istranslation($page)) {
  627. return IkiWiki::SuccessReason->new("is a translation page");
  628. }
  629. else {
  630. return IkiWiki::FailReason->new("is not a translation page");
  631. }
  632. } #}}}
  633. sub match_istranslatable ($;@) { #{{{
  634. my $page=shift;
  635. if (IkiWiki::Plugin::po::istranslatable($page)) {
  636. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  637. }
  638. else {
  639. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  640. }
  641. } #}}}
  642. sub match_lang ($$;@) { #{{{
  643. my $page=shift;
  644. my $wanted=shift;
  645. my $regexp=IkiWiki::glob2re($wanted);
  646. my $lang=IkiWiki::Plugin::po::lang($page);
  647. if ($lang!~/^$regexp$/i) {
  648. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  649. }
  650. else {
  651. return IkiWiki::SuccessReason->new("file language is $wanted");
  652. }
  653. } #}}}
  654. sub match_currentlang ($$;@) { #{{{
  655. my $page=shift;
  656. shift;
  657. my %params=@_;
  658. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  659. my $currentlang=IkiWiki::Plugin::po::lang($params{location});
  660. my $lang=IkiWiki::Plugin::po::lang($page);
  661. if ($lang eq $currentlang) {
  662. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  663. }
  664. else {
  665. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  666. }
  667. } #}}}
  668. 1