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;
21 ## FIXME: makes some test cases cry once every two tries; this may be
22 ## related to the artificial way the testsuite is run, or not.
23 # memoize("istranslatable");
24 memoize("_istranslation");
25 memoize("percenttranslated");
28 hook(type => "getsetup", id => "po", call => \&getsetup);
29 hook(type => "checkconfig", id => "po", call => \&checkconfig);
30 hook(type => "needsbuild", id => "po", call => \&needsbuild);
31 hook(type => "targetpage", id => "po", call => \&targetpage);
32 hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
33 hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
34 hook(type => "filter", id => "po", call => \&filter);
35 hook(type => "htmlize", id => "po", call => \&htmlize);
36 hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
39 sub getsetup () { #{{{
43 rebuild => 1, # format plugin
45 po_master_language => {
51 description => "master language (non-PO files)",
55 po_slave_languages => {
62 description => "slave languages (PO files)",
66 po_translatable_pages => {
68 example => "!*/Discussion",
69 description => "PageSpec controlling which pages are translatable",
70 link => "ikiwiki/PageSpec",
77 description => "internal linking behavior (default/current/negotiated)",
83 sub checkconfig () { #{{{
84 foreach my $field (qw{po_master_language po_slave_languages}) {
85 if (! exists $config{$field} || ! defined $config{$field}) {
86 error(sprintf(gettext("Must specify %s"), $field));
89 if (! exists $config{po_link_to} ||
90 ! defined $config{po_link_to}) {
91 $config{po_link_to}="default";
93 if (! exists $config{po_translatable_pages} ||
94 ! defined $config{po_translatable_pages}) {
95 $config{po_translatable_pages}="";
97 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
98 error(gettext("po_link_to=negotiated requires usedirs to be set"));
100 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
103 sub potfile ($) { #{{{
104 my $masterfile=shift;
105 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
106 return File::Spec->catfile($dir, $name . ".pot");
109 sub pofile ($$) { #{{{
110 my $masterfile=shift;
112 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
113 return File::Spec->catfile($dir, $name . "." . $lang . ".po");
116 sub refreshpot ($) { #{{{
117 my $masterfile=shift;
118 my $potfile=potfile($masterfile);
119 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
120 my $doc=Locale::Po4a::Chooser::new('text',%options);
121 $doc->read($masterfile);
122 $doc->{TT}{utf_mode} = 1;
123 $doc->{TT}{file_in_charset} = 'utf-8';
124 $doc->{TT}{file_out_charset} = 'utf-8';
125 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
126 # this is undocument use of internal Locale::Po4a::TransTractor's data,
127 # compulsory since this module prevents us from using the porefs option.
128 my %po_options = ('porefs' => 'none');
129 $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
132 $doc->writepo($potfile);
135 sub refreshpofiles ($@) { #{{{
136 my $masterfile=shift;
139 my $potfile=potfile($masterfile);
140 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
142 foreach my $pofile (@pofiles) {
144 my $cmd = "msgmerge -U --backup=none $pofile $potfile";
146 or error("[po/refreshpofiles:$pofile] failed to update");
149 File::Copy::syscopy($potfile,$pofile)
150 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
155 sub needsbuild () { #{{{
156 my $needsbuild=shift;
158 # build %translations, using istranslation's side-effect
159 foreach my $page (keys %pagesources) {
160 istranslation($page);
163 # refresh/create POT and PO files as needed
164 my $updated_po_files=0;
165 foreach my $page (keys %pagesources) {
166 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
167 if (istranslatable($page)) {
168 my $file=srcfile($pagesources{$page});
169 if ($pageneedsbuild || ! -e potfile($file)) {
173 foreach my $lang (keys %{$config{po_slave_languages}}) {
174 my $pofile=pofile($file, $lang);
175 if ($pageneedsbuild || ! -e $pofile) {
176 push @pofiles, $pofile;
180 refreshpofiles($file, @pofiles) ;
181 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
182 $updated_po_files = 1;
187 # check staged changes in and trigger a wiki refresh.
188 if ($updated_po_files) {
190 IkiWiki::disable_commit_hook();
191 IkiWiki::rcs_commit_staged(gettext("updated PO files"),
192 "refreshpofiles", "127.0.0.1");
193 IkiWiki::enable_commit_hook();
194 IkiWiki::rcs_update();
197 IkiWiki::saveindex();
198 # refresh module's private variables
201 foreach my $page (keys %pagesources) {
202 istranslation($page);
207 # make existing translations depend on the corresponding master page
208 foreach my $master (keys %translations) {
209 foreach my $slave (values %{$translations{$master}}) {
210 add_depends($slave, $master);
215 sub targetpage (@) { #{{{
217 my $page=$params{page};
218 my $ext=$params{ext};
220 if (istranslation($page)) {
221 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
222 if (! $config{usedirs} || $page eq 'index') {
223 return $masterpage . "." . $lang . "." . $ext;
226 return $masterpage . "/index." . $lang . "." . $ext;
229 elsif (istranslatable($page)) {
230 if (! $config{usedirs} || $page eq 'index') {
231 return $page . "." . $config{po_master_language}{code} . "." . $ext;
234 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
240 sub tweakurlpath ($) { #{{{
242 my $url=$params{url};
243 if ($config{po_link_to} eq "negotiated") {
244 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
249 sub tweakbestlink ($$) { #{{{
251 my $page=$params{page};
252 my $link=$params{link};
253 if ($config{po_link_to} eq "current"
254 && istranslatable($link)
255 && istranslation($page)) {
256 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
257 return $link . "." . $curlang;
262 # We use filter to convert PO to the master page's type,
263 # since other plugins should not work on PO files
264 sub filter (@) { #{{{
266 my $page = $params{page};
267 my $destpage = $params{destpage};
268 my $content = decode_utf8(encode_utf8($params{content}));
270 # decide if this is a PO file that should be converted into a translated document,
271 # and perform various sanity checks
272 if (! istranslation($page) || $filtered{$page}{$destpage}) {
276 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
277 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
278 my $masterfile = srcfile($pagesources{$masterpage});
281 push @masters,$masterfile;
283 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
285 my $doc=Locale::Po4a::Chooser::new('text',%options);
287 'po_in_name' => \@pos,
288 'file_in_name' => \@masters,
289 'file_in_charset' => 'utf-8',
290 'file_out_charset' => 'utf-8',
291 ) or error("[po/filter:$file]: failed to translate");
292 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
293 my $tmpout = $tmpfh->filename;
294 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
295 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
296 $filtered{$page}{$destpage}=1;
300 sub htmlize (@) { #{{{
302 my $page = $params{page};
303 my $content = $params{content};
304 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
305 my $masterfile = srcfile($pagesources{$masterpage});
307 # force content to be htmlize'd as if it was the same type as the master page
308 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
311 sub percenttranslated ($) { #{{{
313 return "N/A" unless (istranslation($page));
314 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
315 my $file=srcfile($pagesources{$page});
316 my $masterfile = srcfile($pagesources{$masterpage});
319 push @masters,$masterfile;
321 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
323 my $doc=Locale::Po4a::Chooser::new('text',%options);
325 'po_in_name' => \@pos,
326 'file_in_name' => \@masters,
327 'file_in_charset' => 'utf-8',
328 'file_out_charset' => 'utf-8',
329 ) or error("[po/percenttranslated:$file]: failed to translate");
330 my ($percent,$hit,$queries) = $doc->stats();
334 sub otherlanguages ($) { #{{{
337 if (istranslatable($page)) {
338 foreach my $lang (sort keys %{$translations{$page}}) {
339 my $translation = $translations{$page}{$lang};
341 url => urlto($translation, $page),
343 language => $config{po_slave_languages}{$lang},
344 percent => percenttranslated($translation),
348 elsif (istranslation($page)) {
349 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
351 url => urlto($masterpage, $page),
352 code => $config{po_master_language}{code},
353 language => $config{po_master_language}{name},
356 foreach my $lang (sort keys %{$translations{$masterpage}}) {
358 url => urlto($translations{$masterpage}{$lang}, $page),
360 language => $config{po_slave_languages}{$lang},
361 percent => percenttranslated($translations{$masterpage}{$lang}),
362 } unless ($lang eq $curlang);
368 sub pagetemplate (@) { #{{{
370 my $page=$params{page};
371 my $template=$params{template};
373 if (istranslation($page) && $template->query(name => "percenttranslated")) {
374 $template->param(percenttranslated => percenttranslated($page));
376 if ($template->query(name => "istranslation")) {
377 $template->param(istranslation => istranslation($page));
379 if ($template->query(name => "istranslatable")) {
380 $template->param(istranslatable => istranslatable($page));
382 if ($template->query(name => "otherlanguages")) {
383 $template->param(otherlanguages => [otherlanguages($page)]);
384 if (istranslatable($page)) {
385 foreach my $translation (values %{$translations{$page}}) {
386 add_depends($page, $translation);
389 elsif (istranslation($page)) {
390 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
391 add_depends($page, $masterpage);
392 foreach my $translation (values %{$translations{$masterpage}}) {
393 add_depends($page, $translation);
399 sub istranslatable ($) { #{{{
401 my $file=$pagesources{$page};
404 || (defined pagetype($file) && pagetype($file) eq 'po')
405 || $file =~ /\.pot$/) {
408 return pagespec_match($page, $config{po_translatable_pages});
411 sub _istranslation ($) { #{{{
413 my $file=$pagesources{$page};
414 if (! defined $file) {
415 return IkiWiki::FailReason->new("no file specified");
419 || ! defined pagetype($file)
420 || ! pagetype($file) eq 'po'
421 || $file =~ /\.pot$/) {
425 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
426 if (! defined $masterpage || ! defined $lang
427 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
428 || ! defined $pagesources{$masterpage}
429 || ! defined $config{po_slave_languages}{$lang}) {
433 return istranslatable($masterpage);
436 sub istranslation ($) { #{{{
438 if (_istranslation($page)) {
439 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
440 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
446 package IkiWiki::PageSpec;
451 sub match_istranslation ($;@) { #{{{
453 if (IkiWiki::Plugin::po::istranslation($page)) {
454 return IkiWiki::SuccessReason->new("is a translation page");
457 return IkiWiki::FailReason->new("is not a translation page");
461 sub match_istranslatable ($;@) { #{{{
463 if (IkiWiki::Plugin::po::istranslatable($page)) {
464 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
467 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
471 sub match_lang ($$;@) { #{{{
474 my $regexp=IkiWiki::glob2re($wanted);
478 if (IkiWiki::Plugin::po::istranslation($page)) {
479 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
482 $lang = $config{po_master_language}{code};
485 if ($lang!~/^$regexp$/i) {
486 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
489 return IkiWiki::SuccessReason->new("file language is $wanted");
493 sub match_currentlang ($$;@) { #{{{
497 my ($currentmasterpage, $currentlang, $masterpage, $lang);
499 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
501 if (IkiWiki::Plugin::po::istranslation($params{location})) {
502 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
505 $currentlang = $config{po_master_language}{code};
508 if (IkiWiki::Plugin::po::istranslation($page)) {
509 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
512 $lang = $config{po_master_language}{code};
515 if ($lang eq $currentlang) {
516 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
519 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");