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