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