summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 127d6d47f3496d2aa5c19c126cbb66ed5ec4793b (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 => "tweakurlpath", id => "po", call => \&tweakurlpath);
  34. hook(type => "filter", id => "po", call => \&filter);
  35. hook(type => "htmlize", id => "po", call => \&htmlize);
  36. hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
  37. inject(name => "IkiWiki::bestlink", call => \&mybestlink);
  38. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  39. inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
  40. }
  41. sub getsetup () { #{{{
  42. return
  43. plugin => {
  44. safe => 0,
  45. rebuild => 1, # format plugin
  46. },
  47. po_master_language => {
  48. type => "string",
  49. example => {
  50. 'code' => 'en',
  51. 'name' => 'English'
  52. },
  53. description => "master language (non-PO files)",
  54. safe => 0,
  55. rebuild => 1,
  56. },
  57. po_slave_languages => {
  58. type => "string",
  59. example => {
  60. 'fr' => 'Français',
  61. 'es' => 'Castellano',
  62. 'de' => 'Deutsch'
  63. },
  64. description => "slave languages (PO files)",
  65. safe => 0,
  66. rebuild => 1,
  67. },
  68. po_translatable_pages => {
  69. type => "pagespec",
  70. example => "!*/Discussion",
  71. description => "PageSpec controlling which pages are translatable",
  72. link => "ikiwiki/PageSpec",
  73. safe => 0,
  74. rebuild => 1,
  75. },
  76. po_link_to => {
  77. type => "string",
  78. example => "current",
  79. description => "internal linking behavior (default/current/negotiated)",
  80. safe => 0,
  81. rebuild => 1,
  82. },
  83. } #}}}
  84. sub checkconfig () { #{{{
  85. foreach my $field (qw{po_master_language po_slave_languages}) {
  86. if (! exists $config{$field} || ! defined $config{$field}) {
  87. error(sprintf(gettext("Must specify %s"), $field));
  88. }
  89. }
  90. if (! exists $config{po_link_to} ||
  91. ! defined $config{po_link_to}) {
  92. $config{po_link_to}="default";
  93. }
  94. if (! exists $config{po_translatable_pages} ||
  95. ! defined $config{po_translatable_pages}) {
  96. $config{po_translatable_pages}="";
  97. }
  98. if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
  99. error(gettext("po_link_to=negotiated requires usedirs to be set"));
  100. }
  101. push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
  102. } #}}}
  103. sub potfile ($) { #{{{
  104. my $masterfile=shift;
  105. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  106. return File::Spec->catfile($dir, $name . ".pot");
  107. } #}}}
  108. sub pofile ($$) { #{{{
  109. my $masterfile=shift;
  110. my $lang=shift;
  111. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  112. return File::Spec->catfile($dir, $name . "." . $lang . ".po");
  113. } #}}}
  114. sub refreshpot ($) { #{{{
  115. my $masterfile=shift;
  116. my $potfile=potfile($masterfile);
  117. my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
  118. my $doc=Locale::Po4a::Chooser::new('text',%options);
  119. $doc->read($masterfile);
  120. $doc->{TT}{utf_mode} = 1;
  121. $doc->{TT}{file_in_charset} = 'utf-8';
  122. $doc->{TT}{file_out_charset} = 'utf-8';
  123. # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
  124. # this is undocument use of internal Locale::Po4a::TransTractor's data,
  125. # compulsory since this module prevents us from using the porefs option.
  126. my %po_options = ('porefs' => 'none');
  127. $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
  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. %filtered=undef;
  191. %translations=undef;
  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} || $page 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 mybestlink ($$) { #{{{
  234. my $page=shift;
  235. my $link=shift;
  236. my $res=$origsubs{'bestlink'}->($page, $link);
  237. if (length $res) {
  238. if ($config{po_link_to} eq "current"
  239. && istranslatable($res)
  240. && istranslation($page)) {
  241. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  242. return $res . "." . $curlang;
  243. }
  244. else {
  245. return $res;
  246. }
  247. }
  248. return "";
  249. } #}}}
  250. # We use filter to convert PO to the master page's type,
  251. # since other plugins should not work on PO files
  252. sub filter (@) { #{{{
  253. my %params = @_;
  254. my $page = $params{page};
  255. my $destpage = $params{destpage};
  256. my $content = decode_utf8(encode_utf8($params{content}));
  257. # decide if this is a PO file that should be converted into a translated document,
  258. # and perform various sanity checks
  259. if (! istranslation($page) || $filtered{$page}{$destpage}) {
  260. return $content;
  261. }
  262. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  263. my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
  264. my $masterfile = srcfile($pagesources{$masterpage});
  265. my (@pos,@masters);
  266. push @pos,$file;
  267. push @masters,$masterfile;
  268. my %options = (
  269. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  270. );
  271. my $doc=Locale::Po4a::Chooser::new('text',%options);
  272. $doc->process(
  273. 'po_in_name' => \@pos,
  274. 'file_in_name' => \@masters,
  275. 'file_in_charset' => 'utf-8',
  276. 'file_out_charset' => 'utf-8',
  277. ) or error("[po/filter:$file]: failed to translate");
  278. my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
  279. my $tmpout = $tmpfh->filename;
  280. $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
  281. $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
  282. $filtered{$page}{$destpage}=1;
  283. return $content;
  284. } #}}}
  285. sub htmlize (@) { #{{{
  286. my %params=@_;
  287. my $page = $params{page};
  288. my $content = $params{content};
  289. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  290. my $masterfile = srcfile($pagesources{$masterpage});
  291. # force content to be htmlize'd as if it was the same type as the master page
  292. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  293. } #}}}
  294. sub percenttranslated ($) { #{{{
  295. my $page=shift;
  296. return "N/A" unless (istranslation($page));
  297. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  298. my $file=srcfile($pagesources{$page});
  299. my $masterfile = srcfile($pagesources{$masterpage});
  300. my (@pos,@masters);
  301. push @pos,$file;
  302. push @masters,$masterfile;
  303. my %options = (
  304. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  305. );
  306. my $doc=Locale::Po4a::Chooser::new('text',%options);
  307. $doc->process(
  308. 'po_in_name' => \@pos,
  309. 'file_in_name' => \@masters,
  310. 'file_in_charset' => 'utf-8',
  311. 'file_out_charset' => 'utf-8',
  312. ) or error("[po/percenttranslated:$file]: failed to translate");
  313. my ($percent,$hit,$queries) = $doc->stats();
  314. return $percent;
  315. } #}}}
  316. sub otherlanguages ($) { #{{{
  317. my $page=shift;
  318. my @ret;
  319. if (istranslatable($page)) {
  320. foreach my $lang (sort keys %{$translations{$page}}) {
  321. my $translation = $translations{$page}{$lang};
  322. push @ret, {
  323. url => urlto($translation, $page),
  324. code => $lang,
  325. language => $config{po_slave_languages}{$lang},
  326. percent => percenttranslated($translation),
  327. };
  328. }
  329. }
  330. elsif (istranslation($page)) {
  331. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  332. push @ret, {
  333. url => urlto($masterpage, $page),
  334. code => $config{po_master_language}{code},
  335. language => $config{po_master_language}{name},
  336. master => 1,
  337. };
  338. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  339. push @ret, {
  340. url => urlto($translations{$masterpage}{$lang}, $page),
  341. code => $lang,
  342. language => $config{po_slave_languages}{$lang},
  343. percent => percenttranslated($translations{$masterpage}{$lang}),
  344. } unless ($lang eq $curlang);
  345. }
  346. }
  347. return @ret;
  348. } #}}}
  349. sub pagetemplate (@) { #{{{
  350. my %params=@_;
  351. my $page=$params{page};
  352. my $template=$params{template};
  353. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  354. $template->param(percenttranslated => percenttranslated($page));
  355. }
  356. if ($template->query(name => "istranslation")) {
  357. $template->param(istranslation => istranslation($page));
  358. }
  359. if ($template->query(name => "istranslatable")) {
  360. $template->param(istranslatable => istranslatable($page));
  361. }
  362. if ($template->query(name => "otherlanguages")) {
  363. $template->param(otherlanguages => [otherlanguages($page)]);
  364. if (istranslatable($page)) {
  365. foreach my $translation (values %{$translations{$page}}) {
  366. add_depends($page, $translation);
  367. }
  368. }
  369. elsif (istranslation($page)) {
  370. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  371. add_depends($page, $masterpage);
  372. foreach my $translation (values %{$translations{$masterpage}}) {
  373. add_depends($page, $translation);
  374. }
  375. }
  376. }
  377. } # }}}
  378. sub istranslatable ($) { #{{{
  379. my $page=shift;
  380. my $file=$pagesources{$page};
  381. if (! defined $file
  382. || (defined pagetype($file) && pagetype($file) eq 'po')
  383. || $file =~ /\.pot$/) {
  384. return 0;
  385. }
  386. return pagespec_match($page, $config{po_translatable_pages});
  387. } #}}}
  388. sub _istranslation ($) { #{{{
  389. my $page=shift;
  390. my $file=$pagesources{$page};
  391. if (! defined $file) {
  392. return IkiWiki::FailReason->new("no file specified");
  393. }
  394. if (! defined $file
  395. || ! defined pagetype($file)
  396. || ! pagetype($file) eq 'po'
  397. || $file =~ /\.pot$/) {
  398. return 0;
  399. }
  400. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  401. if (! defined $masterpage || ! defined $lang
  402. || ! (length($masterpage) > 0) || ! (length($lang) > 0)
  403. || ! defined $pagesources{$masterpage}
  404. || ! defined $config{po_slave_languages}{$lang}) {
  405. return 0;
  406. }
  407. return istranslatable($masterpage);
  408. } #}}}
  409. sub istranslation ($) { #{{{
  410. my $page=shift;
  411. if (_istranslation($page)) {
  412. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  413. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  414. return 1;
  415. }
  416. return 0;
  417. } #}}}
  418. package IkiWiki::PageSpec;
  419. use warnings;
  420. use strict;
  421. use IkiWiki 2.00;
  422. sub match_istranslation ($;@) { #{{{
  423. my $page=shift;
  424. if (IkiWiki::Plugin::po::istranslation($page)) {
  425. return IkiWiki::SuccessReason->new("is a translation page");
  426. }
  427. else {
  428. return IkiWiki::FailReason->new("is not a translation page");
  429. }
  430. } #}}}
  431. sub match_istranslatable ($;@) { #{{{
  432. my $page=shift;
  433. if (IkiWiki::Plugin::po::istranslatable($page)) {
  434. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  435. }
  436. else {
  437. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  438. }
  439. } #}}}
  440. sub match_lang ($$;@) { #{{{
  441. my $page=shift;
  442. my $wanted=shift;
  443. my $regexp=IkiWiki::glob2re($wanted);
  444. my $lang;
  445. my $masterpage;
  446. if (IkiWiki::Plugin::po::istranslation($page)) {
  447. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  448. }
  449. else {
  450. $lang = $config{po_master_language}{code};
  451. }
  452. if ($lang!~/^$regexp$/i) {
  453. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  454. }
  455. else {
  456. return IkiWiki::SuccessReason->new("file language is $wanted");
  457. }
  458. } #}}}
  459. sub match_currentlang ($$;@) { #{{{
  460. my $page=shift;
  461. shift;
  462. my %params=@_;
  463. my ($currentmasterpage, $currentlang, $masterpage, $lang);
  464. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  465. if (IkiWiki::Plugin::po::istranslation($params{location})) {
  466. ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
  467. }
  468. else {
  469. $currentlang = $config{po_master_language}{code};
  470. }
  471. if (IkiWiki::Plugin::po::istranslation($page)) {
  472. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  473. }
  474. else {
  475. $lang = $config{po_master_language}{code};
  476. }
  477. if ($lang eq $currentlang) {
  478. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  479. }
  480. else {
  481. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  482. }
  483. } #}}}
  484. 1