summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 26bd893a91d6e20abacd1ebb22ffd68868f385b3 (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 => 1,
  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 => 1,
  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 => 1,
  66. rebuild => 1,
  67. },
  68. po_link_to => {
  69. type => "string",
  70. example => "current",
  71. description => "internal linking behavior (default/current/negotiated)",
  72. safe => 1,
  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 => "otherlanguages")) {
  345. $template->param(otherlanguages => [otherlanguages($page)]);
  346. if (istranslatable($page)) {
  347. foreach my $translation (values %{$translations{$page}}) {
  348. add_depends($page, $translation);
  349. }
  350. }
  351. elsif (istranslation($page)) {
  352. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  353. add_depends($page, $masterpage);
  354. foreach my $translation (values %{$translations{$masterpage}}) {
  355. add_depends($page, $translation);
  356. }
  357. }
  358. }
  359. } # }}}
  360. sub istranslatable ($) { #{{{
  361. my $page=shift;
  362. my $file=$pagesources{$page};
  363. if (! defined $file
  364. || (defined pagetype($file) && pagetype($file) eq 'po')
  365. || $file =~ /\.pot$/) {
  366. return 0;
  367. }
  368. return pagespec_match($page, $config{po_translatable_pages});
  369. } #}}}
  370. sub _istranslation ($) { #{{{
  371. my $page=shift;
  372. my $file=$pagesources{$page};
  373. if (! defined $file) {
  374. return IkiWiki::FailReason->new("no file specified");
  375. }
  376. if (! defined $file
  377. || ! defined pagetype($file)
  378. || ! pagetype($file) eq 'po'
  379. || $file =~ /\.pot$/) {
  380. return 0;
  381. }
  382. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  383. if (! defined $masterpage || ! defined $lang
  384. || ! (length($masterpage) > 0) || ! (length($lang) > 0)
  385. || ! defined $pagesources{$masterpage}
  386. || ! defined $config{po_slave_languages}{$lang}) {
  387. return 0;
  388. }
  389. return istranslatable($masterpage);
  390. } #}}}
  391. sub istranslation ($) { #{{{
  392. my $page=shift;
  393. if (_istranslation($page)) {
  394. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  395. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  396. return 1;
  397. }
  398. return 0;
  399. } #}}}
  400. package IkiWiki::PageSpec;
  401. use warnings;
  402. use strict;
  403. use IkiWiki 2.00;
  404. sub match_istranslation ($;@) { #{{{
  405. my $page=shift;
  406. if (IkiWiki::Plugin::po::istranslation($page)) {
  407. return IkiWiki::SuccessReason->new("is a translation page");
  408. }
  409. else {
  410. return IkiWiki::FailReason->new("is not a translation page");
  411. }
  412. } #}}}
  413. sub match_istranslatable ($;@) { #{{{
  414. my $page=shift;
  415. if (IkiWiki::Plugin::po::istranslatable($page)) {
  416. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  417. }
  418. else {
  419. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  420. }
  421. } #}}}
  422. sub match_lang ($$;@) { #{{{
  423. my $page=shift;
  424. my $wanted=shift;
  425. my $regexp=IkiWiki::glob2re($wanted);
  426. my $lang;
  427. my $masterpage;
  428. if (IkiWiki::Plugin::po::istranslation($page)) {
  429. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  430. }
  431. else {
  432. $lang = $config{po_master_language}{code};
  433. }
  434. if ($lang!~/^$regexp$/i) {
  435. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  436. }
  437. else {
  438. return IkiWiki::SuccessReason->new("file language is $wanted");
  439. }
  440. } #}}}
  441. sub match_currentlang ($$;@) { #{{{
  442. my $page=shift;
  443. shift;
  444. my %params=@_;
  445. my ($currentmasterpage, $currentlang, $masterpage, $lang);
  446. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  447. if (IkiWiki::Plugin::po::istranslation($params{location})) {
  448. ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
  449. }
  450. else {
  451. $currentlang = $config{po_master_language}{code};
  452. }
  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 eq $currentlang) {
  460. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  461. }
  462. else {
  463. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  464. }
  465. } #}}}
  466. 1