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;
34 hook(type => "getsetup", id => "po", call => \&getsetup);
35 hook(type => "checkconfig", id => "po", call => \&checkconfig);
36 hook(type => "needsbuild", id => "po", call => \&needsbuild);
37 hook(type => "targetpage", id => "po", call => \&targetpage);
38 hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
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 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
43 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
46 sub getsetup () { #{{{
50 rebuild => 1, # format plugin
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);
139 $doc->writepo($potfile);
142 sub refreshpofiles ($@) { #{{{
143 my $masterfile=shift;
146 my $potfile=potfile($masterfile);
147 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
149 foreach my $pofile (@pofiles) {
151 my $cmd = "msgmerge -U --backup=none $pofile $potfile";
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 targetpage (@) { #{{{
224 my $page=$params{page};
225 my $ext=$params{ext};
227 if (istranslation($page)) {
228 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
229 if (! $config{usedirs} || $page eq 'index') {
230 return $masterpage . "." . $lang . "." . $ext;
233 return $masterpage . "/index." . $lang . "." . $ext;
236 elsif (istranslatable($page)) {
237 if (! $config{usedirs} || $page eq 'index') {
238 return $page . "." . $config{po_master_language}{code} . "." . $ext;
241 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
247 sub mybeautify_urlpath ($) { #{{{
249 my $res=$origsubs{'beautify_urlpath'}->($url);
250 if ($config{po_link_to} eq "negotiated") {
251 $res =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
256 sub mybestlink ($$) { #{{{
259 my $res=$origsubs{'bestlink'}->($page, $link);
261 if ($config{po_link_to} eq "current"
262 && istranslatable($res)
263 && istranslation($page)) {
264 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
265 return $res . "." . $curlang;
274 # We use filter to convert PO to the master page's type,
275 # since other plugins should not work on PO files
276 sub filter (@) { #{{{
278 my $page = $params{page};
279 my $destpage = $params{destpage};
280 my $content = decode_utf8(encode_utf8($params{content}));
282 # decide if this is a PO file that should be converted into a translated document,
283 # and perform various sanity checks
284 if (! istranslation($page) || $filtered{$page}{$destpage}) {
288 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
289 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
290 my $masterfile = srcfile($pagesources{$masterpage});
293 push @masters,$masterfile;
295 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
297 my $doc=Locale::Po4a::Chooser::new('text',%options);
299 'po_in_name' => \@pos,
300 'file_in_name' => \@masters,
301 'file_in_charset' => 'utf-8',
302 'file_out_charset' => 'utf-8',
303 ) or error("[po/filter:$file]: failed to translate");
304 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
305 my $tmpout = $tmpfh->filename;
306 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
307 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
308 $filtered{$page}{$destpage}=1;
312 sub htmlize (@) { #{{{
314 my $page = $params{page};
315 my $content = $params{content};
316 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
317 my $masterfile = srcfile($pagesources{$masterpage});
319 # force content to be htmlize'd as if it was the same type as the master page
320 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
323 sub percenttranslated ($) { #{{{
325 return "N/A" unless (istranslation($page));
326 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
327 my $file=srcfile($pagesources{$page});
328 my $masterfile = srcfile($pagesources{$masterpage});
331 push @masters,$masterfile;
333 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
335 my $doc=Locale::Po4a::Chooser::new('text',%options);
337 'po_in_name' => \@pos,
338 'file_in_name' => \@masters,
339 'file_in_charset' => 'utf-8',
340 'file_out_charset' => 'utf-8',
341 ) or error("[po/percenttranslated:$file]: failed to translate");
342 my ($percent,$hit,$queries) = $doc->stats();
346 sub otherlanguages ($) { #{{{
349 if (istranslatable($page)) {
350 foreach my $lang (sort keys %{$translations{$page}}) {
351 my $translation = $translations{$page}{$lang};
353 url => urlto($translation, $page),
355 language => $config{po_slave_languages}{$lang},
356 percent => percenttranslated($translation),
360 elsif (istranslation($page)) {
361 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
363 url => urlto($masterpage, $page),
364 code => $config{po_master_language}{code},
365 language => $config{po_master_language}{name},
368 foreach my $lang (sort keys %{$translations{$masterpage}}) {
370 url => urlto($translations{$masterpage}{$lang}, $page),
372 language => $config{po_slave_languages}{$lang},
373 percent => percenttranslated($translations{$masterpage}{$lang}),
374 } unless ($lang eq $curlang);
380 sub pagetemplate (@) { #{{{
382 my $page=$params{page};
383 my $template=$params{template};
385 if (istranslation($page) && $template->query(name => "percenttranslated")) {
386 $template->param(percenttranslated => percenttranslated($page));
388 if ($template->query(name => "istranslation")) {
389 $template->param(istranslation => istranslation($page));
391 if ($template->query(name => "istranslatable")) {
392 $template->param(istranslatable => istranslatable($page));
394 if ($template->query(name => "otherlanguages")) {
395 $template->param(otherlanguages => [otherlanguages($page)]);
396 if (istranslatable($page)) {
397 foreach my $translation (values %{$translations{$page}}) {
398 add_depends($page, $translation);
401 elsif (istranslation($page)) {
402 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
403 add_depends($page, $masterpage);
404 foreach my $translation (values %{$translations{$masterpage}}) {
405 add_depends($page, $translation);
411 sub istranslatable ($) { #{{{
413 my $file=$pagesources{$page};
416 || (defined pagetype($file) && pagetype($file) eq 'po')
417 || $file =~ /\.pot$/) {
420 return pagespec_match($page, $config{po_translatable_pages});
423 sub _istranslation ($) { #{{{
425 my $file=$pagesources{$page};
426 if (! defined $file) {
427 return IkiWiki::FailReason->new("no file specified");
431 || ! defined pagetype($file)
432 || ! pagetype($file) eq 'po'
433 || $file =~ /\.pot$/) {
437 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
438 if (! defined $masterpage || ! defined $lang
439 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
440 || ! defined $pagesources{$masterpage}
441 || ! defined $config{po_slave_languages}{$lang}) {
445 return istranslatable($masterpage);
448 sub istranslation ($) { #{{{
450 if (_istranslation($page)) {
451 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
452 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
458 package IkiWiki::PageSpec;
463 sub match_istranslation ($;@) { #{{{
465 if (IkiWiki::Plugin::po::istranslation($page)) {
466 return IkiWiki::SuccessReason->new("is a translation page");
469 return IkiWiki::FailReason->new("is not a translation page");
473 sub match_istranslatable ($;@) { #{{{
475 if (IkiWiki::Plugin::po::istranslatable($page)) {
476 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
479 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
483 sub match_lang ($$;@) { #{{{
486 my $regexp=IkiWiki::glob2re($wanted);
490 if (IkiWiki::Plugin::po::istranslation($page)) {
491 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
494 $lang = $config{po_master_language}{code};
497 if ($lang!~/^$regexp$/i) {
498 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
501 return IkiWiki::SuccessReason->new("file language is $wanted");
505 sub match_currentlang ($$;@) { #{{{
509 my ($currentmasterpage, $currentlang, $masterpage, $lang);
511 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
513 if (IkiWiki::Plugin::po::istranslation($params{location})) {
514 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
517 $currentlang = $config{po_master_language}{code};
520 if (IkiWiki::Plugin::po::istranslation($page)) {
521 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
524 $lang = $config{po_master_language}{code};
527 if ($lang eq $currentlang) {
528 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
531 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");