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