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);
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 return File::Spec->catfile($dir, $name . ".pot");
118 sub pofile ($$) { #{{{
119 my $masterfile=shift;
121 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
122 return File::Spec->catfile($dir, $name . "." . $lang . ".po");
125 sub refreshpot ($) { #{{{
126 my $masterfile=shift;
127 my $potfile=potfile($masterfile);
128 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
129 my $doc=Locale::Po4a::Chooser::new('text',%options);
130 $doc->read($masterfile);
131 $doc->{TT}{utf_mode} = 1;
132 $doc->{TT}{file_in_charset} = 'utf-8';
133 $doc->{TT}{file_out_charset} = 'utf-8';
134 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
135 # this is undocument use of internal Locale::Po4a::TransTractor's data,
136 # compulsory since this module prevents us from using the porefs option.
137 my %po_options = ('porefs' => 'none');
138 $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
139 $doc->{TT}{po_out}->set_charset('utf-8');
142 $doc->writepo($potfile);
145 sub refreshpofiles ($@) { #{{{
146 my $masterfile=shift;
149 my $potfile=potfile($masterfile);
150 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
152 foreach my $pofile (@pofiles) {
154 my $cmd = "msgmerge -U --backup=none $pofile $potfile";
156 or error("[po/refreshpofiles:$pofile] failed to update");
159 File::Copy::syscopy($potfile,$pofile)
160 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
165 sub needsbuild () { #{{{
166 my $needsbuild=shift;
168 # build %translations, using istranslation's side-effect
169 foreach my $page (keys %pagesources) {
170 istranslation($page);
173 # refresh/create POT and PO files as needed
174 my $updated_po_files=0;
175 foreach my $page (keys %pagesources) {
176 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
177 if (istranslatable($page)) {
178 my $file=srcfile($pagesources{$page});
179 if ($pageneedsbuild || ! -e potfile($file)) {
183 foreach my $lang (keys %{$config{po_slave_languages}}) {
184 my $pofile=pofile($file, $lang);
185 if ($pageneedsbuild || ! -e $pofile) {
186 push @pofiles, $pofile;
190 refreshpofiles($file, @pofiles) ;
191 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
192 $updated_po_files = 1;
197 # check staged changes in and trigger a wiki refresh.
198 if ($updated_po_files) {
200 IkiWiki::disable_commit_hook();
201 IkiWiki::rcs_commit_staged(gettext("updated PO files"),
202 "refreshpofiles", "127.0.0.1");
203 IkiWiki::enable_commit_hook();
204 IkiWiki::rcs_update();
207 IkiWiki::saveindex();
208 # refresh module's private variables
211 foreach my $page (keys %pagesources) {
212 istranslation($page);
217 # make existing translations depend on the corresponding master page
218 foreach my $master (keys %translations) {
219 foreach my $slave (values %{$translations{$master}}) {
220 add_depends($slave, $master);
225 sub mytargetpage ($$) { #{{{
229 if (istranslation($page)) {
230 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
231 if (! $config{usedirs} || $masterpage eq 'index') {
232 return $masterpage . "." . $lang . "." . $ext;
235 return $masterpage . "/index." . $lang . "." . $ext;
238 elsif (istranslatable($page)) {
239 if (! $config{usedirs} || $page eq 'index') {
240 return $page . "." . $config{po_master_language}{code} . "." . $ext;
243 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
246 return $origsubs{'targetpage'}->($page, $ext);
249 sub mybeautify_urlpath ($) { #{{{
251 my $res=$origsubs{'beautify_urlpath'}->($url);
252 if ($config{po_link_to} eq "negotiated") {
253 $res =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
258 sub urlto_with_orig_beautiful_urlpath($$) { #{{{
262 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
263 my $res=urlto($to, $from);
264 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
269 sub mybestlink ($$) { #{{{
272 my $res=$origsubs{'bestlink'}->($page, $link);
274 if ($config{po_link_to} eq "current"
275 && istranslatable($res)
276 && istranslation($page)) {
277 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
278 return $res . "." . $curlang;
287 # We use filter to convert PO to the master page's format,
288 # since the rest of ikiwiki should not work on PO files.
289 sub filter (@) { #{{{
291 my $page = $params{page};
292 my $destpage = $params{destpage};
293 my $content = decode_utf8(encode_utf8($params{content}));
295 return $content if ( ! istranslation($page)
296 || ( exists $filtered{$page}{$destpage}
297 && $filtered{$page}{$destpage} eq 1 ));
299 # CRLF line terminators make poor Locale::Po4a feel bad
300 $content=~s/\r\n/\n/g;
302 # Locale::Po4a needs an input file, and I'm too lazy to learn
303 # how to disguise a variable as a file
304 my $infile = File::Temp->new(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
305 TMPDIR => 1)->filename;
306 writefile(basename($infile), File::Spec->tmpdir, $content);
308 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
309 my $masterfile = srcfile($pagesources{$masterpage});
312 push @masters,$masterfile;
314 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
316 my $doc=Locale::Po4a::Chooser::new('text',%options);
318 'po_in_name' => \@pos,
319 'file_in_name' => \@masters,
320 'file_in_charset' => 'utf-8',
321 'file_out_charset' => 'utf-8',
322 ) or error("[po/filter:$infile]: failed to translate");
323 my $tmpout = File::Temp->new(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
324 TMPDIR => 1)->filename;
325 $doc->write($tmpout) or error("[po/filter:$infile] could not write $tmpout");
326 $content = readfile($tmpout) or error("[po/filter:$infile] could not read $tmpout");
327 $filtered{$page}{$destpage}=1;
331 sub htmlize (@) { #{{{
333 my $page = $params{page};
334 my $content = $params{content};
335 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
336 my $masterfile = srcfile($pagesources{$masterpage});
338 # force content to be htmlize'd as if it was the same type as the master page
339 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
342 sub percenttranslated ($) { #{{{
344 return "N/A" unless (istranslation($page));
345 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
346 my $file=srcfile($pagesources{$page});
347 my $masterfile = srcfile($pagesources{$masterpage});
350 push @masters,$masterfile;
352 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
354 my $doc=Locale::Po4a::Chooser::new('text',%options);
356 'po_in_name' => \@pos,
357 'file_in_name' => \@masters,
358 'file_in_charset' => 'utf-8',
359 'file_out_charset' => 'utf-8',
360 ) or error("[po/percenttranslated:$file]: failed to translate");
361 my ($percent,$hit,$queries) = $doc->stats();
365 sub otherlanguages ($) { #{{{
368 if (istranslatable($page)) {
369 foreach my $lang (sort keys %{$translations{$page}}) {
370 my $translation = $translations{$page}{$lang};
372 url => urlto($translation, $page),
374 language => $config{po_slave_languages}{$lang},
375 percent => percenttranslated($translation),
379 elsif (istranslation($page)) {
380 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
382 url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
383 code => $config{po_master_language}{code},
384 language => $config{po_master_language}{name},
387 foreach my $lang (sort keys %{$translations{$masterpage}}) {
389 url => urlto($translations{$masterpage}{$lang}, $page),
391 language => $config{po_slave_languages}{$lang},
392 percent => percenttranslated($translations{$masterpage}{$lang}),
393 } unless ($lang eq $curlang);
399 sub pagetemplate (@) { #{{{
401 my $page=$params{page};
402 my $destpage=$params{destpage};
403 my $template=$params{template};
405 if (istranslation($page) && $template->query(name => "percenttranslated")) {
406 $template->param(percenttranslated => percenttranslated($page));
408 if ($template->query(name => "istranslation")) {
409 $template->param(istranslation => istranslation($page));
411 if ($template->query(name => "istranslatable")) {
412 $template->param(istranslatable => istranslatable($page));
414 if ($template->query(name => "otherlanguages")) {
415 $template->param(otherlanguages => [otherlanguages($page)]);
416 if (istranslatable($page)) {
417 foreach my $translation (values %{$translations{$page}}) {
418 add_depends($page, $translation);
421 elsif (istranslation($page)) {
422 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
423 add_depends($page, $masterpage);
424 foreach my $translation (values %{$translations{$masterpage}}) {
425 add_depends($page, $translation);
429 # Rely on IkiWiki::Render's genpage() to decide wether
430 # a discussion link should appear on $page; this is not
431 # totally accurate, though: some broken links may be generated
432 # when cgiurl is disabled.
433 # This compromise avoids some code duplication, and will probably
434 # prevent future breakage when ikiwiki internals change.
435 # Known limitations are preferred to future random bugs.
436 if ($template->param('discussionlink') && istranslation($page)) {
437 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
438 $template->param('discussionlink' => htmllink(
441 $masterpage . '/' . gettext("Discussion"),
444 linktext => gettext("Discussion"),
449 sub editcontent () { #{{{
451 # as we're previewing or saving a page, the content may have
452 # changed, so tell the next filter() invocation it must not be lazy
453 if (exists $filtered{$params{page}}{$params{page}}) {
454 delete $filtered{$params{page}}{$params{page}};
456 return $params{content};
459 sub istranslatable ($) { #{{{
461 my $file=$pagesources{$page};
464 || (defined pagetype($file) && pagetype($file) eq 'po')
465 || $file =~ /\.pot$/) {
468 return pagespec_match($page, $config{po_translatable_pages});
471 sub _istranslation ($) { #{{{
473 my $file=$pagesources{$page};
474 if (! defined $file) {
475 return IkiWiki::FailReason->new("no file specified");
479 || ! defined pagetype($file)
480 || ! pagetype($file) eq 'po'
481 || $file =~ /\.pot$/) {
485 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
486 if (! defined $masterpage || ! defined $lang
487 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
488 || ! defined $pagesources{$masterpage}
489 || ! defined $config{po_slave_languages}{$lang}) {
493 return istranslatable($masterpage);
496 sub istranslation ($) { #{{{
498 if (_istranslation($page)) {
499 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
500 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
506 package IkiWiki::PageSpec;
511 sub match_istranslation ($;@) { #{{{
513 if (IkiWiki::Plugin::po::istranslation($page)) {
514 return IkiWiki::SuccessReason->new("is a translation page");
517 return IkiWiki::FailReason->new("is not a translation page");
521 sub match_istranslatable ($;@) { #{{{
523 if (IkiWiki::Plugin::po::istranslatable($page)) {
524 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
527 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
531 sub match_lang ($$;@) { #{{{
534 my $regexp=IkiWiki::glob2re($wanted);
538 if (IkiWiki::Plugin::po::istranslation($page)) {
539 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
542 $lang = $config{po_master_language}{code};
545 if ($lang!~/^$regexp$/i) {
546 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
549 return IkiWiki::SuccessReason->new("file language is $wanted");
553 sub match_currentlang ($$;@) { #{{{
557 my ($currentmasterpage, $currentlang, $masterpage, $lang);
559 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
561 if (IkiWiki::Plugin::po::istranslation($params{location})) {
562 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
565 $currentlang = $config{po_master_language}{code};
568 if (IkiWiki::Plugin::po::istranslation($page)) {
569 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
572 $lang = $config{po_master_language}{code};
575 if ($lang eq $currentlang) {
576 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
579 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");