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