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 memoize("_istranslation");
23 memoize("percenttranslated");
24 # FIXME: memoizing istranslatable() makes some test cases fail once every
25 # two tries; this may be related to the artificial way the testsuite is
27 # memoize("istranslatable");
29 # backup references to subs that will be overriden
33 hook(type => "getsetup", id => "po", call => \&getsetup);
34 hook(type => "checkconfig", id => "po", call => \&checkconfig);
35 hook(type => "needsbuild", id => "po", call => \&needsbuild);
36 hook(type => "filter", id => "po", call => \&filter);
37 hook(type => "htmlize", id => "po", call => \&htmlize);
38 hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
39 hook(type => "editcontent", id => "po", call => \&editcontent);
41 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
42 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
43 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
44 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
45 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
46 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
49 sub getsetup () { #{{{
53 rebuild => 1, # format plugin & changes html filenames
55 po_master_language => {
61 description => "master language (non-PO files)",
65 po_slave_languages => {
72 description => "slave languages (PO files)",
76 po_translatable_pages => {
78 example => "!*/Discussion",
79 description => "PageSpec controlling which pages are translatable",
80 link => "ikiwiki/PageSpec",
87 description => "internal linking behavior (default/current/negotiated)",
93 sub checkconfig () { #{{{
94 foreach my $field (qw{po_master_language po_slave_languages}) {
95 if (! exists $config{$field} || ! defined $config{$field}) {
96 error(sprintf(gettext("Must specify %s"), $field));
99 if (! exists $config{po_link_to} ||
100 ! defined $config{po_link_to}) {
101 $config{po_link_to}="default";
103 if (! exists $config{po_translatable_pages} ||
104 ! defined $config{po_translatable_pages}) {
105 $config{po_translatable_pages}="";
107 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
108 error(gettext("po_link_to=negotiated requires usedirs to be set"));
110 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
113 sub potfile ($) { #{{{
114 my $masterfile=shift;
116 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
117 $dir='' if $dir eq './';
118 return File::Spec->catpath('', $dir, $name . ".pot");
121 sub pofile ($$) { #{{{
122 my $masterfile=shift;
125 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
126 $dir='' if $dir eq './';
127 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
130 sub refreshpot ($) { #{{{
131 my $masterfile=shift;
133 my $potfile=potfile($masterfile);
134 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
135 my $doc=Locale::Po4a::Chooser::new('text',%options);
136 $doc->read($masterfile);
137 $doc->{TT}{utf_mode} = 1;
138 $doc->{TT}{file_in_charset} = 'utf-8';
139 $doc->{TT}{file_out_charset} = 'utf-8';
140 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
141 # this is undocument use of internal Locale::Po4a::TransTractor's data,
142 # compulsory since this module prevents us from using the porefs option.
143 my %po_options = ('porefs' => 'none');
144 $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
145 $doc->{TT}{po_out}->set_charset('utf-8');
148 $doc->writepo($potfile);
151 sub refreshpofiles ($@) { #{{{
152 my $masterfile=shift;
155 my $potfile=potfile($masterfile);
156 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
158 foreach my $pofile (@pofiles) {
160 system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
161 or error("[po/refreshpofiles:$pofile] failed to update");
164 File::Copy::syscopy($potfile,$pofile)
165 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
170 sub needsbuild () { #{{{
171 my $needsbuild=shift;
173 # build %translations, using istranslation's side-effect
174 foreach my $page (keys %pagesources) {
175 istranslation($page);
178 # refresh/create POT and PO files as needed
179 my $updated_po_files=0;
180 foreach my $page (keys %pagesources) {
181 if (istranslatable($page)) {
182 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
183 my $updated_pot_file=0;
184 my $file=srcfile($pagesources{$page});
185 if ($pageneedsbuild || ! -e potfile($file)) {
190 foreach my $lang (keys %{$config{po_slave_languages}}) {
191 my $pofile=pofile($file, $lang);
192 my $pofile_rel=pofile($pagesources{$page}, $lang);
193 if ($pageneedsbuild || $updated_pot_file || ! -e $pofile) {
194 push @pofiles, $pofile;
195 push @$needsbuild, $pofile_rel
196 unless grep { $_ eq $pofile_rel } @$needsbuild;
200 refreshpofiles($file, @pofiles) ;
201 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
202 $updated_po_files = 1;
207 # check staged changes in
208 if ($updated_po_files) {
210 IkiWiki::disable_commit_hook();
211 IkiWiki::rcs_commit_staged(gettext("updated PO files"),
212 "refreshpofiles", "127.0.0.1");
213 IkiWiki::enable_commit_hook();
214 IkiWiki::rcs_update();
216 # refresh module's private variables
219 foreach my $page (keys %pagesources) {
220 istranslation($page);
224 # make existing translations depend on the corresponding master page
225 foreach my $master (keys %translations) {
226 foreach my $slave (values %{$translations{$master}}) {
227 add_depends($slave, $master);
232 sub mytargetpage ($$) { #{{{
236 if (istranslation($page)) {
237 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
238 if (! $config{usedirs} || $masterpage eq 'index') {
239 return $masterpage . "." . $lang . "." . $ext;
242 return $masterpage . "/index." . $lang . "." . $ext;
245 elsif (istranslatable($page)) {
246 if (! $config{usedirs} || $page eq 'index') {
247 return $page . "." . $config{po_master_language}{code} . "." . $ext;
250 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
253 return $origsubs{'targetpage'}->($page, $ext);
256 sub mybeautify_urlpath ($) { #{{{
259 my $res=$origsubs{'beautify_urlpath'}->($url);
260 if ($config{po_link_to} eq "negotiated") {
261 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
266 sub urlto_with_orig_beautiful_urlpath($$) { #{{{
270 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
271 my $res=urlto($to, $from);
272 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
277 sub mybestlink ($$) { #{{{
281 my $res=$origsubs{'bestlink'}->($page, $link);
283 if ($config{po_link_to} eq "current"
284 && istranslatable($res)
285 && istranslation($page)) {
286 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
287 return $res . "." . $curlang;
296 # We use filter to convert PO to the master page's format,
297 # since the rest of ikiwiki should not work on PO files.
298 sub filter (@) { #{{{
301 my $page = $params{page};
302 my $destpage = $params{destpage};
303 my $content = decode_utf8(encode_utf8($params{content}));
305 return $content if ( ! istranslation($page)
306 || ( exists $filtered{$page}{$destpage}
307 && $filtered{$page}{$destpage} eq 1 ));
309 # CRLF line terminators make poor Locale::Po4a feel bad
310 $content=~s/\r\n/\n/g;
312 # Implementation notes
314 # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
315 # to learn how to disguise a variable as a file.
316 # 2. There are incompatibilities between some File::Temp versions
317 # (including 0.18, bundled with Lenny's perl-modules package)
318 # and others (e.g. 0.20, previously present in the archive as
319 # a standalone package): under certain circumstances, some
320 # return a relative filename, whereas others return an absolute one;
321 # we here use this module in a way that is at least compatible
322 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
323 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
324 DIR => File::Spec->tmpdir,
325 UNLINK => 1)->filename;
326 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
327 DIR => File::Spec->tmpdir,
328 UNLINK => 1)->filename;
330 writefile(basename($infile), File::Spec->tmpdir, $content);
332 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
333 my $masterfile = srcfile($pagesources{$masterpage});
336 push @masters,$masterfile;
338 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
340 my $doc=Locale::Po4a::Chooser::new('text',%options);
342 'po_in_name' => \@pos,
343 'file_in_name' => \@masters,
344 'file_in_charset' => 'utf-8',
345 'file_out_charset' => 'utf-8',
346 ) or error("[po/filter:$infile]: failed to translate");
347 $doc->write($outfile) or error("[po/filter:$infile] could not write $outfile");
348 $content = readfile($outfile) or error("[po/filter:$infile] could not read $outfile");
350 # Unlinking should happen automatically, thanks to File::Temp,
351 # but it does not work here, probably because of the way writefile()
352 # and Locale::Po4a::write() work.
353 unlink $infile, $outfile;
355 $filtered{$page}{$destpage}=1;
359 sub htmlize (@) { #{{{
362 my $page = $params{page};
363 my $content = $params{content};
364 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
365 my $masterfile = srcfile($pagesources{$masterpage});
367 # force content to be htmlize'd as if it was the same type as the master page
368 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
371 sub percenttranslated ($) { #{{{
374 return gettext("N/A") unless (istranslation($page));
375 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
376 my $file=srcfile($pagesources{$page});
377 my $masterfile = srcfile($pagesources{$masterpage});
380 push @masters,$masterfile;
382 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
384 my $doc=Locale::Po4a::Chooser::new('text',%options);
386 'po_in_name' => \@pos,
387 'file_in_name' => \@masters,
388 'file_in_charset' => 'utf-8',
389 'file_out_charset' => 'utf-8',
390 ) or error("[po/percenttranslated:$file]: failed to translate");
391 my ($percent,$hit,$queries) = $doc->stats();
395 sub otherlanguages ($) { #{{{
399 if (istranslatable($page)) {
400 foreach my $lang (sort keys %{$translations{$page}}) {
401 my $translation = $translations{$page}{$lang};
403 url => urlto($translation, $page),
405 language => $config{po_slave_languages}{$lang},
406 percent => percenttranslated($translation),
410 elsif (istranslation($page)) {
411 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
413 url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
414 code => $config{po_master_language}{code},
415 language => $config{po_master_language}{name},
418 foreach my $lang (sort keys %{$translations{$masterpage}}) {
420 url => urlto($translations{$masterpage}{$lang}, $page),
422 language => $config{po_slave_languages}{$lang},
423 percent => percenttranslated($translations{$masterpage}{$lang}),
424 } unless ($lang eq $curlang);
430 sub pagetemplate (@) { #{{{
432 my $page=$params{page};
433 my $destpage=$params{destpage};
434 my $template=$params{template};
436 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
438 if (istranslation($page) && $template->query(name => "percenttranslated")) {
439 $template->param(percenttranslated => percenttranslated($page));
441 if ($template->query(name => "istranslation")) {
442 $template->param(istranslation => istranslation($page));
444 if ($template->query(name => "istranslatable")) {
445 $template->param(istranslatable => istranslatable($page));
447 if ($template->query(name => "otherlanguages")) {
448 $template->param(otherlanguages => [otherlanguages($page)]);
449 if (istranslatable($page)) {
450 foreach my $translation (values %{$translations{$page}}) {
451 add_depends($page, $translation);
454 elsif (istranslation($page)) {
455 add_depends($page, $masterpage);
456 foreach my $translation (values %{$translations{$masterpage}}) {
457 add_depends($page, $translation);
461 # Rely on IkiWiki::Render's genpage() to decide wether
462 # a discussion link should appear on $page; this is not
463 # totally accurate, though: some broken links may be generated
464 # when cgiurl is disabled.
465 # This compromise avoids some code duplication, and will probably
466 # prevent future breakage when ikiwiki internals change.
467 # Known limitations are preferred to future random bugs.
468 if ($template->param('discussionlink') && istranslation($page)) {
469 $template->param('discussionlink' => htmllink(
472 $masterpage . '/' . gettext("Discussion"),
475 linktext => gettext("Discussion"),
478 # remove broken parentlink to ./index.html on home page's translations
479 if ($template->param('parentlinks')
480 && istranslation($page)
481 && $masterpage eq "index") {
482 $template->param('parentlinks' => []);
486 sub editcontent () { #{{{
488 # as we're previewing or saving a page, the content may have
489 # changed, so tell the next filter() invocation it must not be lazy
490 if (exists $filtered{$params{page}}{$params{page}}) {
491 delete $filtered{$params{page}}{$params{page}};
493 return $params{content};
496 sub istranslatable ($) { #{{{
499 my $file=$pagesources{$page};
502 || (defined pagetype($file) && pagetype($file) eq 'po')
503 || $file =~ /\.pot$/) {
506 return pagespec_match($page, $config{po_translatable_pages});
509 sub _istranslation ($) { #{{{
512 my $file=$pagesources{$page};
513 if (! defined $file) {
514 return IkiWiki::FailReason->new("no file specified");
518 || ! defined pagetype($file)
519 || ! pagetype($file) eq 'po'
520 || $file =~ /\.pot$/) {
524 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
525 if (! defined $masterpage || ! defined $lang
526 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
527 || ! defined $pagesources{$masterpage}
528 || ! defined $config{po_slave_languages}{$lang}) {
532 return istranslatable($masterpage);
535 sub istranslation ($) { #{{{
538 if (_istranslation($page)) {
539 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
540 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
546 package IkiWiki::PageSpec;
551 sub match_istranslation ($;@) { #{{{
554 if (IkiWiki::Plugin::po::istranslation($page)) {
555 return IkiWiki::SuccessReason->new("is a translation page");
558 return IkiWiki::FailReason->new("is not a translation page");
562 sub match_istranslatable ($;@) { #{{{
565 if (IkiWiki::Plugin::po::istranslatable($page)) {
566 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
569 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
573 sub match_lang ($$;@) { #{{{
577 my $regexp=IkiWiki::glob2re($wanted);
581 if (IkiWiki::Plugin::po::istranslation($page)) {
582 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
585 $lang = $config{po_master_language}{code};
588 if ($lang!~/^$regexp$/i) {
589 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
592 return IkiWiki::SuccessReason->new("file language is $wanted");
596 sub match_currentlang ($$;@) { #{{{
601 my ($currentmasterpage, $currentlang, $masterpage, $lang);
603 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
605 if (IkiWiki::Plugin::po::istranslation($params{location})) {
606 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
609 $currentlang = $config{po_master_language}{code};
612 if (IkiWiki::Plugin::po::istranslation($page)) {
613 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
616 $lang = $config{po_master_language}{code};
619 if ($lang eq $currentlang) {
620 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
623 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");