summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 9dda8c0db29d8116acb7e4417276865bbe462897 (plain)
  1. #!/usr/bin/perl
  2. # .po as a wiki page type
  3. # inspired by the GPL'd po4a-translate,
  4. # which is Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
  5. package IkiWiki::Plugin::po;
  6. use warnings;
  7. use strict;
  8. use IkiWiki 2.00;
  9. use Encode;
  10. use Locale::Po4a::Chooser;
  11. use Locale::Po4a::Po;
  12. use File::Basename;
  13. use File::Copy;
  14. use File::Spec;
  15. use File::Temp;
  16. use Memoize;
  17. my %translations;
  18. our %filtered;
  19. ## FIXME: makes some test cases cry once every two tries; this may be
  20. ## related to the artificial way the testsuite is run, or not.
  21. # memoize("istranslatable");
  22. memoize("_istranslation");
  23. memoize("percenttranslated");
  24. # backup references to subs that will be overriden
  25. my %origsubs;
  26. $origsubs{'bestlink'}=\&IkiWiki::bestlink;
  27. $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
  28. $origsubs{'targetpage'}=\&IkiWiki::targetpage;
  29. sub import {
  30. hook(type => "getsetup", id => "po", call => \&getsetup);
  31. hook(type => "checkconfig", id => "po", call => \&checkconfig);
  32. hook(type => "needsbuild", id => "po", call => \&needsbuild);
  33. hook(type => "filter", id => "po", call => \&filter);
  34. hook(type => "htmlize", id => "po", call => \&htmlize);
  35. hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
  36. inject(name => "IkiWiki::bestlink", call => \&mybestlink);
  37. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  38. inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
  39. }
  40. sub getsetup () { #{{{
  41. return
  42. plugin => {
  43. safe => 0,
  44. rebuild => 1, # format plugin
  45. },
  46. po_master_language => {
  47. type => "string",
  48. example => {
  49. 'code' => 'en',
  50. 'name' => 'English'
  51. },
  52. description => "master language (non-PO files)",
  53. safe => 0,
  54. rebuild => 1,
  55. },
  56. po_slave_languages => {
  57. type => "string",
  58. example => {
  59. 'fr' => 'Français',
  60. 'es' => 'Castellano',
  61. 'de' => 'Deutsch'
  62. },
  63. description => "slave languages (PO files)",
  64. safe => 0,
  65. rebuild => 1,
  66. },
  67. po_translatable_pages => {
  68. type => "pagespec",
  69. example => "!*/Discussion",
  70. description => "PageSpec controlling which pages are translatable",
  71. link => "ikiwiki/PageSpec",
  72. safe => 0,
  73. rebuild => 1,
  74. },
  75. po_link_to => {
  76. type => "string",
  77. example => "current",
  78. description => "internal linking behavior (default/current/negotiated)",
  79. safe => 0,
  80. rebuild => 1,
  81. },
  82. } #}}}
  83. sub checkconfig () { #{{{
  84. foreach my $field (qw{po_master_language po_slave_languages}) {
  85. if (! exists $config{$field} || ! defined $config{$field}) {
  86. error(sprintf(gettext("Must specify %s"), $field));
  87. }
  88. }
  89. if (! exists $config{po_link_to} ||
  90. ! defined $config{po_link_to}) {
  91. $config{po_link_to}="default";
  92. }
  93. if (! exists $config{po_translatable_pages} ||
  94. ! defined $config{po_translatable_pages}) {
  95. $config{po_translatable_pages}="";
  96. }
  97. if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
  98. error(gettext("po_link_to=negotiated requires usedirs to be set"));
  99. }
  100. push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
  101. } #}}}
  102. sub potfile ($) { #{{{
  103. my $masterfile=shift;
  104. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  105. return File::Spec->catfile($dir, $name . ".pot");
  106. } #}}}
  107. sub pofile ($$) { #{{{
  108. my $masterfile=shift;
  109. my $lang=shift;
  110. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  111. return File::Spec->catfile($dir, $name . "." . $lang . ".po");
  112. } #}}}
  113. sub refreshpot ($) { #{{{
  114. my $masterfile=shift;
  115. my $potfile=potfile($masterfile);
  116. my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
  117. my $doc=Locale::Po4a::Chooser::new('text',%options);
  118. $doc->read($masterfile);
  119. $doc->{TT}{utf_mode} = 1;
  120. $doc->{TT}{file_in_charset} = 'utf-8';
  121. $doc->{TT}{file_out_charset} = 'utf-8';
  122. # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
  123. # this is undocument use of internal Locale::Po4a::TransTractor's data,
  124. # compulsory since this module prevents us from using the porefs option.
  125. my %po_options = ('porefs' => 'none');
  126. $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
  127. $doc->{TT}{po_out}->set_charset('utf-8');
  128. # do the actual work
  129. $doc->parse;
  130. $doc->writepo($potfile);
  131. } #}}}
  132. sub refreshpofiles ($@) { #{{{
  133. my $masterfile=shift;
  134. my @pofiles=@_;
  135. my $potfile=potfile($masterfile);
  136. error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
  137. foreach my $pofile (@pofiles) {
  138. if (-e $pofile) {
  139. my $cmd = "msgmerge -U --backup=none $pofile $potfile";
  140. system ($cmd) == 0
  141. or error("[po/refreshpofiles:$pofile] failed to update");
  142. }
  143. else {
  144. File::Copy::syscopy($potfile,$pofile)
  145. or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
  146. }
  147. }
  148. } #}}}
  149. sub needsbuild () { #{{{
  150. my $needsbuild=shift;
  151. # build %translations, using istranslation's side-effect
  152. foreach my $page (keys %pagesources) {
  153. istranslation($page);
  154. }
  155. # refresh/create POT and PO files as needed
  156. my $updated_po_files=0;
  157. foreach my $page (keys %pagesources) {
  158. my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
  159. if (istranslatable($page)) {
  160. my $file=srcfile($pagesources{$page});
  161. if ($pageneedsbuild || ! -e potfile($file)) {
  162. refreshpot($file);
  163. }
  164. my @pofiles;
  165. foreach my $lang (keys %{$config{po_slave_languages}}) {
  166. my $pofile=pofile($file, $lang);
  167. if ($pageneedsbuild || ! -e $pofile) {
  168. push @pofiles, $pofile;
  169. }
  170. }
  171. if (@pofiles) {
  172. refreshpofiles($file, @pofiles) ;
  173. map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
  174. $updated_po_files = 1;
  175. }
  176. }
  177. }
  178. # check staged changes in and trigger a wiki refresh.
  179. if ($updated_po_files) {
  180. if ($config{rcs}) {
  181. IkiWiki::disable_commit_hook();
  182. IkiWiki::rcs_commit_staged(gettext("updated PO files"),
  183. "refreshpofiles", "127.0.0.1");
  184. IkiWiki::enable_commit_hook();
  185. IkiWiki::rcs_update();
  186. }
  187. IkiWiki::refresh();
  188. IkiWiki::saveindex();
  189. # refresh module's private variables
  190. undef %filtered;
  191. undef %translations;
  192. foreach my $page (keys %pagesources) {
  193. istranslation($page);
  194. }
  195. }
  196. # make existing translations depend on the corresponding master page
  197. foreach my $master (keys %translations) {
  198. foreach my $slave (values %{$translations{$master}}) {
  199. add_depends($slave, $master);
  200. }
  201. }
  202. } #}}}
  203. sub mytargetpage ($$) { #{{{
  204. my $page=shift;
  205. my $ext=shift;
  206. if (istranslation($page)) {
  207. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  208. if (! $config{usedirs} || $masterpage eq 'index') {
  209. return $masterpage . "." . $lang . "." . $ext;
  210. }
  211. else {
  212. return $masterpage . "/index." . $lang . "." . $ext;
  213. }
  214. }
  215. elsif (istranslatable($page)) {
  216. if (! $config{usedirs} || $page eq 'index') {
  217. return $page . "." . $config{po_master_language}{code} . "." . $ext;
  218. }
  219. else {
  220. return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
  221. }
  222. }
  223. return $origsubs{'targetpage'}->($page, $ext);
  224. } #}}}
  225. sub mybeautify_urlpath ($) { #{{{
  226. my $url=shift;
  227. my $res=$origsubs{'beautify_urlpath'}->($url);
  228. if ($config{po_link_to} eq "negotiated") {
  229. $res =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
  230. }
  231. return $res;
  232. } #}}}
  233. sub urlto_with_orig_beautiful_urlpath($$) { #{{{
  234. my $to=shift;
  235. my $from=shift;
  236. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
  237. my $res=urlto($to, $from);
  238. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  239. return $res;
  240. } #}}}
  241. sub mybestlink ($$) { #{{{
  242. my $page=shift;
  243. my $link=shift;
  244. my $res=$origsubs{'bestlink'}->($page, $link);
  245. if (length $res) {
  246. if ($config{po_link_to} eq "current"
  247. && istranslatable($res)
  248. && istranslation($page)) {
  249. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  250. return $res . "." . $curlang;
  251. }
  252. else {
  253. return $res;
  254. }
  255. }
  256. return "";
  257. } #}}}
  258. # We use filter to convert PO to the master page's type,
  259. # since other plugins should not work on PO files
  260. sub filter (@) { #{{{
  261. my %params = @_;
  262. my $page = $params{page};
  263. my $destpage = $params{destpage};
  264. my $content = decode_utf8(encode_utf8($params{content}));
  265. # decide if this is a PO file that should be converted into a translated document,
  266. # and perform various sanity checks
  267. if (! istranslation($page) || $filtered{$page}{$destpage}) {
  268. return $content;
  269. }
  270. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  271. my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
  272. my $masterfile = srcfile($pagesources{$masterpage});
  273. my (@pos,@masters);
  274. push @pos,$file;
  275. push @masters,$masterfile;
  276. my %options = (
  277. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  278. );
  279. my $doc=Locale::Po4a::Chooser::new('text',%options);
  280. $doc->process(
  281. 'po_in_name' => \@pos,
  282. 'file_in_name' => \@masters,
  283. 'file_in_charset' => 'utf-8',
  284. 'file_out_charset' => 'utf-8',
  285. ) or error("[po/filter:$file]: failed to translate");
  286. my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
  287. my $tmpout = $tmpfh->filename;
  288. $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
  289. $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
  290. $filtered{$page}{$destpage}=1;
  291. return $content;
  292. } #}}}
  293. sub htmlize (@) { #{{{
  294. my %params=@_;
  295. my $page = $params{page};
  296. my $content = $params{content};
  297. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  298. my $masterfile = srcfile($pagesources{$masterpage});
  299. # force content to be htmlize'd as if it was the same type as the master page
  300. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  301. } #}}}
  302. sub percenttranslated ($) { #{{{
  303. my $page=shift;
  304. return "N/A" unless (istranslation($page));
  305. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  306. my $file=srcfile($pagesources{$page});
  307. my $masterfile = srcfile($pagesources{$masterpage});
  308. my (@pos,@masters);
  309. push @pos,$file;
  310. push @masters,$masterfile;
  311. my %options = (
  312. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  313. );
  314. my $doc=Locale::Po4a::Chooser::new('text',%options);
  315. $doc->process(
  316. 'po_in_name' => \@pos,
  317. 'file_in_name' => \@masters,
  318. 'file_in_charset' => 'utf-8',
  319. 'file_out_charset' => 'utf-8',
  320. ) or error("[po/percenttranslated:$file]: failed to translate");
  321. my ($percent,$hit,$queries) = $doc->stats();
  322. return $percent;
  323. } #}}}
  324. sub otherlanguages ($) { #{{{
  325. my $page=shift;
  326. my @ret;
  327. if (istranslatable($page)) {
  328. foreach my $lang (sort keys %{$translations{$page}}) {
  329. my $translation = $translations{$page}{$lang};
  330. push @ret, {
  331. url => urlto($translation, $page),
  332. code => $lang,
  333. language => $config{po_slave_languages}{$lang},
  334. percent => percenttranslated($translation),
  335. };
  336. }
  337. }
  338. elsif (istranslation($page)) {
  339. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  340. push @ret, {
  341. url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
  342. code => $config{po_master_language}{code},
  343. language => $config{po_master_language}{name},
  344. master => 1,
  345. };
  346. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  347. push @ret, {
  348. url => urlto($translations{$masterpage}{$lang}, $page),
  349. code => $lang,
  350. language => $config{po_slave_languages}{$lang},
  351. percent => percenttranslated($translations{$masterpage}{$lang}),
  352. } unless ($lang eq $curlang);
  353. }
  354. }
  355. return @ret;
  356. } #}}}
  357. sub pagetemplate (@) { #{{{
  358. my %params=@_;
  359. my $page=$params{page};
  360. my $destpage=$params{destpage};
  361. my $template=$params{template};
  362. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  363. $template->param(percenttranslated => percenttranslated($page));
  364. }
  365. if ($template->query(name => "istranslation")) {
  366. $template->param(istranslation => istranslation($page));
  367. }
  368. if ($template->query(name => "istranslatable")) {
  369. $template->param(istranslatable => istranslatable($page));
  370. }
  371. if ($template->query(name => "otherlanguages")) {
  372. $template->param(otherlanguages => [otherlanguages($page)]);
  373. if (istranslatable($page)) {
  374. foreach my $translation (values %{$translations{$page}}) {
  375. add_depends($page, $translation);
  376. }
  377. }
  378. elsif (istranslation($page)) {
  379. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  380. add_depends($page, $masterpage);
  381. foreach my $translation (values %{$translations{$masterpage}}) {
  382. add_depends($page, $translation);
  383. }
  384. }
  385. }
  386. # Rely on IkiWiki::Render's genpage() to decide wether
  387. # a discussion link should appear on $page; this is not
  388. # totally accurate, though: some broken links may be generated
  389. # when cgiurl is disabled.
  390. # This compromise avoids some code duplication, and will probably
  391. # prevent future breakage when ikiwiki internals change.
  392. # Known limitations are preferred to future random bugs.
  393. if ($template->param('discussionlink') && istranslation($page)) {
  394. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  395. $template->param('discussionlink' => htmllink(
  396. $page,
  397. $destpage,
  398. $masterpage . '/' . gettext("Discussion"),
  399. noimageinline => 1,
  400. forcesubpage => 0,
  401. linktext => gettext("Discussion"),
  402. ));
  403. }
  404. } # }}}
  405. sub istranslatable ($) { #{{{
  406. my $page=shift;
  407. my $file=$pagesources{$page};
  408. if (! defined $file
  409. || (defined pagetype($file) && pagetype($file) eq 'po')
  410. || $file =~ /\.pot$/) {
  411. return 0;
  412. }
  413. return pagespec_match($page, $config{po_translatable_pages});
  414. } #}}}
  415. sub _istranslation ($) { #{{{
  416. my $page=shift;
  417. my $file=$pagesources{$page};
  418. if (! defined $file) {
  419. return IkiWiki::FailReason->new("no file specified");
  420. }
  421. if (! defined $file
  422. || ! defined pagetype($file)
  423. || ! pagetype($file) eq 'po'
  424. || $file =~ /\.pot$/) {
  425. return 0;
  426. }
  427. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  428. if (! defined $masterpage || ! defined $lang
  429. || ! (length($masterpage) > 0) || ! (length($lang) > 0)
  430. || ! defined $pagesources{$masterpage}
  431. || ! defined $config{po_slave_languages}{$lang}) {
  432. return 0;
  433. }
  434. return istranslatable($masterpage);
  435. } #}}}
  436. sub istranslation ($) { #{{{
  437. my $page=shift;
  438. if (_istranslation($page)) {
  439. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  440. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  441. return 1;
  442. }
  443. return 0;
  444. } #}}}
  445. package IkiWiki::PageSpec;
  446. use warnings;
  447. use strict;
  448. use IkiWiki 2.00;
  449. sub match_istranslation ($;@) { #{{{
  450. my $page=shift;
  451. if (IkiWiki::Plugin::po::istranslation($page)) {
  452. return IkiWiki::SuccessReason->new("is a translation page");
  453. }
  454. else {
  455. return IkiWiki::FailReason->new("is not a translation page");
  456. }
  457. } #}}}
  458. sub match_istranslatable ($;@) { #{{{
  459. my $page=shift;
  460. if (IkiWiki::Plugin::po::istranslatable($page)) {
  461. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  462. }
  463. else {
  464. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  465. }
  466. } #}}}
  467. sub match_lang ($$;@) { #{{{
  468. my $page=shift;
  469. my $wanted=shift;
  470. my $regexp=IkiWiki::glob2re($wanted);
  471. my $lang;
  472. my $masterpage;
  473. if (IkiWiki::Plugin::po::istranslation($page)) {
  474. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  475. }
  476. else {
  477. $lang = $config{po_master_language}{code};
  478. }
  479. if ($lang!~/^$regexp$/i) {
  480. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  481. }
  482. else {
  483. return IkiWiki::SuccessReason->new("file language is $wanted");
  484. }
  485. } #}}}
  486. sub match_currentlang ($$;@) { #{{{
  487. my $page=shift;
  488. shift;
  489. my %params=@_;
  490. my ($currentmasterpage, $currentlang, $masterpage, $lang);
  491. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  492. if (IkiWiki::Plugin::po::istranslation($params{location})) {
  493. ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
  494. }
  495. else {
  496. $currentlang = $config{po_master_language}{code};
  497. }
  498. if (IkiWiki::Plugin::po::istranslation($page)) {
  499. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  500. }
  501. else {
  502. $lang = $config{po_master_language}{code};
  503. }
  504. if ($lang eq $currentlang) {
  505. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  506. }
  507. else {
  508. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  509. }
  510. } #}}}
  511. 1