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