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
30 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
31 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
32 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
35 hook(type => "getsetup", id => "po", call => \&getsetup);
36 hook(type => "checkconfig", id => "po", call => \&checkconfig);
37 hook(type => "needsbuild", id => "po", call => \&needsbuild);
38 hook(type => "filter", id => "po", call => \&filter);
39 hook(type => "htmlize", id => "po", call => \&htmlize);
40 hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
41 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
42 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
43 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
46 sub getsetup () { #{{{
50 rebuild => 1, # format plugin & changes html filenames
52 po_master_language => {
58 description => "master language (non-PO files)",
62 po_slave_languages => {
69 description => "slave languages (PO files)",
73 po_translatable_pages => {
75 example => "!*/Discussion",
76 description => "PageSpec controlling which pages are translatable",
77 link => "ikiwiki/PageSpec",
84 description => "internal linking behavior (default/current/negotiated)",
90 sub checkconfig () { #{{{
91 foreach my $field (qw{po_master_language po_slave_languages}) {
92 if (! exists $config{$field} || ! defined $config{$field}) {
93 error(sprintf(gettext("Must specify %s"), $field));
96 if (! exists $config{po_link_to} ||
97 ! defined $config{po_link_to}) {
98 $config{po_link_to}="default";
100 if (! exists $config{po_translatable_pages} ||
101 ! defined $config{po_translatable_pages}) {
102 $config{po_translatable_pages}="";
104 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
105 error(gettext("po_link_to=negotiated requires usedirs to be set"));
107 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
110 sub potfile ($) { #{{{
111 my $masterfile=shift;
112 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
113 return File::Spec->catfile($dir, $name . ".pot");
116 sub pofile ($$) { #{{{
117 my $masterfile=shift;
119 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
120 return File::Spec->catfile($dir, $name . "." . $lang . ".po");
123 sub refreshpot ($) { #{{{
124 my $masterfile=shift;
125 my $potfile=potfile($masterfile);
126 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
127 my $doc=Locale::Po4a::Chooser::new('text',%options);
128 $doc->read($masterfile);
129 $doc->{TT}{utf_mode} = 1;
130 $doc->{TT}{file_in_charset} = 'utf-8';
131 $doc->{TT}{file_out_charset} = 'utf-8';
132 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
133 # this is undocument use of internal Locale::Po4a::TransTractor's data,
134 # compulsory since this module prevents us from using the porefs option.
135 my %po_options = ('porefs' => 'none');
136 $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
137 $doc->{TT}{po_out}->set_charset('utf-8');
140 $doc->writepo($potfile);
143 sub refreshpofiles ($@) { #{{{
144 my $masterfile=shift;
147 my $potfile=potfile($masterfile);
148 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
150 foreach my $pofile (@pofiles) {
152 system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
153 or error("[po/refreshpofiles:$pofile] failed to update");
156 File::Copy::syscopy($potfile,$pofile)
157 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
162 sub needsbuild () { #{{{
163 my $needsbuild=shift;
165 # build %translations, using istranslation's side-effect
166 foreach my $page (keys %pagesources) {
167 istranslation($page);
170 # refresh/create POT and PO files as needed
171 my $updated_po_files=0;
172 foreach my $page (keys %pagesources) {
173 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
174 if (istranslatable($page)) {
175 my $file=srcfile($pagesources{$page});
176 if ($pageneedsbuild || ! -e potfile($file)) {
180 foreach my $lang (keys %{$config{po_slave_languages}}) {
181 my $pofile=pofile($file, $lang);
182 if ($pageneedsbuild || ! -e $pofile) {
183 push @pofiles, $pofile;
187 refreshpofiles($file, @pofiles) ;
188 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
189 $updated_po_files = 1;
194 # check staged changes in and trigger a wiki refresh.
195 if ($updated_po_files) {
197 IkiWiki::disable_commit_hook();
198 IkiWiki::rcs_commit_staged(gettext("updated PO files"),
199 "refreshpofiles", "127.0.0.1");
200 IkiWiki::enable_commit_hook();
201 IkiWiki::rcs_update();
204 IkiWiki::saveindex();
205 # refresh module's private variables
208 foreach my $page (keys %pagesources) {
209 istranslation($page);
214 # make existing translations depend on the corresponding master page
215 foreach my $master (keys %translations) {
216 foreach my $slave (values %{$translations{$master}}) {
217 add_depends($slave, $master);
222 sub mytargetpage ($$) { #{{{
226 if (istranslation($page)) {
227 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
228 if (! $config{usedirs} || $masterpage eq 'index') {
229 return $masterpage . "." . $lang . "." . $ext;
232 return $masterpage . "/index." . $lang . "." . $ext;
235 elsif (istranslatable($page)) {
236 if (! $config{usedirs} || $page eq 'index') {
237 return $page . "." . $config{po_master_language}{code} . "." . $ext;
240 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
243 return $origsubs{'targetpage'}->($page, $ext);
246 sub mybeautify_urlpath ($) { #{{{
248 my $res=$origsubs{'beautify_urlpath'}->($url);
249 if ($config{po_link_to} eq "negotiated") {
250 $res =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
255 sub urlto_with_orig_beautiful_urlpath($$) { #{{{
259 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
260 my $res=urlto($to, $from);
261 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
266 sub mybestlink ($$) { #{{{
269 my $res=$origsubs{'bestlink'}->($page, $link);
271 if ($config{po_link_to} eq "current"
272 && istranslatable($res)
273 && istranslation($page)) {
274 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
275 return $res . "." . $curlang;
284 # We use filter to convert PO to the master page's type,
285 # since other plugins should not work on PO files
286 sub filter (@) { #{{{
288 my $page = $params{page};
289 my $destpage = $params{destpage};
290 my $content = decode_utf8(encode_utf8($params{content}));
292 # decide if this is a PO file that should be converted into a
293 # translated document, and perform various sanity checks
294 if (! istranslation($page) || $filtered{$page}{$destpage}) {
298 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
299 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
300 my $masterfile = srcfile($pagesources{$masterpage});
303 push @masters,$masterfile;
305 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
307 my $doc=Locale::Po4a::Chooser::new('text',%options);
309 'po_in_name' => \@pos,
310 'file_in_name' => \@masters,
311 'file_in_charset' => 'utf-8',
312 'file_out_charset' => 'utf-8',
313 ) or error("[po/filter:$file]: failed to translate");
314 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
315 my $tmpout = $tmpfh->filename;
316 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
317 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
318 $filtered{$page}{$destpage}=1;
322 sub htmlize (@) { #{{{
324 my $page = $params{page};
325 my $content = $params{content};
326 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
327 my $masterfile = srcfile($pagesources{$masterpage});
329 # force content to be htmlize'd as if it was the same type as the master page
330 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
333 sub percenttranslated ($) { #{{{
335 return "N/A" unless (istranslation($page));
336 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
337 my $file=srcfile($pagesources{$page});
338 my $masterfile = srcfile($pagesources{$masterpage});
341 push @masters,$masterfile;
343 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
345 my $doc=Locale::Po4a::Chooser::new('text',%options);
347 'po_in_name' => \@pos,
348 'file_in_name' => \@masters,
349 'file_in_charset' => 'utf-8',
350 'file_out_charset' => 'utf-8',
351 ) or error("[po/percenttranslated:$file]: failed to translate");
352 my ($percent,$hit,$queries) = $doc->stats();
356 sub otherlanguages ($) { #{{{
359 if (istranslatable($page)) {
360 foreach my $lang (sort keys %{$translations{$page}}) {
361 my $translation = $translations{$page}{$lang};
363 url => urlto($translation, $page),
365 language => $config{po_slave_languages}{$lang},
366 percent => percenttranslated($translation),
370 elsif (istranslation($page)) {
371 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
373 url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
374 code => $config{po_master_language}{code},
375 language => $config{po_master_language}{name},
378 foreach my $lang (sort keys %{$translations{$masterpage}}) {
380 url => urlto($translations{$masterpage}{$lang}, $page),
382 language => $config{po_slave_languages}{$lang},
383 percent => percenttranslated($translations{$masterpage}{$lang}),
384 } unless ($lang eq $curlang);
390 sub pagetemplate (@) { #{{{
392 my $page=$params{page};
393 my $destpage=$params{destpage};
394 my $template=$params{template};
396 if (istranslation($page) && $template->query(name => "percenttranslated")) {
397 $template->param(percenttranslated => percenttranslated($page));
399 if ($template->query(name => "istranslation")) {
400 $template->param(istranslation => istranslation($page));
402 if ($template->query(name => "istranslatable")) {
403 $template->param(istranslatable => istranslatable($page));
405 if ($template->query(name => "otherlanguages")) {
406 $template->param(otherlanguages => [otherlanguages($page)]);
407 if (istranslatable($page)) {
408 foreach my $translation (values %{$translations{$page}}) {
409 add_depends($page, $translation);
412 elsif (istranslation($page)) {
413 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
414 add_depends($page, $masterpage);
415 foreach my $translation (values %{$translations{$masterpage}}) {
416 add_depends($page, $translation);
420 # Rely on IkiWiki::Render's genpage() to decide wether
421 # a discussion link should appear on $page; this is not
422 # totally accurate, though: some broken links may be generated
423 # when cgiurl is disabled.
424 # This compromise avoids some code duplication, and will probably
425 # prevent future breakage when ikiwiki internals change.
426 # Known limitations are preferred to future random bugs.
427 if ($template->param('discussionlink') && istranslation($page)) {
428 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
429 $template->param('discussionlink' => htmllink(
432 $masterpage . '/' . gettext("Discussion"),
435 linktext => gettext("Discussion"),
440 sub istranslatable ($) { #{{{
442 my $file=$pagesources{$page};
445 || (defined pagetype($file) && pagetype($file) eq 'po')
446 || $file =~ /\.pot$/) {
449 return pagespec_match($page, $config{po_translatable_pages});
452 sub _istranslation ($) { #{{{
454 my $file=$pagesources{$page};
455 if (! defined $file) {
456 return IkiWiki::FailReason->new("no file specified");
460 || ! defined pagetype($file)
461 || ! pagetype($file) eq 'po'
462 || $file =~ /\.pot$/) {
466 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
467 if (! defined $masterpage || ! defined $lang
468 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
469 || ! defined $pagesources{$masterpage}
470 || ! defined $config{po_slave_languages}{$lang}) {
474 return istranslatable($masterpage);
477 sub istranslation ($) { #{{{
479 if (_istranslation($page)) {
480 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
481 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
487 package IkiWiki::PageSpec;
492 sub match_istranslation ($;@) { #{{{
494 if (IkiWiki::Plugin::po::istranslation($page)) {
495 return IkiWiki::SuccessReason->new("is a translation page");
498 return IkiWiki::FailReason->new("is not a translation page");
502 sub match_istranslatable ($;@) { #{{{
504 if (IkiWiki::Plugin::po::istranslatable($page)) {
505 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
508 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
512 sub match_lang ($$;@) { #{{{
515 my $regexp=IkiWiki::glob2re($wanted);
519 if (IkiWiki::Plugin::po::istranslation($page)) {
520 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
523 $lang = $config{po_master_language}{code};
526 if ($lang!~/^$regexp$/i) {
527 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
530 return IkiWiki::SuccessReason->new("file language is $wanted");
534 sub match_currentlang ($$;@) { #{{{
538 my ($currentmasterpage, $currentlang, $masterpage, $lang);
540 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
542 if (IkiWiki::Plugin::po::istranslation($params{location})) {
543 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
546 $currentlang = $config{po_master_language}{code};
549 if (IkiWiki::Plugin::po::istranslation($page)) {
550 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
553 $lang = $config{po_master_language}{code};
556 if ($lang eq $currentlang) {
557 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
560 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");