summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 4608f59c7d2ffb8b1124e5f1e01133f5daf1be55 (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. # do the actual work
  128. $doc->parse;
  129. $doc->writepo($potfile);
  130. } #}}}
  131. sub refreshpofiles ($@) { #{{{
  132. my $masterfile=shift;
  133. my @pofiles=@_;
  134. my $potfile=potfile($masterfile);
  135. error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
  136. foreach my $pofile (@pofiles) {
  137. if (-e $pofile) {
  138. my $cmd = "msgmerge -U --backup=none $pofile $potfile";
  139. system ($cmd) == 0
  140. or error("[po/refreshpofiles:$pofile] failed to update");
  141. }
  142. else {
  143. File::Copy::syscopy($potfile,$pofile)
  144. or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
  145. }
  146. }
  147. } #}}}
  148. sub needsbuild () { #{{{
  149. my $needsbuild=shift;
  150. # build %translations, using istranslation's side-effect
  151. foreach my $page (keys %pagesources) {
  152. istranslation($page);
  153. }
  154. # refresh/create POT and PO files as needed
  155. my $updated_po_files=0;
  156. foreach my $page (keys %pagesources) {
  157. my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
  158. if (istranslatable($page)) {
  159. my $file=srcfile($pagesources{$page});
  160. if ($pageneedsbuild || ! -e potfile($file)) {
  161. refreshpot($file);
  162. }
  163. my @pofiles;
  164. foreach my $lang (keys %{$config{po_slave_languages}}) {
  165. my $pofile=pofile($file, $lang);
  166. if ($pageneedsbuild || ! -e $pofile) {
  167. push @pofiles, $pofile;
  168. }
  169. }
  170. if (@pofiles) {
  171. refreshpofiles($file, @pofiles) ;
  172. map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
  173. $updated_po_files = 1;
  174. }
  175. }
  176. }
  177. # check staged changes in and trigger a wiki refresh.
  178. if ($updated_po_files) {
  179. if ($config{rcs}) {
  180. IkiWiki::disable_commit_hook();
  181. IkiWiki::rcs_commit_staged(gettext("updated PO files"),
  182. "refreshpofiles", "127.0.0.1");
  183. IkiWiki::enable_commit_hook();
  184. IkiWiki::rcs_update();
  185. }
  186. IkiWiki::refresh();
  187. IkiWiki::saveindex();
  188. # refresh module's private variables
  189. %filtered=undef;
  190. %translations=undef;
  191. foreach my $page (keys %pagesources) {
  192. istranslation($page);
  193. }
  194. }
  195. # make existing translations depend on the corresponding master page
  196. foreach my $master (keys %translations) {
  197. foreach my $slave (values %{$translations{$master}}) {
  198. add_depends($slave, $master);
  199. }
  200. }
  201. } #}}}
  202. sub mytargetpage ($$) { #{{{
  203. my $page=shift;
  204. my $ext=shift;
  205. if (istranslation($page)) {
  206. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  207. if (! $config{usedirs} || $page eq 'index') {
  208. return $masterpage . "." . $lang . "." . $ext;
  209. }
  210. else {
  211. return $masterpage . "/index." . $lang . "." . $ext;
  212. }
  213. }
  214. elsif (istranslatable($page)) {
  215. if (! $config{usedirs} || $page eq 'index') {
  216. return $page . "." . $config{po_master_language}{code} . "." . $ext;
  217. }
  218. else {
  219. return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
  220. }
  221. }
  222. return $origsubs{'targetpage'}->($page, $ext);
  223. } #}}}
  224. sub mybeautify_urlpath ($) { #{{{
  225. my $url=shift;
  226. my $res=$origsubs{'beautify_urlpath'}->($url);
  227. if ($config{po_link_to} eq "negotiated") {
  228. $res =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
  229. }
  230. return $res;
  231. } #}}}
  232. sub mybestlink ($$) { #{{{
  233. my $page=shift;
  234. my $link=shift;
  235. my $res=$origsubs{'bestlink'}->($page, $link);
  236. if (length $res) {
  237. if ($config{po_link_to} eq "current"
  238. && istranslatable($res)
  239. && istranslation($page)) {
  240. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  241. return $res . "." . $curlang;
  242. }
  243. else {
  244. return $res;
  245. }
  246. }
  247. return "";
  248. } #}}}
  249. # We use filter to convert PO to the master page's type,
  250. # since other plugins should not work on PO files
  251. sub filter (@) { #{{{
  252. my %params = @_;
  253. my $page = $params{page};
  254. my $destpage = $params{destpage};
  255. my $content = decode_utf8(encode_utf8($params{content}));
  256. # decide if this is a PO file that should be converted into a translated document,
  257. # and perform various sanity checks
  258. if (! istranslation($page) || $filtered{$page}{$destpage}) {
  259. return $content;
  260. }
  261. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  262. my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
  263. my $masterfile = srcfile($pagesources{$masterpage});
  264. my (@pos,@masters);
  265. push @pos,$file;
  266. push @masters,$masterfile;
  267. my %options = (
  268. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  269. );
  270. my $doc=Locale::Po4a::Chooser::new('text',%options);
  271. $doc->process(
  272. 'po_in_name' => \@pos,
  273. 'file_in_name' => \@masters,
  274. 'file_in_charset' => 'utf-8',
  275. 'file_out_charset' => 'utf-8',
  276. ) or error("[po/filter:$file]: failed to translate");
  277. my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
  278. my $tmpout = $tmpfh->filename;
  279. $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
  280. $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
  281. $filtered{$page}{$destpage}=1;
  282. return $content;
  283. } #}}}
  284. sub htmlize (@) { #{{{
  285. my %params=@_;
  286. my $page = $params{page};
  287. my $content = $params{content};
  288. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  289. my $masterfile = srcfile($pagesources{$masterpage});
  290. # force content to be htmlize'd as if it was the same type as the master page
  291. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  292. } #}}}
  293. sub percenttranslated ($) { #{{{
  294. my $page=shift;
  295. return "N/A" unless (istranslation($page));
  296. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  297. my $file=srcfile($pagesources{$page});
  298. my $masterfile = srcfile($pagesources{$masterpage});
  299. my (@pos,@masters);
  300. push @pos,$file;
  301. push @masters,$masterfile;
  302. my %options = (
  303. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  304. );
  305. my $doc=Locale::Po4a::Chooser::new('text',%options);
  306. $doc->process(
  307. 'po_in_name' => \@pos,
  308. 'file_in_name' => \@masters,
  309. 'file_in_charset' => 'utf-8',
  310. 'file_out_charset' => 'utf-8',
  311. ) or error("[po/percenttranslated:$file]: failed to translate");
  312. my ($percent,$hit,$queries) = $doc->stats();
  313. return $percent;
  314. } #}}}
  315. sub otherlanguages ($) { #{{{
  316. my $page=shift;
  317. my @ret;
  318. if (istranslatable($page)) {
  319. foreach my $lang (sort keys %{$translations{$page}}) {
  320. my $translation = $translations{$page}{$lang};
  321. push @ret, {
  322. url => urlto($translation, $page),
  323. code => $lang,
  324. language => $config{po_slave_languages}{$lang},
  325. percent => percenttranslated($translation),
  326. };
  327. }
  328. }
  329. elsif (istranslation($page)) {
  330. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  331. push @ret, {
  332. url => urlto($masterpage, $page),
  333. code => $config{po_master_language}{code},
  334. language => $config{po_master_language}{name},
  335. master => 1,
  336. };
  337. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  338. push @ret, {
  339. url => urlto($translations{$masterpage}{$lang}, $page),
  340. code => $lang,
  341. language => $config{po_slave_languages}{$lang},
  342. percent => percenttranslated($translations{$masterpage}{$lang}),
  343. } unless ($lang eq $curlang);
  344. }
  345. }
  346. return @ret;
  347. } #}}}
  348. sub pagetemplate (@) { #{{{
  349. my %params=@_;
  350. my $page=$params{page};
  351. my $template=$params{template};
  352. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  353. $template->param(percenttranslated => percenttranslated($page));
  354. }
  355. if ($template->query(name => "istranslation")) {
  356. $template->param(istranslation => istranslation($page));
  357. }
  358. if ($template->query(name => "istranslatable")) {
  359. $template->param(istranslatable => istranslatable($page));
  360. }
  361. if ($template->query(name => "otherlanguages")) {
  362. $template->param(otherlanguages => [otherlanguages($page)]);
  363. if (istranslatable($page)) {
  364. foreach my $translation (values %{$translations{$page}}) {
  365. add_depends($page, $translation);
  366. }
  367. }
  368. elsif (istranslation($page)) {
  369. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  370. add_depends($page, $masterpage);
  371. foreach my $translation (values %{$translations{$masterpage}}) {
  372. add_depends($page, $translation);
  373. }
  374. }
  375. }
  376. } # }}}
  377. sub istranslatable ($) { #{{{
  378. my $page=shift;
  379. my $file=$pagesources{$page};
  380. if (! defined $file
  381. || (defined pagetype($file) && pagetype($file) eq 'po')
  382. || $file =~ /\.pot$/) {
  383. return 0;
  384. }
  385. return pagespec_match($page, $config{po_translatable_pages});
  386. } #}}}
  387. sub _istranslation ($) { #{{{
  388. my $page=shift;
  389. my $file=$pagesources{$page};
  390. if (! defined $file) {
  391. return IkiWiki::FailReason->new("no file specified");
  392. }
  393. if (! defined $file
  394. || ! defined pagetype($file)
  395. || ! pagetype($file) eq 'po'
  396. || $file =~ /\.pot$/) {
  397. return 0;
  398. }
  399. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  400. if (! defined $masterpage || ! defined $lang
  401. || ! (length($masterpage) > 0) || ! (length($lang) > 0)
  402. || ! defined $pagesources{$masterpage}
  403. || ! defined $config{po_slave_languages}{$lang}) {
  404. return 0;
  405. }
  406. return istranslatable($masterpage);
  407. } #}}}
  408. sub istranslation ($) { #{{{
  409. my $page=shift;
  410. if (_istranslation($page)) {
  411. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  412. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  413. return 1;
  414. }
  415. return 0;
  416. } #}}}
  417. package IkiWiki::PageSpec;
  418. use warnings;
  419. use strict;
  420. use IkiWiki 2.00;
  421. sub match_istranslation ($;@) { #{{{
  422. my $page=shift;
  423. if (IkiWiki::Plugin::po::istranslation($page)) {
  424. return IkiWiki::SuccessReason->new("is a translation page");
  425. }
  426. else {
  427. return IkiWiki::FailReason->new("is not a translation page");
  428. }
  429. } #}}}
  430. sub match_istranslatable ($;@) { #{{{
  431. my $page=shift;
  432. if (IkiWiki::Plugin::po::istranslatable($page)) {
  433. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  434. }
  435. else {
  436. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  437. }
  438. } #}}}
  439. sub match_lang ($$;@) { #{{{
  440. my $page=shift;
  441. my $wanted=shift;
  442. my $regexp=IkiWiki::glob2re($wanted);
  443. my $lang;
  444. my $masterpage;
  445. if (IkiWiki::Plugin::po::istranslation($page)) {
  446. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  447. }
  448. else {
  449. $lang = $config{po_master_language}{code};
  450. }
  451. if ($lang!~/^$regexp$/i) {
  452. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  453. }
  454. else {
  455. return IkiWiki::SuccessReason->new("file language is $wanted");
  456. }
  457. } #}}}
  458. sub match_currentlang ($$;@) { #{{{
  459. my $page=shift;
  460. shift;
  461. my %params=@_;
  462. my ($currentmasterpage, $currentlang, $masterpage, $lang);
  463. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  464. if (IkiWiki::Plugin::po::istranslation($params{location})) {
  465. ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
  466. }
  467. else {
  468. $currentlang = $config{po_master_language}{code};
  469. }
  470. if (IkiWiki::Plugin::po::istranslation($page)) {
  471. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  472. }
  473. else {
  474. $lang = $config{po_master_language}{code};
  475. }
  476. if ($lang eq $currentlang) {
  477. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  478. }
  479. else {
  480. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  481. }
  482. } #}}}
  483. 1