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