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
31 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
32 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
33 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
36 hook(type => "getsetup", id => "po", call => \&getsetup);
37 hook(type => "checkconfig", id => "po", call => \&checkconfig);
38 hook(type => "needsbuild", id => "po", call => \&needsbuild);
39 hook(type => "filter", id => "po", call => \&filter);
40 hook(type => "htmlize", id => "po", call => \&htmlize);
41 hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
42 hook(type => "editcontent", id => "po", call => \&editcontent);
43 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
44 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
45 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
48 sub getsetup () { #{{{
52 rebuild => 1, # format plugin
54 po_master_language => {
60 description => "master language (non-PO files)",
64 po_slave_languages => {
71 description => "slave languages (PO files)",
75 po_translatable_pages => {
77 example => "!*/Discussion",
78 description => "PageSpec controlling which pages are translatable",
79 link => "ikiwiki/PageSpec",
86 description => "internal linking behavior (default/current/negotiated)",
92 sub checkconfig () { #{{{
93 foreach my $field (qw{po_master_language po_slave_languages}) {
94 if (! exists $config{$field} || ! defined $config{$field}) {
95 error(sprintf(gettext("Must specify %s"), $field));
98 if (! exists $config{po_link_to} ||
99 ! defined $config{po_link_to}) {
100 $config{po_link_to}="default";
102 if (! exists $config{po_translatable_pages} ||
103 ! defined $config{po_translatable_pages}) {
104 $config{po_translatable_pages}="";
106 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
107 error(gettext("po_link_to=negotiated requires usedirs to be set"));
109 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
112 sub potfile ($) { #{{{
113 my $masterfile=shift;
114 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
115 $dir='' if $dir eq './';
116 return File::Spec->catpath('', $dir, $name . ".pot");
119 sub pofile ($$) { #{{{
120 my $masterfile=shift;
122 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
123 $dir='' if $dir eq './';
124 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
127 sub refreshpot ($) { #{{{
128 my $masterfile=shift;
129 my $potfile=potfile($masterfile);
130 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
131 my $doc=Locale::Po4a::Chooser::new('text',%options);
132 $doc->read($masterfile);
133 $doc->{TT}{utf_mode} = 1;
134 $doc->{TT}{file_in_charset} = 'utf-8';
135 $doc->{TT}{file_out_charset} = 'utf-8';
136 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
137 # this is undocument use of internal Locale::Po4a::TransTractor's data,
138 # compulsory since this module prevents us from using the porefs option.
139 my %po_options = ('porefs' => 'none');
140 $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
141 $doc->{TT}{po_out}->set_charset('utf-8');
144 $doc->writepo($potfile);
147 sub refreshpofiles ($@) { #{{{
148 my $masterfile=shift;
151 my $potfile=potfile($masterfile);
152 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
154 foreach my $pofile (@pofiles) {
156 my $cmd = "msgmerge -U --backup=none $pofile $potfile";
158 or error("[po/refreshpofiles:$pofile] failed to update");
161 File::Copy::syscopy($potfile,$pofile)
162 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
167 sub needsbuild () { #{{{
168 my $needsbuild=shift;
170 # build %translations, using istranslation's side-effect
171 foreach my $page (keys %pagesources) {
172 istranslation($page);
175 # refresh/create POT and PO files as needed
176 my $updated_po_files=0;
177 foreach my $page (keys %pagesources) {
178 if (istranslatable($page)) {
179 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
180 my $updated_pot_file=0;
181 my $file=srcfile($pagesources{$page});
182 if ($pageneedsbuild || ! -e potfile($file)) {
187 foreach my $lang (keys %{$config{po_slave_languages}}) {
188 my $pofile=pofile($file, $lang);
189 my $pofile_rel=pofile($pagesources{$page}, $lang);
190 if ($pageneedsbuild || $updated_pot_file || ! -e $pofile) {
191 push @pofiles, $pofile;
192 push @$needsbuild, $pofile_rel
193 unless grep { $_ eq $pofile_rel } @$needsbuild;
197 refreshpofiles($file, @pofiles) ;
198 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
199 $updated_po_files = 1;
204 # check staged changes in
205 if ($updated_po_files) {
207 IkiWiki::disable_commit_hook();
208 IkiWiki::rcs_commit_staged(gettext("updated PO files"),
209 "refreshpofiles", "127.0.0.1");
210 IkiWiki::enable_commit_hook();
211 IkiWiki::rcs_update();
213 # refresh module's private variables
216 foreach my $page (keys %pagesources) {
217 istranslation($page);
221 # make existing translations depend on the corresponding master page
222 foreach my $master (keys %translations) {
223 foreach my $slave (values %{$translations{$master}}) {
224 add_depends($slave, $master);
229 sub mytargetpage ($$) { #{{{
233 if (istranslation($page)) {
234 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
235 if (! $config{usedirs} || $masterpage eq 'index') {
236 return $masterpage . "." . $lang . "." . $ext;
239 return $masterpage . "/index." . $lang . "." . $ext;
242 elsif (istranslatable($page)) {
243 if (! $config{usedirs} || $page eq 'index') {
244 return $page . "." . $config{po_master_language}{code} . "." . $ext;
247 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
250 return $origsubs{'targetpage'}->($page, $ext);
253 sub mybeautify_urlpath ($) { #{{{
255 my $res=$origsubs{'beautify_urlpath'}->($url);
256 if ($config{po_link_to} eq "negotiated") {
257 $res =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
262 sub urlto_with_orig_beautiful_urlpath($$) { #{{{
266 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
267 my $res=urlto($to, $from);
268 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
273 sub mybestlink ($$) { #{{{
276 my $res=$origsubs{'bestlink'}->($page, $link);
278 if ($config{po_link_to} eq "current"
279 && istranslatable($res)
280 && istranslation($page)) {
281 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
282 return $res . "." . $curlang;
291 # We use filter to convert PO to the master page's format,
292 # since the rest of ikiwiki should not work on PO files.
293 sub filter (@) { #{{{
295 my $page = $params{page};
296 my $destpage = $params{destpage};
297 my $content = decode_utf8(encode_utf8($params{content}));
299 return $content if ( ! istranslation($page)
300 || ( exists $filtered{$page}{$destpage}
301 && $filtered{$page}{$destpage} eq 1 ));
303 # CRLF line terminators make poor Locale::Po4a feel bad
304 $content=~s/\r\n/\n/g;
306 # Implementation notes
308 # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
309 # to learn how to disguise a variable as a file.
310 # 2. There are incompatibilities between some File::Temp versions
311 # (including 0.18, bundled with Lenny's perl-modules package)
312 # and others (e.g. 0.20, previously present in the archive as
313 # a standalone package): under certain circumstances, some
314 # return a relative filename, whereas others return an absolute one;
315 # we here use this module in a way that is at least compatible
316 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
317 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
318 DIR => File::Spec->tmpdir,
319 UNLINK => 1)->filename;
320 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
321 DIR => File::Spec->tmpdir,
322 UNLINK => 1)->filename;
324 writefile(basename($infile), File::Spec->tmpdir, $content);
326 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
327 my $masterfile = srcfile($pagesources{$masterpage});
330 push @masters,$masterfile;
332 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
334 my $doc=Locale::Po4a::Chooser::new('text',%options);
336 'po_in_name' => \@pos,
337 'file_in_name' => \@masters,
338 'file_in_charset' => 'utf-8',
339 'file_out_charset' => 'utf-8',
340 ) or error("[po/filter:$infile]: failed to translate");
341 $doc->write($outfile) or error("[po/filter:$infile] could not write $outfile");
342 $content = readfile($outfile) or error("[po/filter:$infile] could not read $outfile");
344 # Unlinking should happen automatically, thanks to File::Temp,
345 # but it does not work here, probably because of the way writefile()
346 # and Locale::Po4a::write() work.
347 unlink $infile, $outfile;
349 $filtered{$page}{$destpage}=1;
353 sub htmlize (@) { #{{{
355 my $page = $params{page};
356 my $content = $params{content};
357 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
358 my $masterfile = srcfile($pagesources{$masterpage});
360 # force content to be htmlize'd as if it was the same type as the master page
361 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
364 sub percenttranslated ($) { #{{{
366 return "N/A" unless (istranslation($page));
367 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
368 my $file=srcfile($pagesources{$page});
369 my $masterfile = srcfile($pagesources{$masterpage});
372 push @masters,$masterfile;
374 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
376 my $doc=Locale::Po4a::Chooser::new('text',%options);
378 'po_in_name' => \@pos,
379 'file_in_name' => \@masters,
380 'file_in_charset' => 'utf-8',
381 'file_out_charset' => 'utf-8',
382 ) or error("[po/percenttranslated:$file]: failed to translate");
383 my ($percent,$hit,$queries) = $doc->stats();
387 sub otherlanguages ($) { #{{{
390 if (istranslatable($page)) {
391 foreach my $lang (sort keys %{$translations{$page}}) {
392 my $translation = $translations{$page}{$lang};
394 url => urlto($translation, $page),
396 language => $config{po_slave_languages}{$lang},
397 percent => percenttranslated($translation),
401 elsif (istranslation($page)) {
402 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
404 url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
405 code => $config{po_master_language}{code},
406 language => $config{po_master_language}{name},
409 foreach my $lang (sort keys %{$translations{$masterpage}}) {
411 url => urlto($translations{$masterpage}{$lang}, $page),
413 language => $config{po_slave_languages}{$lang},
414 percent => percenttranslated($translations{$masterpage}{$lang}),
415 } unless ($lang eq $curlang);
421 sub pagetemplate (@) { #{{{
423 my $page=$params{page};
424 my $destpage=$params{destpage};
425 my $template=$params{template};
426 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
428 if (istranslation($page) && $template->query(name => "percenttranslated")) {
429 $template->param(percenttranslated => percenttranslated($page));
431 if ($template->query(name => "istranslation")) {
432 $template->param(istranslation => istranslation($page));
434 if ($template->query(name => "istranslatable")) {
435 $template->param(istranslatable => istranslatable($page));
437 if ($template->query(name => "otherlanguages")) {
438 $template->param(otherlanguages => [otherlanguages($page)]);
439 if (istranslatable($page)) {
440 foreach my $translation (values %{$translations{$page}}) {
441 add_depends($page, $translation);
444 elsif (istranslation($page)) {
445 add_depends($page, $masterpage);
446 foreach my $translation (values %{$translations{$masterpage}}) {
447 add_depends($page, $translation);
451 # Rely on IkiWiki::Render's genpage() to decide wether
452 # a discussion link should appear on $page; this is not
453 # totally accurate, though: some broken links may be generated
454 # when cgiurl is disabled.
455 # This compromise avoids some code duplication, and will probably
456 # prevent future breakage when ikiwiki internals change.
457 # Known limitations are preferred to future random bugs.
458 if ($template->param('discussionlink') && istranslation($page)) {
459 $template->param('discussionlink' => htmllink(
462 $masterpage . '/' . gettext("Discussion"),
465 linktext => gettext("Discussion"),
468 # remove broken parentlink to ./index.html on home page's translations
469 if ($template->param('parentlinks')
470 && istranslation($page)
471 && $masterpage eq "index") {
472 $template->param('parentlinks' => []);
476 sub editcontent () { #{{{
478 # as we're previewing or saving a page, the content may have
479 # changed, so tell the next filter() invocation it must not be lazy
480 if (exists $filtered{$params{page}}{$params{page}}) {
481 delete $filtered{$params{page}}{$params{page}};
483 return $params{content};
486 sub istranslatable ($) { #{{{
488 my $file=$pagesources{$page};
491 || (defined pagetype($file) && pagetype($file) eq 'po')
492 || $file =~ /\.pot$/) {
495 return pagespec_match($page, $config{po_translatable_pages});
498 sub _istranslation ($) { #{{{
500 my $file=$pagesources{$page};
501 if (! defined $file) {
502 return IkiWiki::FailReason->new("no file specified");
506 || ! defined pagetype($file)
507 || ! pagetype($file) eq 'po'
508 || $file =~ /\.pot$/) {
512 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
513 if (! defined $masterpage || ! defined $lang
514 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
515 || ! defined $pagesources{$masterpage}
516 || ! defined $config{po_slave_languages}{$lang}) {
520 return istranslatable($masterpage);
523 sub istranslation ($) { #{{{
525 if (_istranslation($page)) {
526 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
527 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
533 package IkiWiki::PageSpec;
538 sub match_istranslation ($;@) { #{{{
540 if (IkiWiki::Plugin::po::istranslation($page)) {
541 return IkiWiki::SuccessReason->new("is a translation page");
544 return IkiWiki::FailReason->new("is not a translation page");
548 sub match_istranslatable ($;@) { #{{{
550 if (IkiWiki::Plugin::po::istranslatable($page)) {
551 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
554 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
558 sub match_lang ($$;@) { #{{{
561 my $regexp=IkiWiki::glob2re($wanted);
565 if (IkiWiki::Plugin::po::istranslation($page)) {
566 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
569 $lang = $config{po_master_language}{code};
572 if ($lang!~/^$regexp$/i) {
573 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
576 return IkiWiki::SuccessReason->new("file language is $wanted");
580 sub match_currentlang ($$;@) { #{{{
584 my ($currentmasterpage, $currentlang, $masterpage, $lang);
586 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
588 if (IkiWiki::Plugin::po::istranslation($params{location})) {
589 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
592 $currentlang = $config{po_master_language}{code};
595 if (IkiWiki::Plugin::po::istranslation($page)) {
596 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
599 $lang = $config{po_master_language}{code};
602 if ($lang eq $currentlang) {
603 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
606 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");