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 # Locale::Po4a needs an input file, and I'm too lazy to learn
307 # how to disguise a variable as a file
308 my $infile = File::Temp->new(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
309 TMPDIR => 1)->filename;
310 writefile(basename($infile), File::Spec->tmpdir, $content);
312 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
313 my $masterfile = srcfile($pagesources{$masterpage});
316 push @masters,$masterfile;
318 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
320 my $doc=Locale::Po4a::Chooser::new('text',%options);
322 'po_in_name' => \@pos,
323 'file_in_name' => \@masters,
324 'file_in_charset' => 'utf-8',
325 'file_out_charset' => 'utf-8',
326 ) or error("[po/filter:$infile]: failed to translate");
327 my $tmpout = File::Temp->new(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
328 TMPDIR => 1)->filename;
329 $doc->write($tmpout) or error("[po/filter:$infile] could not write $tmpout");
330 $content = readfile($tmpout) or error("[po/filter:$infile] could not read $tmpout");
331 $filtered{$page}{$destpage}=1;
335 sub htmlize (@) { #{{{
337 my $page = $params{page};
338 my $content = $params{content};
339 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
340 my $masterfile = srcfile($pagesources{$masterpage});
342 # force content to be htmlize'd as if it was the same type as the master page
343 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
346 sub percenttranslated ($) { #{{{
348 return "N/A" unless (istranslation($page));
349 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
350 my $file=srcfile($pagesources{$page});
351 my $masterfile = srcfile($pagesources{$masterpage});
354 push @masters,$masterfile;
356 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
358 my $doc=Locale::Po4a::Chooser::new('text',%options);
360 'po_in_name' => \@pos,
361 'file_in_name' => \@masters,
362 'file_in_charset' => 'utf-8',
363 'file_out_charset' => 'utf-8',
364 ) or error("[po/percenttranslated:$file]: failed to translate");
365 my ($percent,$hit,$queries) = $doc->stats();
369 sub otherlanguages ($) { #{{{
372 if (istranslatable($page)) {
373 foreach my $lang (sort keys %{$translations{$page}}) {
374 my $translation = $translations{$page}{$lang};
376 url => urlto($translation, $page),
378 language => $config{po_slave_languages}{$lang},
379 percent => percenttranslated($translation),
383 elsif (istranslation($page)) {
384 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
386 url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
387 code => $config{po_master_language}{code},
388 language => $config{po_master_language}{name},
391 foreach my $lang (sort keys %{$translations{$masterpage}}) {
393 url => urlto($translations{$masterpage}{$lang}, $page),
395 language => $config{po_slave_languages}{$lang},
396 percent => percenttranslated($translations{$masterpage}{$lang}),
397 } unless ($lang eq $curlang);
403 sub pagetemplate (@) { #{{{
405 my $page=$params{page};
406 my $destpage=$params{destpage};
407 my $template=$params{template};
408 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
410 if (istranslation($page) && $template->query(name => "percenttranslated")) {
411 $template->param(percenttranslated => percenttranslated($page));
413 if ($template->query(name => "istranslation")) {
414 $template->param(istranslation => istranslation($page));
416 if ($template->query(name => "istranslatable")) {
417 $template->param(istranslatable => istranslatable($page));
419 if ($template->query(name => "otherlanguages")) {
420 $template->param(otherlanguages => [otherlanguages($page)]);
421 if (istranslatable($page)) {
422 foreach my $translation (values %{$translations{$page}}) {
423 add_depends($page, $translation);
426 elsif (istranslation($page)) {
427 add_depends($page, $masterpage);
428 foreach my $translation (values %{$translations{$masterpage}}) {
429 add_depends($page, $translation);
433 # Rely on IkiWiki::Render's genpage() to decide wether
434 # a discussion link should appear on $page; this is not
435 # totally accurate, though: some broken links may be generated
436 # when cgiurl is disabled.
437 # This compromise avoids some code duplication, and will probably
438 # prevent future breakage when ikiwiki internals change.
439 # Known limitations are preferred to future random bugs.
440 if ($template->param('discussionlink') && istranslation($page)) {
441 $template->param('discussionlink' => htmllink(
444 $masterpage . '/' . gettext("Discussion"),
447 linktext => gettext("Discussion"),
450 # remove broken parentlink to ./index.html on home page's translations
451 if ($template->param('parentlinks')
452 && istranslation($page)
453 && $masterpage eq "index") {
454 $template->param('parentlinks' => []);
458 sub editcontent () { #{{{
460 # as we're previewing or saving a page, the content may have
461 # changed, so tell the next filter() invocation it must not be lazy
462 if (exists $filtered{$params{page}}{$params{page}}) {
463 delete $filtered{$params{page}}{$params{page}};
465 return $params{content};
468 sub istranslatable ($) { #{{{
470 my $file=$pagesources{$page};
473 || (defined pagetype($file) && pagetype($file) eq 'po')
474 || $file =~ /\.pot$/) {
477 return pagespec_match($page, $config{po_translatable_pages});
480 sub _istranslation ($) { #{{{
482 my $file=$pagesources{$page};
483 if (! defined $file) {
484 return IkiWiki::FailReason->new("no file specified");
488 || ! defined pagetype($file)
489 || ! pagetype($file) eq 'po'
490 || $file =~ /\.pot$/) {
494 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
495 if (! defined $masterpage || ! defined $lang
496 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
497 || ! defined $pagesources{$masterpage}
498 || ! defined $config{po_slave_languages}{$lang}) {
502 return istranslatable($masterpage);
505 sub istranslation ($) { #{{{
507 if (_istranslation($page)) {
508 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
509 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
515 package IkiWiki::PageSpec;
520 sub match_istranslation ($;@) { #{{{
522 if (IkiWiki::Plugin::po::istranslation($page)) {
523 return IkiWiki::SuccessReason->new("is a translation page");
526 return IkiWiki::FailReason->new("is not a translation page");
530 sub match_istranslatable ($;@) { #{{{
532 if (IkiWiki::Plugin::po::istranslatable($page)) {
533 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
536 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
540 sub match_lang ($$;@) { #{{{
543 my $regexp=IkiWiki::glob2re($wanted);
547 if (IkiWiki::Plugin::po::istranslation($page)) {
548 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
551 $lang = $config{po_master_language}{code};
554 if ($lang!~/^$regexp$/i) {
555 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
558 return IkiWiki::SuccessReason->new("file language is $wanted");
562 sub match_currentlang ($$;@) { #{{{
566 my ($currentmasterpage, $currentlang, $masterpage, $lang);
568 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
570 if (IkiWiki::Plugin::po::istranslation($params{location})) {
571 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
574 $currentlang = $config{po_master_language}{code};
577 if (IkiWiki::Plugin::po::istranslation($page)) {
578 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
581 $lang = $config{po_master_language}{code};
584 if ($lang eq $currentlang) {
585 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
588 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");