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;
11 use Locale::Po4a::Chooser;
22 ## FIXME: makes some test cases cry once every two tries; this may be
23 ## related to the artificial way the testsuite is run, or not.
24 # memoize("istranslatable");
25 memoize("_istranslation");
26 memoize("percenttranslated");
28 # backup references to subs that will be overriden
30 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
31 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
32 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
35 hook(type => "getsetup", id => "po", call => \&getsetup);
36 hook(type => "checkconfig", id => "po", call => \&checkconfig);
37 hook(type => "needsbuild", id => "po", call => \&needsbuild);
38 hook(type => "filter", id => "po", call => \&filter);
39 hook(type => "htmlize", id => "po", call => \&htmlize);
40 hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
41 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
42 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
43 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
46 sub getsetup () { #{{{
50 rebuild => 1, # format plugin
52 po_master_language => {
58 description => "master language (non-PO files)",
62 po_slave_languages => {
69 description => "slave languages (PO files)",
73 po_translatable_pages => {
75 example => "!*/Discussion",
76 description => "PageSpec controlling which pages are translatable",
77 link => "ikiwiki/PageSpec",
84 description => "internal linking behavior (default/current/negotiated)",
90 sub checkconfig () { #{{{
91 foreach my $field (qw{po_master_language po_slave_languages}) {
92 if (! exists $config{$field} || ! defined $config{$field}) {
93 error(sprintf(gettext("Must specify %s"), $field));
96 if (! exists $config{po_link_to} ||
97 ! defined $config{po_link_to}) {
98 $config{po_link_to}="default";
100 if (! exists $config{po_translatable_pages} ||
101 ! defined $config{po_translatable_pages}) {
102 $config{po_translatable_pages}="";
104 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
105 error(gettext("po_link_to=negotiated requires usedirs to be set"));
107 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
110 sub potfile ($) { #{{{
111 my $masterfile=shift;
112 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
113 return File::Spec->catfile($dir, $name . ".pot");
116 sub pofile ($$) { #{{{
117 my $masterfile=shift;
119 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
120 return File::Spec->catfile($dir, $name . "." . $lang . ".po");
123 sub refreshpot ($) { #{{{
124 my $masterfile=shift;
125 my $potfile=potfile($masterfile);
126 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
127 my $doc=Locale::Po4a::Chooser::new('text',%options);
128 $doc->read($masterfile);
129 $doc->{TT}{utf_mode} = 1;
130 $doc->{TT}{file_in_charset} = 'utf-8';
131 $doc->{TT}{file_out_charset} = 'utf-8';
132 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
133 # this is undocument use of internal Locale::Po4a::TransTractor's data,
134 # compulsory since this module prevents us from using the porefs option.
135 my %po_options = ('porefs' => 'none');
136 $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
137 $doc->{TT}{po_out}->set_charset('utf-8');
140 $doc->writepo($potfile);
143 sub refreshpofiles ($@) { #{{{
144 my $masterfile=shift;
147 my $potfile=potfile($masterfile);
148 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
150 foreach my $pofile (@pofiles) {
152 my $cmd = "msgmerge -U --backup=none $pofile $potfile";
154 or error("[po/refreshpofiles:$pofile] failed to update");
157 File::Copy::syscopy($potfile,$pofile)
158 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
163 sub needsbuild () { #{{{
164 my $needsbuild=shift;
166 # build %translations, using istranslation's side-effect
167 foreach my $page (keys %pagesources) {
168 istranslation($page);
171 # refresh/create POT and PO files as needed
172 my $updated_po_files=0;
173 foreach my $page (keys %pagesources) {
174 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
175 if (istranslatable($page)) {
176 my $file=srcfile($pagesources{$page});
177 if ($pageneedsbuild || ! -e potfile($file)) {
181 foreach my $lang (keys %{$config{po_slave_languages}}) {
182 my $pofile=pofile($file, $lang);
183 if ($pageneedsbuild || ! -e $pofile) {
184 push @pofiles, $pofile;
188 refreshpofiles($file, @pofiles) ;
189 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
190 $updated_po_files = 1;
195 # check staged changes in and trigger a wiki refresh.
196 if ($updated_po_files) {
198 IkiWiki::disable_commit_hook();
199 IkiWiki::rcs_commit_staged(gettext("updated PO files"),
200 "refreshpofiles", "127.0.0.1");
201 IkiWiki::enable_commit_hook();
202 IkiWiki::rcs_update();
205 IkiWiki::saveindex();
206 # refresh module's private variables
209 foreach my $page (keys %pagesources) {
210 istranslation($page);
215 # make existing translations depend on the corresponding master page
216 foreach my $master (keys %translations) {
217 foreach my $slave (values %{$translations{$master}}) {
218 add_depends($slave, $master);
223 sub mytargetpage ($$) { #{{{
227 if (istranslation($page)) {
228 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
229 if (! $config{usedirs} || $masterpage eq 'index') {
230 return $masterpage . "." . $lang . "." . $ext;
233 return $masterpage . "/index." . $lang . "." . $ext;
236 elsif (istranslatable($page)) {
237 if (! $config{usedirs} || $page eq 'index') {
238 return $page . "." . $config{po_master_language}{code} . "." . $ext;
241 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
244 return $origsubs{'targetpage'}->($page, $ext);
247 sub mybeautify_urlpath ($) { #{{{
249 my $res=$origsubs{'beautify_urlpath'}->($url);
250 if ($config{po_link_to} eq "negotiated") {
251 $res =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
256 sub urlto_with_orig_beautiful_urlpath($$) { #{{{
260 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
261 my $res=urlto($to, $from);
262 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
267 sub mybestlink ($$) { #{{{
270 my $res=$origsubs{'bestlink'}->($page, $link);
272 if ($config{po_link_to} eq "current"
273 && istranslatable($res)
274 && istranslation($page)) {
275 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
276 return $res . "." . $curlang;
285 # We use filter to convert PO to the master page's type,
286 # since other plugins should not work on PO files
287 sub filter (@) { #{{{
289 my $page = $params{page};
290 my $destpage = $params{destpage};
291 my $content = decode_utf8(encode_utf8($params{content}));
293 # decide if this is a PO file that should be converted into a translated document,
294 # and perform various sanity checks
295 if (! istranslation($page) || $filtered{$page}{$destpage}) {
299 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
300 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
301 my $masterfile = srcfile($pagesources{$masterpage});
304 push @masters,$masterfile;
306 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
308 my $doc=Locale::Po4a::Chooser::new('text',%options);
310 'po_in_name' => \@pos,
311 'file_in_name' => \@masters,
312 'file_in_charset' => 'utf-8',
313 'file_out_charset' => 'utf-8',
314 ) or error("[po/filter:$file]: failed to translate");
315 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
316 my $tmpout = $tmpfh->filename;
317 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
318 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
319 $filtered{$page}{$destpage}=1;
323 sub htmlize (@) { #{{{
325 my $page = $params{page};
326 my $content = $params{content};
327 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
328 my $masterfile = srcfile($pagesources{$masterpage});
330 # force content to be htmlize'd as if it was the same type as the master page
331 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
334 sub percenttranslated ($) { #{{{
336 return "N/A" unless (istranslation($page));
337 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
338 my $file=srcfile($pagesources{$page});
339 my $masterfile = srcfile($pagesources{$masterpage});
342 push @masters,$masterfile;
344 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
346 my $doc=Locale::Po4a::Chooser::new('text',%options);
348 'po_in_name' => \@pos,
349 'file_in_name' => \@masters,
350 'file_in_charset' => 'utf-8',
351 'file_out_charset' => 'utf-8',
352 ) or error("[po/percenttranslated:$file]: failed to translate");
353 my ($percent,$hit,$queries) = $doc->stats();
357 sub otherlanguages ($) { #{{{
360 if (istranslatable($page)) {
361 foreach my $lang (sort keys %{$translations{$page}}) {
362 my $translation = $translations{$page}{$lang};
364 url => urlto($translation, $page),
366 language => $config{po_slave_languages}{$lang},
367 percent => percenttranslated($translation),
371 elsif (istranslation($page)) {
372 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
374 url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
375 code => $config{po_master_language}{code},
376 language => $config{po_master_language}{name},
379 foreach my $lang (sort keys %{$translations{$masterpage}}) {
381 url => urlto($translations{$masterpage}{$lang}, $page),
383 language => $config{po_slave_languages}{$lang},
384 percent => percenttranslated($translations{$masterpage}{$lang}),
385 } unless ($lang eq $curlang);
391 sub pagetemplate (@) { #{{{
393 my $page=$params{page};
394 my $destpage=$params{destpage};
395 my $template=$params{template};
397 if (istranslation($page) && $template->query(name => "percenttranslated")) {
398 $template->param(percenttranslated => percenttranslated($page));
400 if ($template->query(name => "istranslation")) {
401 $template->param(istranslation => istranslation($page));
403 if ($template->query(name => "istranslatable")) {
404 $template->param(istranslatable => istranslatable($page));
406 if ($template->query(name => "otherlanguages")) {
407 $template->param(otherlanguages => [otherlanguages($page)]);
408 if (istranslatable($page)) {
409 foreach my $translation (values %{$translations{$page}}) {
410 add_depends($page, $translation);
413 elsif (istranslation($page)) {
414 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
415 add_depends($page, $masterpage);
416 foreach my $translation (values %{$translations{$masterpage}}) {
417 add_depends($page, $translation);
421 # Rely on IkiWiki::Render's genpage() to decide wether
422 # a discussion link should appear on $page; this is not
423 # totally accurate, though: some broken links may be generated
424 # when cgiurl is disabled.
425 # This compromise avoids some code duplication, and will probably
426 # prevent future breakage when ikiwiki internals change.
427 # Known limitations are preferred to future random bugs.
428 if ($template->param('discussionlink') && istranslation($page)) {
429 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
430 $template->param('discussionlink' => htmllink(
433 $masterpage . '/' . gettext("Discussion"),
436 linktext => gettext("Discussion"),
441 sub istranslatable ($) { #{{{
443 my $file=$pagesources{$page};
446 || (defined pagetype($file) && pagetype($file) eq 'po')
447 || $file =~ /\.pot$/) {
450 return pagespec_match($page, $config{po_translatable_pages});
453 sub _istranslation ($) { #{{{
455 my $file=$pagesources{$page};
456 if (! defined $file) {
457 return IkiWiki::FailReason->new("no file specified");
461 || ! defined pagetype($file)
462 || ! pagetype($file) eq 'po'
463 || $file =~ /\.pot$/) {
467 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
468 if (! defined $masterpage || ! defined $lang
469 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
470 || ! defined $pagesources{$masterpage}
471 || ! defined $config{po_slave_languages}{$lang}) {
475 return istranslatable($masterpage);
478 sub istranslation ($) { #{{{
480 if (_istranslation($page)) {
481 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
482 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
488 package IkiWiki::PageSpec;
493 sub match_istranslation ($;@) { #{{{
495 if (IkiWiki::Plugin::po::istranslation($page)) {
496 return IkiWiki::SuccessReason->new("is a translation page");
499 return IkiWiki::FailReason->new("is not a translation page");
503 sub match_istranslatable ($;@) { #{{{
505 if (IkiWiki::Plugin::po::istranslatable($page)) {
506 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
509 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
513 sub match_lang ($$;@) { #{{{
516 my $regexp=IkiWiki::glob2re($wanted);
520 if (IkiWiki::Plugin::po::istranslation($page)) {
521 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
524 $lang = $config{po_master_language}{code};
527 if ($lang!~/^$regexp$/i) {
528 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
531 return IkiWiki::SuccessReason->new("file language is $wanted");
535 sub match_currentlang ($$;@) { #{{{
539 my ($currentmasterpage, $currentlang, $masterpage, $lang);
541 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
543 if (IkiWiki::Plugin::po::istranslation($params{location})) {
544 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
547 $currentlang = $config{po_master_language}{code};
550 if (IkiWiki::Plugin::po::istranslation($page)) {
551 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
554 $lang = $config{po_master_language}{code};
557 if ($lang eq $currentlang) {
558 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
561 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");