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