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