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 ## FIXME: makes some test cases cry once every two tries; this may be
23 ## related to the artificial way the testsuite is run, or not.
24 # memoize("istranslatable");
25 memoize("_istranslation");
26 memoize("percenttranslated");
28 # backup references to subs that will be overriden
32 hook(type => "getsetup", id => "po", call => \&getsetup);
33 hook(type => "checkconfig", id => "po", call => \&checkconfig);
34 hook(type => "needsbuild", id => "po", call => \&needsbuild);
35 hook(type => "filter", id => "po", call => \&filter);
36 hook(type => "htmlize", id => "po", call => \&htmlize);
37 hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
39 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
40 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
41 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
42 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
43 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
44 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
47 sub getsetup () { #{{{
51 rebuild => 1, # format plugin & changes html filenames
53 po_master_language => {
59 description => "master language (non-PO files)",
63 po_slave_languages => {
70 description => "slave languages (PO files)",
74 po_translatable_pages => {
76 example => "!*/Discussion",
77 description => "PageSpec controlling which pages are translatable",
78 link => "ikiwiki/PageSpec",
85 description => "internal linking behavior (default/current/negotiated)",
91 sub checkconfig () { #{{{
92 foreach my $field (qw{po_master_language po_slave_languages}) {
93 if (! exists $config{$field} || ! defined $config{$field}) {
94 error(sprintf(gettext("Must specify %s"), $field));
97 if (! exists $config{po_link_to} ||
98 ! defined $config{po_link_to}) {
99 $config{po_link_to}="default";
101 if (! exists $config{po_translatable_pages} ||
102 ! defined $config{po_translatable_pages}) {
103 $config{po_translatable_pages}="";
105 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
106 error(gettext("po_link_to=negotiated requires usedirs to be set"));
108 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
111 sub potfile ($) { #{{{
112 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;
122 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
123 return File::Spec->catfile($dir, $name . "." . $lang . ".po");
126 sub refreshpot ($) { #{{{
127 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 system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
157 or error("[po/refreshpofiles:$pofile] failed to update");
160 File::Copy::syscopy($potfile,$pofile)
161 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
166 sub needsbuild () { #{{{
167 my $needsbuild=shift;
169 # build %translations, using istranslation's side-effect
170 foreach my $page (keys %pagesources) {
171 istranslation($page);
174 # refresh/create POT and PO files as needed
175 my $updated_po_files=0;
176 foreach my $page (keys %pagesources) {
177 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
178 if (istranslatable($page)) {
179 my $file=srcfile($pagesources{$page});
180 if ($pageneedsbuild || ! -e potfile($file)) {
184 foreach my $lang (keys %{$config{po_slave_languages}}) {
185 my $pofile=pofile($file, $lang);
186 if ($pageneedsbuild || ! -e $pofile) {
187 push @pofiles, $pofile;
191 refreshpofiles($file, @pofiles) ;
192 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
193 $updated_po_files = 1;
198 # check staged changes in and trigger a wiki refresh.
199 if ($updated_po_files) {
201 IkiWiki::disable_commit_hook();
202 IkiWiki::rcs_commit_staged(gettext("updated PO files"),
203 "refreshpofiles", "127.0.0.1");
204 IkiWiki::enable_commit_hook();
205 IkiWiki::rcs_update();
208 IkiWiki::saveindex();
209 # refresh module's private variables
212 foreach my $page (keys %pagesources) {
213 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 ($) { #{{{
252 my $res=$origsubs{'beautify_urlpath'}->($url);
253 if ($config{po_link_to} eq "negotiated") {
254 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
259 sub urlto_with_orig_beautiful_urlpath($$) { #{{{
263 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
264 my $res=urlto($to, $from);
265 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
270 sub mybestlink ($$) { #{{{
274 my $res=$origsubs{'bestlink'}->($page, $link);
276 if ($config{po_link_to} eq "current"
277 && istranslatable($res)
278 && istranslation($page)) {
279 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
280 return $res . "." . $curlang;
289 # We use filter to convert PO to the master page's type,
290 # since other plugins should not work on PO files
291 sub filter (@) { #{{{
294 my $page = $params{page};
295 my $destpage = $params{destpage};
296 my $content = decode_utf8(encode_utf8($params{content}));
298 # decide if this is a PO file that should be converted into a
299 # translated document, and perform various sanity checks
300 if (! istranslation($page) || $filtered{$page}{$destpage}) {
304 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
305 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
306 my $masterfile = srcfile($pagesources{$masterpage});
309 push @masters,$masterfile;
311 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
313 my $doc=Locale::Po4a::Chooser::new('text',%options);
315 'po_in_name' => \@pos,
316 'file_in_name' => \@masters,
317 'file_in_charset' => 'utf-8',
318 'file_out_charset' => 'utf-8',
319 ) or error("[po/filter:$file]: failed to translate");
320 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
321 my $tmpout = $tmpfh->filename;
322 # XXX is there any way to avoid the useless write to a temp file? --Joey
323 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
324 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
325 $filtered{$page}{$destpage}=1;
329 sub htmlize (@) { #{{{
332 my $page = $params{page};
333 my $content = $params{content};
334 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
335 my $masterfile = srcfile($pagesources{$masterpage});
337 # force content to be htmlize'd as if it was the same type as the master page
338 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
341 sub percenttranslated ($) { #{{{
344 return gettext("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 ($) { #{{{
369 if (istranslatable($page)) {
370 foreach my $lang (sort keys %{$translations{$page}}) {
371 my $translation = $translations{$page}{$lang};
373 url => urlto($translation, $page),
375 language => $config{po_slave_languages}{$lang},
376 percent => percenttranslated($translation),
380 elsif (istranslation($page)) {
381 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
383 url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
384 code => $config{po_master_language}{code},
385 language => $config{po_master_language}{name},
388 foreach my $lang (sort keys %{$translations{$masterpage}}) {
390 url => urlto($translations{$masterpage}{$lang}, $page),
392 language => $config{po_slave_languages}{$lang},
393 percent => percenttranslated($translations{$masterpage}{$lang}),
394 } unless ($lang eq $curlang);
400 sub pagetemplate (@) { #{{{
402 my $page=$params{page};
404 my $destpage=$params{destpage};
405 my $template=$params{template};
407 if (istranslation($page) && $template->query(name => "percenttranslated")) {
408 $template->param(percenttranslated => percenttranslated($page));
410 if ($template->query(name => "istranslation")) {
411 $template->param(istranslation => istranslation($page));
413 if ($template->query(name => "istranslatable")) {
414 $template->param(istranslatable => istranslatable($page));
416 if ($template->query(name => "otherlanguages")) {
417 $template->param(otherlanguages => [otherlanguages($page)]);
418 if (istranslatable($page)) {
419 foreach my $translation (values %{$translations{$page}}) {
420 add_depends($page, $translation);
423 elsif (istranslation($page)) {
424 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
425 add_depends($page, $masterpage);
426 foreach my $translation (values %{$translations{$masterpage}}) {
427 add_depends($page, $translation);
431 # Rely on IkiWiki::Render's genpage() to decide wether
432 # a discussion link should appear on $page; this is not
433 # totally accurate, though: some broken links may be generated
434 # when cgiurl is disabled.
435 # This compromise avoids some code duplication, and will probably
436 # prevent future breakage when ikiwiki internals change.
437 # Known limitations are preferred to future random bugs.
438 if ($template->param('discussionlink') && istranslation($page)) {
439 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
440 $template->param('discussionlink' => htmllink(
443 $masterpage . '/' . gettext("Discussion"),
446 linktext => gettext("Discussion"),
451 sub istranslatable ($) { #{{{
454 my $file=$pagesources{$page};
457 || (defined pagetype($file) && pagetype($file) eq 'po')
458 || $file =~ /\.pot$/) {
461 return pagespec_match($page, $config{po_translatable_pages});
464 sub _istranslation ($) { #{{{
467 my $file=$pagesources{$page};
468 if (! defined $file) {
469 return IkiWiki::FailReason->new("no file specified");
473 || ! defined pagetype($file)
474 || ! pagetype($file) eq 'po'
475 || $file =~ /\.pot$/) {
479 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
480 if (! defined $masterpage || ! defined $lang
481 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
482 || ! defined $pagesources{$masterpage}
483 || ! defined $config{po_slave_languages}{$lang}) {
487 return istranslatable($masterpage);
490 sub istranslation ($) { #{{{
493 if (_istranslation($page)) {
494 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
495 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
501 package IkiWiki::PageSpec;
506 sub match_istranslation ($;@) { #{{{
509 if (IkiWiki::Plugin::po::istranslation($page)) {
510 return IkiWiki::SuccessReason->new("is a translation page");
513 return IkiWiki::FailReason->new("is not a translation page");
517 sub match_istranslatable ($;@) { #{{{
520 if (IkiWiki::Plugin::po::istranslatable($page)) {
521 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
524 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
528 sub match_lang ($$;@) { #{{{
532 my $regexp=IkiWiki::glob2re($wanted);
536 if (IkiWiki::Plugin::po::istranslation($page)) {
537 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
540 $lang = $config{po_master_language}{code};
543 if ($lang!~/^$regexp$/i) {
544 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
547 return IkiWiki::SuccessReason->new("file language is $wanted");
551 sub match_currentlang ($$;@) { #{{{
556 my ($currentmasterpage, $currentlang, $masterpage, $lang);
558 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
560 if (IkiWiki::Plugin::po::istranslation($params{location})) {
561 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
564 $currentlang = $config{po_master_language}{code};
567 if (IkiWiki::Plugin::po::istranslation($page)) {
568 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
571 $lang = $config{po_master_language}{code};
574 if ($lang eq $currentlang) {
575 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
578 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");