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