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