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