2 # .po as a wiki page type
3 # Licensed under GPL v2 or greater
4 # Copyright (C) 2008 intrigeri <intrigeri@boum.org>
5 # inspired by the GPL'd po4a-translate,
6 # which is Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
7 package IkiWiki::Plugin::po;
13 use Locale::Po4a::Chooser;
24 memoize("_istranslation");
25 memoize("percenttranslated");
26 # FIXME: memoizing istranslatable() makes some test cases fail once every
27 # two tries; this may be related to the artificial way the testsuite is
29 # memoize("istranslatable");
31 # backup references to subs that will be overriden
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, last => 1);
41 hook(type => "editcontent", id => "po", call => \&editcontent);
43 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
44 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
45 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
46 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
47 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
48 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
51 sub getsetup () { #{{{
55 rebuild => 1, # format plugin & changes html filenames
57 po_master_language => {
63 description => "master language (non-PO files)",
67 po_slave_languages => {
74 description => "slave languages (PO files)",
78 po_translatable_pages => {
80 example => "!*/Discussion",
81 description => "PageSpec controlling which pages are translatable",
82 link => "ikiwiki/PageSpec",
89 description => "internal linking behavior (default/current/negotiated)",
95 sub checkconfig () { #{{{
96 foreach my $field (qw{po_master_language po_slave_languages}) {
97 if (! exists $config{$field} || ! defined $config{$field}) {
98 error(sprintf(gettext("Must specify %s"), $field));
101 if (! exists $config{po_link_to} ||
102 ! defined $config{po_link_to}) {
103 $config{po_link_to}="default";
105 if (! exists $config{po_translatable_pages} ||
106 ! defined $config{po_translatable_pages}) {
107 $config{po_translatable_pages}="";
109 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
110 error(gettext("po_link_to=negotiated requires usedirs to be set"));
112 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
115 sub potfile ($) { #{{{
116 my $masterfile=shift;
118 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
119 $dir='' if $dir eq './';
120 return File::Spec->catpath('', $dir, $name . ".pot");
123 sub pofile ($$) { #{{{
124 my $masterfile=shift;
127 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
128 $dir='' if $dir eq './';
129 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
132 sub refreshpot ($) { #{{{
133 my $masterfile=shift;
135 my $potfile=potfile($masterfile);
136 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
137 my $doc=Locale::Po4a::Chooser::new('text',%options);
138 $doc->read($masterfile);
139 $doc->{TT}{utf_mode} = 1;
140 $doc->{TT}{file_in_charset} = 'utf-8';
141 $doc->{TT}{file_out_charset} = 'utf-8';
142 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
143 # this is undocument use of internal Locale::Po4a::TransTractor's data,
144 # compulsory since this module prevents us from using the porefs option.
145 my %po_options = ('porefs' => 'none');
146 $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
147 $doc->{TT}{po_out}->set_charset('utf-8');
150 $doc->writepo($potfile);
153 sub refreshpofiles ($@) { #{{{
154 my $masterfile=shift;
157 my $potfile=potfile($masterfile);
158 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
160 foreach my $pofile (@pofiles) {
162 system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
163 or error("[po/refreshpofiles:$pofile] failed to update");
166 File::Copy::syscopy($potfile,$pofile)
167 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
172 sub needsbuild () { #{{{
173 my $needsbuild=shift;
175 # build %translations, using istranslation's side-effect
176 foreach my $page (keys %pagesources) {
177 istranslation($page);
180 # refresh/create POT and PO files as needed
181 my $updated_po_files=0;
182 foreach my $page (keys %pagesources) {
183 if (istranslatable($page)) {
184 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
185 my $updated_pot_file=0;
186 my $file=srcfile($pagesources{$page});
187 if ($pageneedsbuild || ! -e potfile($file)) {
192 foreach my $lang (keys %{$config{po_slave_languages}}) {
193 my $pofile=pofile($file, $lang);
194 my $pofile_rel=pofile($pagesources{$page}, $lang);
195 if ($pageneedsbuild || $updated_pot_file || ! -e $pofile) {
196 push @pofiles, $pofile;
197 push @$needsbuild, $pofile_rel
198 unless grep { $_ eq $pofile_rel } @$needsbuild;
202 refreshpofiles($file, @pofiles) ;
203 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
204 $updated_po_files = 1;
209 # check staged changes in
210 if ($updated_po_files) {
212 IkiWiki::disable_commit_hook();
213 IkiWiki::rcs_commit_staged(gettext("updated PO files"),
214 "refreshpofiles", "127.0.0.1");
215 IkiWiki::enable_commit_hook();
216 IkiWiki::rcs_update();
218 # refresh module's private variables
221 foreach my $page (keys %pagesources) {
222 istranslation($page);
226 # make existing translations depend on the corresponding master page
227 foreach my $master (keys %translations) {
228 foreach my $slave (values %{$translations{$master}}) {
229 add_depends($slave, $master);
234 sub mytargetpage ($$) { #{{{
238 if (istranslation($page)) {
239 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
240 if (! $config{usedirs} || $masterpage eq 'index') {
241 return $masterpage . "." . $lang . "." . $ext;
244 return $masterpage . "/index." . $lang . "." . $ext;
247 elsif (istranslatable($page)) {
248 if (! $config{usedirs} || $page eq 'index') {
249 return $page . "." . $config{po_master_language}{code} . "." . $ext;
252 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
255 return $origsubs{'targetpage'}->($page, $ext);
258 sub mybeautify_urlpath ($) { #{{{
261 my $res=$origsubs{'beautify_urlpath'}->($url);
262 if ($config{po_link_to} eq "negotiated") {
263 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
268 sub urlto_with_orig_beautiful_urlpath($$) { #{{{
272 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
273 my $res=urlto($to, $from);
274 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
279 sub mybestlink ($$) { #{{{
283 my $res=$origsubs{'bestlink'}->($page, $link);
285 if ($config{po_link_to} eq "current"
286 && istranslatable($res)
287 && istranslation($page)) {
288 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
289 return $res . "." . $curlang;
298 # We use filter to convert PO to the master page's format,
299 # since the rest of ikiwiki should not work on PO files.
300 sub filter (@) { #{{{
303 my $page = $params{page};
304 my $destpage = $params{destpage};
305 my $content = decode_utf8(encode_utf8($params{content}));
307 return $content if ( ! istranslation($page)
308 || ( exists $filtered{$page}{$destpage}
309 && $filtered{$page}{$destpage} eq 1 ));
311 # CRLF line terminators make poor Locale::Po4a feel bad
312 $content=~s/\r\n/\n/g;
314 # Implementation notes
316 # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
317 # to learn how to disguise a variable as a file.
318 # 2. There are incompatibilities between some File::Temp versions
319 # (including 0.18, bundled with Lenny's perl-modules package)
320 # and others (e.g. 0.20, previously present in the archive as
321 # a standalone package): under certain circumstances, some
322 # return a relative filename, whereas others return an absolute one;
323 # we here use this module in a way that is at least compatible
324 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
325 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
326 DIR => File::Spec->tmpdir,
327 UNLINK => 1)->filename;
328 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
329 DIR => File::Spec->tmpdir,
330 UNLINK => 1)->filename;
332 writefile(basename($infile), File::Spec->tmpdir, $content);
334 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
335 my $masterfile = srcfile($pagesources{$masterpage});
338 push @masters,$masterfile;
340 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
342 my $doc=Locale::Po4a::Chooser::new('text',%options);
344 'po_in_name' => \@pos,
345 'file_in_name' => \@masters,
346 'file_in_charset' => 'utf-8',
347 'file_out_charset' => 'utf-8',
348 ) or error("[po/filter:$infile]: failed to translate");
349 $doc->write($outfile) or error("[po/filter:$infile] could not write $outfile");
350 $content = readfile($outfile) or error("[po/filter:$infile] could not read $outfile");
352 # Unlinking should happen automatically, thanks to File::Temp,
353 # but it does not work here, probably because of the way writefile()
354 # and Locale::Po4a::write() work.
355 unlink $infile, $outfile;
357 $filtered{$page}{$destpage}=1;
361 sub htmlize (@) { #{{{
364 my $page = $params{page};
365 my $content = $params{content};
366 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
367 my $masterfile = srcfile($pagesources{$masterpage});
369 # force content to be htmlize'd as if it was the same type as the master page
370 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
373 sub percenttranslated ($) { #{{{
376 return gettext("N/A") unless (istranslation($page));
377 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
378 my $file=srcfile($pagesources{$page});
379 my $masterfile = srcfile($pagesources{$masterpage});
382 push @masters,$masterfile;
384 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
386 my $doc=Locale::Po4a::Chooser::new('text',%options);
388 'po_in_name' => \@pos,
389 'file_in_name' => \@masters,
390 'file_in_charset' => 'utf-8',
391 'file_out_charset' => 'utf-8',
392 ) or error("[po/percenttranslated:$file]: failed to translate");
393 my ($percent,$hit,$queries) = $doc->stats();
397 sub otherlanguages ($) { #{{{
401 if (istranslatable($page)) {
402 foreach my $lang (sort keys %{$translations{$page}}) {
403 my $translation = $translations{$page}{$lang};
405 url => urlto($translation, $page),
407 language => $config{po_slave_languages}{$lang},
408 percent => percenttranslated($translation),
412 elsif (istranslation($page)) {
413 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
415 url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
416 code => $config{po_master_language}{code},
417 language => $config{po_master_language}{name},
420 foreach my $lang (sort keys %{$translations{$masterpage}}) {
422 url => urlto($translations{$masterpage}{$lang}, $page),
424 language => $config{po_slave_languages}{$lang},
425 percent => percenttranslated($translations{$masterpage}{$lang}),
426 } unless ($lang eq $curlang);
432 sub pagetemplate (@) { #{{{
434 my $page=$params{page};
435 my $destpage=$params{destpage};
436 my $template=$params{template};
438 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
440 if (istranslation($page) && $template->query(name => "percenttranslated")) {
441 $template->param(percenttranslated => percenttranslated($page));
443 if ($template->query(name => "istranslation")) {
444 $template->param(istranslation => istranslation($page));
446 if ($template->query(name => "istranslatable")) {
447 $template->param(istranslatable => istranslatable($page));
449 if ($template->query(name => "otherlanguages")) {
450 $template->param(otherlanguages => [otherlanguages($page)]);
451 if (istranslatable($page)) {
452 foreach my $translation (values %{$translations{$page}}) {
453 add_depends($page, $translation);
456 elsif (istranslation($page)) {
457 add_depends($page, $masterpage);
458 foreach my $translation (values %{$translations{$masterpage}}) {
459 add_depends($page, $translation);
463 # Rely on IkiWiki::Render's genpage() to decide wether
464 # a discussion link should appear on $page; this is not
465 # totally accurate, though: some broken links may be generated
466 # when cgiurl is disabled.
467 # This compromise avoids some code duplication, and will probably
468 # prevent future breakage when ikiwiki internals change.
469 # Known limitations are preferred to future random bugs.
470 if ($template->param('discussionlink') && istranslation($page)) {
471 $template->param('discussionlink' => htmllink(
474 $masterpage . '/' . gettext("Discussion"),
477 linktext => gettext("Discussion"),
480 # remove broken parentlink to ./index.html on home page's translations
481 if ($template->param('parentlinks')
482 && istranslation($page)
483 && $masterpage eq "index") {
484 $template->param('parentlinks' => []);
488 sub editcontent () { #{{{
490 # as we're previewing or saving a page, the content may have
491 # changed, so tell the next filter() invocation it must not be lazy
492 if (exists $filtered{$params{page}}{$params{page}}) {
493 delete $filtered{$params{page}}{$params{page}};
495 return $params{content};
498 sub istranslatable ($) { #{{{
501 my $file=$pagesources{$page};
504 || (defined pagetype($file) && pagetype($file) eq 'po')
505 || $file =~ /\.pot$/) {
508 return pagespec_match($page, $config{po_translatable_pages});
511 sub _istranslation ($) { #{{{
514 my $file=$pagesources{$page};
515 if (! defined $file) {
516 return IkiWiki::FailReason->new("no file specified");
520 || ! defined pagetype($file)
521 || ! pagetype($file) eq 'po'
522 || $file =~ /\.pot$/) {
526 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
527 if (! defined $masterpage || ! defined $lang
528 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
529 || ! defined $pagesources{$masterpage}
530 || ! defined $config{po_slave_languages}{$lang}) {
534 return istranslatable($masterpage);
537 sub istranslation ($) { #{{{
540 if (_istranslation($page)) {
541 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
542 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
548 package IkiWiki::PageSpec;
553 sub match_istranslation ($;@) { #{{{
556 if (IkiWiki::Plugin::po::istranslation($page)) {
557 return IkiWiki::SuccessReason->new("is a translation page");
560 return IkiWiki::FailReason->new("is not a translation page");
564 sub match_istranslatable ($;@) { #{{{
567 if (IkiWiki::Plugin::po::istranslatable($page)) {
568 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
571 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
575 sub match_lang ($$;@) { #{{{
579 my $regexp=IkiWiki::glob2re($wanted);
583 if (IkiWiki::Plugin::po::istranslation($page)) {
584 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
587 $lang = $config{po_master_language}{code};
590 if ($lang!~/^$regexp$/i) {
591 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
594 return IkiWiki::SuccessReason->new("file language is $wanted");
598 sub match_currentlang ($$;@) { #{{{
603 my ($currentmasterpage, $currentlang, $masterpage, $lang);
605 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
607 if (IkiWiki::Plugin::po::istranslation($params{location})) {
608 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
611 $currentlang = $config{po_master_language}{code};
614 if (IkiWiki::Plugin::po::istranslation($page)) {
615 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
618 $lang = $config{po_master_language}{code};
621 if ($lang eq $currentlang) {
622 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
625 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");