summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: d8a7de8eb529600204ccdf0eaac4e467a70477c7 (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. debug "bla".$rename->{src};
  305. while (my ($lang, $otherpage) = each %otherpages) {
  306. push @{$torename}, {
  307. src => $otherpage,
  308. srcfile => $pagesources{$otherpage},
  309. dest => otherlanguage($rename->{dest}, $lang),
  310. destfile => $rename->{dest}.".".$lang.".po",
  311. required => 0,
  312. };
  313. debug "po(renamepages): pushed src=$otherpage, dest=".otherlanguage($rename->{dest}, $lang);
  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. # 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. resettranslationscache();
  362. flushmemoizecache();
  363. # Trigger a wiki refresh.
  364. require IkiWiki::Render;
  365. # without preliminary saveindex/loadindex, refresh()
  366. # complains about a lot of uninitialized variables
  367. IkiWiki::saveindex();
  368. IkiWiki::loadindex();
  369. IkiWiki::refresh();
  370. IkiWiki::saveindex();
  371. }
  372. } #}}}
  373. # As we're previewing or saving a page, the content may have
  374. # changed, so tell the next filter() invocation it must not be lazy.
  375. sub editcontent () { #{{{
  376. my %params=@_;
  377. unsetalreadyfiltered($params{page}, $params{page});
  378. return $params{content};
  379. } #}}}
  380. # ,----
  381. # | Injected functions
  382. # `----
  383. # Implement po_link_to 'current' and 'negotiated' settings.
  384. sub mybestlink ($$) { #{{{
  385. my $page=shift;
  386. my $link=shift;
  387. my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
  388. if (length $res
  389. && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
  390. && istranslatable($res)
  391. && istranslation($page)) {
  392. return $res . "." . lang($page);
  393. }
  394. return $res;
  395. } #}}}
  396. sub mybeautify_urlpath ($) { #{{{
  397. my $url=shift;
  398. my $res=$origsubs{'beautify_urlpath'}->($url);
  399. if ($config{po_link_to} eq "negotiated") {
  400. $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
  401. $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
  402. map {
  403. $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
  404. } (keys %{$config{po_slave_languages}});
  405. }
  406. return $res;
  407. } #}}}
  408. sub mytargetpage ($$) { #{{{
  409. my $page=shift;
  410. my $ext=shift;
  411. if (istranslation($page) || istranslatable($page)) {
  412. my ($masterpage, $lang) = (masterpage($page), lang($page));
  413. if (! $config{usedirs} || $masterpage eq 'index') {
  414. return $masterpage . "." . $lang . "." . $ext;
  415. }
  416. else {
  417. return $masterpage . "/index." . $lang . "." . $ext;
  418. }
  419. }
  420. return $origsubs{'targetpage'}->($page, $ext);
  421. } #}}}
  422. sub myurlto ($$;$) { #{{{
  423. my $to=shift;
  424. my $from=shift;
  425. my $absolute=shift;
  426. # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
  427. if (! length $to
  428. && $config{po_link_to} eq "current"
  429. && istranslatable('index')) {
  430. return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
  431. }
  432. return $origsubs{'urlto'}->($to,$from,$absolute);
  433. } #}}}
  434. # ,----
  435. # | Blackboxes for private data
  436. # `----
  437. {
  438. my %filtered;
  439. sub alreadyfiltered($$) { #{{{
  440. my $page=shift;
  441. my $destpage=shift;
  442. return ( exists $filtered{$page}{$destpage}
  443. && $filtered{$page}{$destpage} eq 1 );
  444. } #}}}
  445. sub setalreadyfiltered($$) { #{{{
  446. my $page=shift;
  447. my $destpage=shift;
  448. $filtered{$page}{$destpage}=1;
  449. } #}}}
  450. sub unsetalreadyfiltered($$) { #{{{
  451. my $page=shift;
  452. my $destpage=shift;
  453. if (exists $filtered{$page}{$destpage}) {
  454. delete $filtered{$page}{$destpage};
  455. }
  456. } #}}}
  457. sub resetalreadyfiltered() { #{{{
  458. undef %filtered;
  459. } #}}}
  460. }
  461. # ,----
  462. # | Helper functions
  463. # `----
  464. sub maybe_add_leading_slash ($;$) { #{{{
  465. my $str=shift;
  466. my $add=shift;
  467. $add=1 unless defined $add;
  468. return '/' . $str if $add;
  469. return $str;
  470. } #}}}
  471. sub istranslatablefile ($) { #{{{
  472. my $file=shift;
  473. return 0 unless defined $file;
  474. return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
  475. return 0 if $file =~ /\.pot$/;
  476. return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
  477. return;
  478. } #}}}
  479. sub istranslatable ($) { #{{{
  480. my $page=shift;
  481. $page=~s#^/##;
  482. return 1 if istranslatablefile($pagesources{$page});
  483. return;
  484. } #}}}
  485. sub _istranslation ($) { #{{{
  486. my $page=shift;
  487. my $hasleadingslash = ($page=~s#^/##);
  488. my $file=$pagesources{$page};
  489. return 0 unless (defined $file
  490. && defined pagetype($file)
  491. && pagetype($file) eq 'po');
  492. return 0 if $file =~ /\.pot$/;
  493. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  494. return 0 unless (defined $masterpage && defined $lang
  495. && length $masterpage && length $lang
  496. && defined $pagesources{$masterpage}
  497. && defined $config{po_slave_languages}{$lang});
  498. return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
  499. if istranslatable($masterpage);
  500. } #}}}
  501. sub istranslation ($) { #{{{
  502. my $page=shift;
  503. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  504. my $hasleadingslash = ($masterpage=~s#^/##);
  505. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  506. return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
  507. }
  508. return;
  509. } #}}}
  510. sub masterpage ($) { #{{{
  511. my $page=shift;
  512. if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
  513. return $masterpage;
  514. }
  515. return $page;
  516. } #}}}
  517. sub lang ($) { #{{{
  518. my $page=shift;
  519. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  520. return $lang;
  521. }
  522. return $config{po_master_language}{code};
  523. } #}}}
  524. sub islanguagecode ($) { #{{{
  525. my $code=shift;
  526. return ($code =~ /^[a-z]{2}$/);
  527. } #}}}
  528. sub otherlanguage ($$) { #{{{
  529. my $page=shift;
  530. my $code=shift;
  531. return masterpage($page) if $code eq $config{po_master_language}{code};
  532. return masterpage($page) . '.' . $code;
  533. } #}}}
  534. sub otherlanguages ($) { #{{{
  535. my $page=shift;
  536. my %ret;
  537. return \%ret unless (istranslation($page) || istranslatable($page));
  538. my $curlang=lang($page);
  539. foreach my $lang
  540. ($config{po_master_language}{code}, keys %{$config{po_slave_languages}}) {
  541. next if $lang eq $curlang;
  542. $ret{$lang}=otherlanguage($page, $lang);
  543. }
  544. return \%ret;
  545. } #}}}
  546. sub potfile ($) { #{{{
  547. my $masterfile=shift;
  548. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  549. $dir='' if $dir eq './';
  550. return File::Spec->catpath('', $dir, $name . ".pot");
  551. } #}}}
  552. sub pofile ($$) { #{{{
  553. my $masterfile=shift;
  554. my $lang=shift;
  555. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  556. $dir='' if $dir eq './';
  557. return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
  558. } #}}}
  559. sub pofiles ($) { #{{{
  560. my $masterfile=shift;
  561. return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
  562. } #}}}
  563. sub refreshpot ($) { #{{{
  564. my $masterfile=shift;
  565. my $potfile=potfile($masterfile);
  566. my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
  567. my $doc=Locale::Po4a::Chooser::new('text',%options);
  568. $doc->{TT}{utf_mode} = 1;
  569. $doc->{TT}{file_in_charset} = 'utf-8';
  570. $doc->{TT}{file_out_charset} = 'utf-8';
  571. $doc->read($masterfile);
  572. # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
  573. # this is undocument use of internal Locale::Po4a::TransTractor's data,
  574. # compulsory since this module prevents us from using the porefs option.
  575. my %po_options = ('porefs' => 'none');
  576. $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
  577. $doc->{TT}{po_out}->set_charset('utf-8');
  578. # do the actual work
  579. $doc->parse;
  580. IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
  581. $doc->writepo($potfile);
  582. } #}}}
  583. sub refreshpofiles ($@) { #{{{
  584. my $masterfile=shift;
  585. my @pofiles=@_;
  586. my $potfile=potfile($masterfile);
  587. error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
  588. foreach my $pofile (@pofiles) {
  589. IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
  590. if (-e $pofile) {
  591. system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
  592. or error("[po/refreshpofiles:$pofile] failed to update");
  593. }
  594. else {
  595. File::Copy::syscopy($potfile,$pofile)
  596. or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
  597. }
  598. }
  599. } #}}}
  600. sub buildtranslationscache() { #{{{
  601. # use istranslation's side-effect
  602. map istranslation($_), (keys %pagesources);
  603. } #}}}
  604. sub resettranslationscache() { #{{{
  605. undef %translations;
  606. } #}}}
  607. sub flushmemoizecache() { #{{{
  608. Memoize::flush_cache("istranslatable");
  609. Memoize::flush_cache("_istranslation");
  610. Memoize::flush_cache("percenttranslated");
  611. } #}}}
  612. sub urlto_with_orig_beautiful_urlpath($$) { #{{{
  613. my $to=shift;
  614. my $from=shift;
  615. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
  616. my $res=urlto($to, $from);
  617. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  618. return $res;
  619. } #}}}
  620. sub percenttranslated ($) { #{{{
  621. my $page=shift;
  622. return gettext("N/A") unless istranslation($page);
  623. my $file=srcfile($pagesources{$page});
  624. my $masterfile = srcfile($pagesources{masterpage($page)});
  625. my (@pos,@masters);
  626. push @pos,$file;
  627. push @masters,$masterfile;
  628. my %options = (
  629. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  630. );
  631. my $doc=Locale::Po4a::Chooser::new('text',%options);
  632. $doc->process(
  633. 'po_in_name' => \@pos,
  634. 'file_in_name' => \@masters,
  635. 'file_in_charset' => 'utf-8',
  636. 'file_out_charset' => 'utf-8',
  637. ) or error("[po/percenttranslated:$page]: failed to translate");
  638. my ($percent,$hit,$queries) = $doc->stats();
  639. return $percent;
  640. } #}}}
  641. sub languagename ($) { #{{{
  642. my $code=shift;
  643. return $config{po_master_language}{name}
  644. if $code eq $config{po_master_language}{code};
  645. return $config{po_slave_languages}{$code}
  646. if defined $config{po_slave_languages}{$code};
  647. return;
  648. } #}}}
  649. sub otherlanguagesloop ($) { #{{{
  650. my $page=shift;
  651. my @ret;
  652. my %otherpages=%{otherlanguages($page)};
  653. while (my ($lang, $otherpage) = each %otherpages) {
  654. if (istranslation($page) && masterpage($page) eq $otherpage) {
  655. push @ret, {
  656. url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
  657. code => $lang,
  658. language => languagename($lang),
  659. master => 1,
  660. };
  661. }
  662. else {
  663. push @ret, {
  664. url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
  665. code => $lang,
  666. language => languagename($lang),
  667. percent => percenttranslated($otherpage),
  668. }
  669. }
  670. }
  671. return sort {
  672. return -1 if $a->{code} eq $config{po_master_language}{code};
  673. return 1 if $b->{code} eq $config{po_master_language}{code};
  674. return $a->{language} cmp $b->{language};
  675. } @ret;
  676. } #}}}
  677. sub homepageurl (;$) { #{{{
  678. my $page=shift;
  679. return urlto('', $page);
  680. } #}}}
  681. sub deletetranslations ($) { #{{{
  682. my $deletedmasterfile=shift;
  683. debug "po(deletetranslations): TODO: delete translations of $deletedmasterfile";
  684. } #}}}
  685. # ,----
  686. # | PageSpec's
  687. # `----
  688. package IkiWiki::PageSpec;
  689. use warnings;
  690. use strict;
  691. use IkiWiki 2.00;
  692. sub match_istranslation ($;@) { #{{{
  693. my $page=shift;
  694. if (IkiWiki::Plugin::po::istranslation($page)) {
  695. return IkiWiki::SuccessReason->new("is a translation page");
  696. }
  697. else {
  698. return IkiWiki::FailReason->new("is not a translation page");
  699. }
  700. } #}}}
  701. sub match_istranslatable ($;@) { #{{{
  702. my $page=shift;
  703. if (IkiWiki::Plugin::po::istranslatable($page)) {
  704. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  705. }
  706. else {
  707. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  708. }
  709. } #}}}
  710. sub match_lang ($$;@) { #{{{
  711. my $page=shift;
  712. my $wanted=shift;
  713. my $regexp=IkiWiki::glob2re($wanted);
  714. my $lang=IkiWiki::Plugin::po::lang($page);
  715. if ($lang!~/^$regexp$/i) {
  716. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  717. }
  718. else {
  719. return IkiWiki::SuccessReason->new("file language is $wanted");
  720. }
  721. } #}}}
  722. sub match_currentlang ($$;@) { #{{{
  723. my $page=shift;
  724. shift;
  725. my %params=@_;
  726. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  727. my $currentlang=IkiWiki::Plugin::po::lang($params{location});
  728. my $lang=IkiWiki::Plugin::po::lang($page);
  729. if ($lang eq $currentlang) {
  730. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  731. }
  732. else {
  733. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  734. }
  735. } #}}}
  736. 1