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