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