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 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};
404 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
406 if (istranslation($page) && $template->query(name => "percenttranslated")) {
407 $template->param(percenttranslated => percenttranslated($page));
409 if ($template->query(name => "istranslation")) {
410 $template->param(istranslation => istranslation($page));
412 if ($template->query(name => "istranslatable")) {
413 $template->param(istranslatable => istranslatable($page));
415 if ($template->query(name => "otherlanguages")) {
416 $template->param(otherlanguages => [otherlanguages($page)]);
417 if (istranslatable($page)) {
418 foreach my $translation (values %{$translations{$page}}) {
419 add_depends($page, $translation);
422 elsif (istranslation($page)) {
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 $template->param('discussionlink' => htmllink(
440 $masterpage . '/' . gettext("Discussion"),
443 linktext => gettext("Discussion"),
446 # remove broken parentlink to ./index.html on home page's translations
447 if ($template->param('parentlinks')
448 && istranslation($page)
449 && $masterpage eq "index") {
450 $template->param('parentlinks' => []);
454 sub editcontent () { #{{{
456 # as we're previewing or saving a page, the content may have
457 # changed, so tell the next filter() invocation it must not be lazy
458 if (exists $filtered{$params{page}}{$params{page}}) {
459 delete $filtered{$params{page}}{$params{page}};
461 return $params{content};
464 sub istranslatable ($) { #{{{
466 my $file=$pagesources{$page};
469 || (defined pagetype($file) && pagetype($file) eq 'po')
470 || $file =~ /\.pot$/) {
473 return pagespec_match($page, $config{po_translatable_pages});
476 sub _istranslation ($) { #{{{
478 my $file=$pagesources{$page};
479 if (! defined $file) {
480 return IkiWiki::FailReason->new("no file specified");
484 || ! defined pagetype($file)
485 || ! pagetype($file) eq 'po'
486 || $file =~ /\.pot$/) {
490 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
491 if (! defined $masterpage || ! defined $lang
492 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
493 || ! defined $pagesources{$masterpage}
494 || ! defined $config{po_slave_languages}{$lang}) {
498 return istranslatable($masterpage);
501 sub istranslation ($) { #{{{
503 if (_istranslation($page)) {
504 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
505 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
511 package IkiWiki::PageSpec;
516 sub match_istranslation ($;@) { #{{{
518 if (IkiWiki::Plugin::po::istranslation($page)) {
519 return IkiWiki::SuccessReason->new("is a translation page");
522 return IkiWiki::FailReason->new("is not a translation page");
526 sub match_istranslatable ($;@) { #{{{
528 if (IkiWiki::Plugin::po::istranslatable($page)) {
529 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
532 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
536 sub match_lang ($$;@) { #{{{
539 my $regexp=IkiWiki::glob2re($wanted);
543 if (IkiWiki::Plugin::po::istranslation($page)) {
544 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
547 $lang = $config{po_master_language}{code};
550 if ($lang!~/^$regexp$/i) {
551 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
554 return IkiWiki::SuccessReason->new("file language is $wanted");
558 sub match_currentlang ($$;@) { #{{{
562 my ($currentmasterpage, $currentlang, $masterpage, $lang);
564 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
566 if (IkiWiki::Plugin::po::istranslation($params{location})) {
567 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
570 $currentlang = $config{po_master_language}{code};
573 if (IkiWiki::Plugin::po::istranslation($page)) {
574 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
577 $lang = $config{po_master_language}{code};
580 if ($lang eq $currentlang) {
581 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
584 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");