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