summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 0902322e31be9fbe5d41dba47fe75e3384f37e48 (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} || $masterpage 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 urlto_with_orig_beautiful_urlpath($$) { #{{{
  233. my $to=shift;
  234. my $from=shift;
  235. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
  236. my $res=urlto($to, $from);
  237. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  238. return $res;
  239. } #}}}
  240. sub mybestlink ($$) { #{{{
  241. my $page=shift;
  242. my $link=shift;
  243. my $res=$origsubs{'bestlink'}->($page, $link);
  244. if (length $res) {
  245. if ($config{po_link_to} eq "current"
  246. && istranslatable($res)
  247. && istranslation($page)) {
  248. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  249. return $res . "." . $curlang;
  250. }
  251. else {
  252. return $res;
  253. }
  254. }
  255. return "";
  256. } #}}}
  257. # We use filter to convert PO to the master page's type,
  258. # since other plugins should not work on PO files
  259. sub filter (@) { #{{{
  260. my %params = @_;
  261. my $page = $params{page};
  262. my $destpage = $params{destpage};
  263. my $content = decode_utf8(encode_utf8($params{content}));
  264. # decide if this is a PO file that should be converted into a translated document,
  265. # and perform various sanity checks
  266. if (! istranslation($page) || $filtered{$page}{$destpage}) {
  267. return $content;
  268. }
  269. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  270. my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
  271. my $masterfile = srcfile($pagesources{$masterpage});
  272. my (@pos,@masters);
  273. push @pos,$file;
  274. push @masters,$masterfile;
  275. my %options = (
  276. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  277. );
  278. my $doc=Locale::Po4a::Chooser::new('text',%options);
  279. $doc->process(
  280. 'po_in_name' => \@pos,
  281. 'file_in_name' => \@masters,
  282. 'file_in_charset' => 'utf-8',
  283. 'file_out_charset' => 'utf-8',
  284. ) or error("[po/filter:$file]: failed to translate");
  285. my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
  286. my $tmpout = $tmpfh->filename;
  287. $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
  288. $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
  289. $filtered{$page}{$destpage}=1;
  290. return $content;
  291. } #}}}
  292. sub htmlize (@) { #{{{
  293. my %params=@_;
  294. my $page = $params{page};
  295. my $content = $params{content};
  296. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  297. my $masterfile = srcfile($pagesources{$masterpage});
  298. # force content to be htmlize'd as if it was the same type as the master page
  299. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  300. } #}}}
  301. sub percenttranslated ($) { #{{{
  302. my $page=shift;
  303. return "N/A" unless (istranslation($page));
  304. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  305. my $file=srcfile($pagesources{$page});
  306. my $masterfile = srcfile($pagesources{$masterpage});
  307. my (@pos,@masters);
  308. push @pos,$file;
  309. push @masters,$masterfile;
  310. my %options = (
  311. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  312. );
  313. my $doc=Locale::Po4a::Chooser::new('text',%options);
  314. $doc->process(
  315. 'po_in_name' => \@pos,
  316. 'file_in_name' => \@masters,
  317. 'file_in_charset' => 'utf-8',
  318. 'file_out_charset' => 'utf-8',
  319. ) or error("[po/percenttranslated:$file]: failed to translate");
  320. my ($percent,$hit,$queries) = $doc->stats();
  321. return $percent;
  322. } #}}}
  323. sub otherlanguages ($) { #{{{
  324. my $page=shift;
  325. my @ret;
  326. if (istranslatable($page)) {
  327. foreach my $lang (sort keys %{$translations{$page}}) {
  328. my $translation = $translations{$page}{$lang};
  329. push @ret, {
  330. url => urlto($translation, $page),
  331. code => $lang,
  332. language => $config{po_slave_languages}{$lang},
  333. percent => percenttranslated($translation),
  334. };
  335. }
  336. }
  337. elsif (istranslation($page)) {
  338. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  339. push @ret, {
  340. url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
  341. code => $config{po_master_language}{code},
  342. language => $config{po_master_language}{name},
  343. master => 1,
  344. };
  345. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  346. push @ret, {
  347. url => urlto($translations{$masterpage}{$lang}, $page),
  348. code => $lang,
  349. language => $config{po_slave_languages}{$lang},
  350. percent => percenttranslated($translations{$masterpage}{$lang}),
  351. } unless ($lang eq $curlang);
  352. }
  353. }
  354. return @ret;
  355. } #}}}
  356. sub pagetemplate (@) { #{{{
  357. my %params=@_;
  358. my $page=$params{page};
  359. my $template=$params{template};
  360. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  361. $template->param(percenttranslated => percenttranslated($page));
  362. }
  363. if ($template->query(name => "istranslation")) {
  364. $template->param(istranslation => istranslation($page));
  365. }
  366. if ($template->query(name => "istranslatable")) {
  367. $template->param(istranslatable => istranslatable($page));
  368. }
  369. if ($template->query(name => "otherlanguages")) {
  370. $template->param(otherlanguages => [otherlanguages($page)]);
  371. if (istranslatable($page)) {
  372. foreach my $translation (values %{$translations{$page}}) {
  373. add_depends($page, $translation);
  374. }
  375. }
  376. elsif (istranslation($page)) {
  377. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  378. add_depends($page, $masterpage);
  379. foreach my $translation (values %{$translations{$masterpage}}) {
  380. add_depends($page, $translation);
  381. }
  382. }
  383. }
  384. } # }}}
  385. sub istranslatable ($) { #{{{
  386. my $page=shift;
  387. my $file=$pagesources{$page};
  388. if (! defined $file
  389. || (defined pagetype($file) && pagetype($file) eq 'po')
  390. || $file =~ /\.pot$/) {
  391. return 0;
  392. }
  393. return pagespec_match($page, $config{po_translatable_pages});
  394. } #}}}
  395. sub _istranslation ($) { #{{{
  396. my $page=shift;
  397. my $file=$pagesources{$page};
  398. if (! defined $file) {
  399. return IkiWiki::FailReason->new("no file specified");
  400. }
  401. if (! defined $file
  402. || ! defined pagetype($file)
  403. || ! pagetype($file) eq 'po'
  404. || $file =~ /\.pot$/) {
  405. return 0;
  406. }
  407. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  408. if (! defined $masterpage || ! defined $lang
  409. || ! (length($masterpage) > 0) || ! (length($lang) > 0)
  410. || ! defined $pagesources{$masterpage}
  411. || ! defined $config{po_slave_languages}{$lang}) {
  412. return 0;
  413. }
  414. return istranslatable($masterpage);
  415. } #}}}
  416. sub istranslation ($) { #{{{
  417. my $page=shift;
  418. if (_istranslation($page)) {
  419. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  420. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  421. return 1;
  422. }
  423. return 0;
  424. } #}}}
  425. package IkiWiki::PageSpec;
  426. use warnings;
  427. use strict;
  428. use IkiWiki 2.00;
  429. sub match_istranslation ($;@) { #{{{
  430. my $page=shift;
  431. if (IkiWiki::Plugin::po::istranslation($page)) {
  432. return IkiWiki::SuccessReason->new("is a translation page");
  433. }
  434. else {
  435. return IkiWiki::FailReason->new("is not a translation page");
  436. }
  437. } #}}}
  438. sub match_istranslatable ($;@) { #{{{
  439. my $page=shift;
  440. if (IkiWiki::Plugin::po::istranslatable($page)) {
  441. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  442. }
  443. else {
  444. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  445. }
  446. } #}}}
  447. sub match_lang ($$;@) { #{{{
  448. my $page=shift;
  449. my $wanted=shift;
  450. my $regexp=IkiWiki::glob2re($wanted);
  451. my $lang;
  452. my $masterpage;
  453. if (IkiWiki::Plugin::po::istranslation($page)) {
  454. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  455. }
  456. else {
  457. $lang = $config{po_master_language}{code};
  458. }
  459. if ($lang!~/^$regexp$/i) {
  460. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  461. }
  462. else {
  463. return IkiWiki::SuccessReason->new("file language is $wanted");
  464. }
  465. } #}}}
  466. sub match_currentlang ($$;@) { #{{{
  467. my $page=shift;
  468. shift;
  469. my %params=@_;
  470. my ($currentmasterpage, $currentlang, $masterpage, $lang);
  471. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  472. if (IkiWiki::Plugin::po::istranslation($params{location})) {
  473. ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
  474. }
  475. else {
  476. $currentlang = $config{po_master_language}{code};
  477. }
  478. if (IkiWiki::Plugin::po::istranslation($page)) {
  479. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  480. }
  481. else {
  482. $lang = $config{po_master_language}{code};
  483. }
  484. if ($lang eq $currentlang) {
  485. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  486. }
  487. else {
  488. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  489. }
  490. } #}}}
  491. 1