]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/po.pm
98c070481978a560d328ecc107dac4396d3a341e
[ikiwiki.git] / IkiWiki / Plugin / po.pm
1 #!/usr/bin/perl
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;
6
7 use warnings;
8 use strict;
9 use IkiWiki 2.00;
10 use Encode;
11 use Locale::Po4a::Chooser;
12 use File::Basename;
13 use File::Copy;
14 use File::Spec;
15 use File::Temp;
16 use Memoize;
17
18 my %translations;
19 memoize("istranslatable");
20 memoize("_istranslation");
21 memoize("percenttranslated");
22
23 sub import {
24         hook(type => "getsetup", id => "po", call => \&getsetup);
25         hook(type => "checkconfig", id => "po", call => \&checkconfig);
26         hook(type => "needsbuild", id => "po", call => \&needsbuild);
27         hook(type => "targetpage", id => "po", call => \&targetpage);
28         hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
29         hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
30         hook(type => "filter", id => "po", call => \&filter);
31         hook(type => "htmlize", id => "po", call => \&htmlize);
32         hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
33 }
34
35 sub getsetup () { #{{{
36         return
37                 plugin => {
38                         safe => 0,
39                         rebuild => 1, # format plugin
40                 },
41                 po_master_language => {
42                         type => "string",
43                         example => {
44                                 'code' => 'en',
45                                 'name' => 'English'
46                         },
47                         description => "master language (non-PO files)",
48                         safe => 1,
49                         rebuild => 1,
50                 },
51                 po_slave_languages => {
52                         type => "string",
53                         example => {
54                                 'fr' => 'Français',
55                                 'es' => 'Castellano',
56                                 'de' => 'Deutsch'
57                         },
58                         description => "slave languages (PO files)",
59                         safe => 1,
60                         rebuild => 1,
61                 },
62                 po_translatable_pages => {
63                         type => "pagespec",
64                         example => "!*/Discussion",
65                         description => "PageSpec controlling which pages are translatable",
66                         link => "ikiwiki/PageSpec",
67                         safe => 1,
68                         rebuild => 1,
69                 },
70                 po_link_to => {
71                         type => "string",
72                         example => "current",
73                         description => "internal linking behavior (default/current/negotiated)",
74                         safe => 1,
75                         rebuild => 1,
76                 },
77 } #}}}
78
79 sub checkconfig () { #{{{
80         foreach my $field (qw{po_master_language po_slave_languages}) {
81                 if (! exists $config{$field} || ! defined $config{$field}) {
82                         error(sprintf(gettext("Must specify %s"), $field));
83                 }
84         }
85         if (! exists $config{po_link_to} ||
86             ! defined $config{po_link_to}) {
87             $config{po_link_to}="default";
88         }
89         if (! exists $config{po_translatable_pages} ||
90             ! defined $config{po_translatable_pages}) {
91             $config{po_translatable_pages}="";
92         }
93         if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
94                 error(gettext("po_link_to=negotiated requires usedirs to be set"));
95         }
96         push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
97 } #}}}
98
99 sub refreshpot ($) { #{{{
100         my $masterfile=shift;
101         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
102         my $potfile=File::Spec->catfile($dir, $name . ".pot");
103         my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
104         my $doc=Locale::Po4a::Chooser::new('text',%options);
105         $doc->read($masterfile);
106         $doc->{TT}{utf_mode} = 1;
107         $doc->{TT}{file_in_charset} = 'utf-8';
108         $doc->{TT}{file_out_charset} = 'utf-8';
109         $doc->parse;
110         $doc->writepo($potfile);
111 } #}}}
112
113 sub refreshpofiles ($@) { #{{{
114         my $masterfile=shift;
115         my @pofiles=@_;
116
117         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
118         my $potfile=File::Spec->catfile($dir, $name . ".pot");
119         error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
120
121         foreach my $pofile (@pofiles) {
122                 if (-e $pofile) {
123                         my $cmd = "msgmerge -U $pofile $potfile";
124                         system ($cmd) == 0
125                                 or error("[po/refreshpofiles:$pofile] failed to update");
126                 }
127                 else {
128                         File::Copy::syscopy($potfile,$pofile)
129                                 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
130                 }
131         }
132 } #}}}
133
134 sub needsbuild () { #{{{
135         my $needsbuild=shift;
136
137         # build %translations, using istranslation's side-effect
138         foreach my $page (keys %pagesources) {
139                 istranslation($page);
140         }
141
142         # refresh POT and PO files as needed
143         foreach my $file (@$needsbuild) {
144                 my $page=pagename($file);
145                 if (istranslatable($page)) {
146                         refreshpot(srcfile($file));
147                         my @pofiles;
148                         foreach my $lang (keys %{$translations{$page}}) {
149                                 push @pofiles, $pagesources{$translations{$page}{$lang}};
150                         }
151                         refreshpofiles(srcfile($file), map { srcfile($_) } @pofiles);
152                 }
153         }
154
155         # make existing translations depend on the corresponding master page
156         foreach my $master (keys %translations) {
157                 foreach my $slave (values %{$translations{$master}}) {
158                         add_depends($slave, $master);
159                 }
160         }
161 } #}}}
162
163 sub targetpage (@) { #{{{
164         my %params = @_;
165         my $page=$params{page};
166         my $ext=$params{ext};
167
168         if (istranslation($page)) {
169                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
170                 if (! $config{usedirs} || $page eq 'index') {
171                         return $masterpage . "." . $lang . "." . $ext;
172                 }
173                 else {
174                         return $masterpage . "/index." . $lang . "." . $ext;
175                 }
176         }
177         elsif (istranslatable($page)) {
178                 if (! $config{usedirs} || $page eq 'index') {
179                         return $page . "." . $config{po_master_language}{code} . "." . $ext;
180                 }
181                 else {
182                         return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
183                 }
184         }
185         return;
186 } #}}}
187
188 sub tweakurlpath ($) { #{{{
189         my %params = @_;
190         my $url=$params{url};
191         if ($config{po_link_to} eq "negotiated") {
192                 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
193         }
194         return $url;
195 } #}}}
196
197 sub tweakbestlink ($$) { #{{{
198         my %params = @_;
199         my $page=$params{page};
200         my $link=$params{link};
201         if ($config{po_link_to} eq "current"
202             && istranslatable($link)
203             && istranslation($page)) {
204                 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
205                 return $link . "." . $curlang;
206         }
207         return $link;
208 } #}}}
209
210 our %filtered;
211 # We use filter to convert PO to the master page's type,
212 # since other plugins should not work on PO files
213 sub filter (@) { #{{{
214         my %params = @_;
215         my $page = $params{page};
216         my $destpage = $params{destpage};
217         my $content = decode_utf8(encode_utf8($params{content}));
218
219         # decide if this is a PO file that should be converted into a translated document,
220         # and perform various sanity checks
221         if (! istranslation($page) || $filtered{$page}{$destpage}) {
222                 return $content;
223         }
224
225         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
226         my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
227         my $masterfile = srcfile($pagesources{$masterpage});
228         my (@pos,@masters);
229         push @pos,$file;
230         push @masters,$masterfile;
231         my %options = (
232                         "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
233                         );
234         my $doc=Locale::Po4a::Chooser::new('text',%options);
235         $doc->process(
236                 'po_in_name'    => \@pos,
237                 'file_in_name'  => \@masters,
238                 'file_in_charset'  => 'utf-8',
239                 'file_out_charset' => 'utf-8',
240         ) or error("[po/filter:$file]: failed to translate");
241         my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
242         my $tmpout = $tmpfh->filename;
243         $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
244         $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
245         $filtered{$page}{$destpage}=1;
246         return $content;
247 } #}}}
248
249 sub htmlize (@) { #{{{
250         my %params=@_;
251         my $page = $params{page};
252         my $content = $params{content};
253         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
254         my $masterfile = srcfile($pagesources{$masterpage});
255
256         # force content to be htmlize'd as if it was the same type as the master page
257         return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
258 } #}}}
259
260 sub percenttranslated ($) { #{{{
261         my $page=shift;
262         return "N/A" unless (istranslation($page));
263         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
264         my $file=srcfile($pagesources{$page});
265         my $masterfile = srcfile($pagesources{$masterpage});
266         my (@pos,@masters);
267         push @pos,$file;
268         push @masters,$masterfile;
269         my %options = (
270                         "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
271                         );
272         my $doc=Locale::Po4a::Chooser::new('text',%options);
273         $doc->process(
274                 'po_in_name'    => \@pos,
275                 'file_in_name'  => \@masters,
276                 'file_in_charset'  => 'utf-8',
277                 'file_out_charset' => 'utf-8',
278         ) or error("[po/percenttranslated:$file]: failed to translate");
279         my ($percent,$hit,$queries) = $doc->stats();
280         return $percent;
281 } #}}}
282
283 sub otherlanguages ($) { #{{{
284         my $page=shift;
285         my @ret;
286         if (istranslatable($page)) {
287                 foreach my $lang (sort keys %{$translations{$page}}) {
288                         my $translation = $translations{$page}{$lang};
289                         push @ret, {
290                                 url => urlto($translation, $page),
291                                 code => $lang,
292                                 language => $config{po_slave_languages}{$lang},
293                                 percent => percenttranslated($translation),
294                         };
295                 }
296         }
297         elsif (istranslation($page)) {
298                 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
299                 push @ret, {
300                         url => urlto($masterpage, $page),
301                         code => $config{po_master_language}{code},
302                         language => $config{po_master_language}{name},
303                         master => 1,
304                 };
305                 foreach my $lang (sort keys %{$translations{$masterpage}}) {
306                         push @ret, {
307                                 url => urlto($translations{$masterpage}{$lang}, $page),
308                                 code => $lang,
309                                 language => $config{po_slave_languages}{$lang},
310                                 percent => percenttranslated($page),
311                         } unless ($lang eq $curlang);
312                 }
313         }
314         return @ret;
315 } #}}}
316
317 sub pagetemplate (@) { #{{{
318         my %params=@_;
319         my $page=$params{page};
320         my $template=$params{template};
321
322         if (istranslation($page) && $template->query(name => "percenttranslated")) {
323                 $template->param(percenttranslated => percenttranslated($page));
324         }
325         if ($template->query(name => "otherlanguages")) {
326                 $template->param(otherlanguages => [otherlanguages($page)]);
327         }
328 } # }}}
329
330 sub istranslatable ($) { #{{{
331         my $page=shift;
332         my $file=$pagesources{$page};
333
334         if (! defined $file
335             || (defined pagetype($file) && pagetype($file) eq 'po')
336             || $file =~ /\.pot$/) {
337                 return 0;
338         }
339         return pagespec_match($page, $config{po_translatable_pages});
340 } #}}}
341
342 sub _istranslation ($) { #{{{
343         my $page=shift;
344         my $file=$pagesources{$page};
345         if (! defined $file) {
346                 return IkiWiki::FailReason->new("no file specified");
347         }
348
349         if (! defined $file
350             || ! defined pagetype($file)
351             || ! pagetype($file) eq 'po'
352             || $file =~ /\.pot$/) {
353                 return 0;
354         }
355
356         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
357         if (! defined $masterpage || ! defined $lang
358             || ! (length($masterpage) > 0) || ! (length($lang) > 0)
359             || ! defined $pagesources{$masterpage}
360             || ! defined $config{po_slave_languages}{$lang}) {
361                 return 0;
362         }
363
364         return istranslatable($masterpage);
365 } #}}}
366
367 sub istranslation ($) { #{{{
368         my $page=shift;
369         if (_istranslation($page)) {
370                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
371                 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
372                 return 1;
373         }
374         return 0;
375 } #}}}
376
377 package IkiWiki::PageSpec;
378 use warnings;
379 use strict;
380 use IkiWiki 2.00;
381
382 sub match_istranslation ($;@) { #{{{
383         my $page=shift;
384         if (IkiWiki::Plugin::po::istranslation($page)) {
385                 return IkiWiki::SuccessReason->new("is a translation page");
386         }
387         else {
388                 return IkiWiki::FailReason->new("is not a translation page");
389         }
390 } #}}}
391
392 sub match_istranslatable ($;@) { #{{{
393         my $page=shift;
394         if (IkiWiki::Plugin::po::istranslatable($page)) {
395                 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
396         }
397         else {
398                 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
399         }
400 } #}}}
401
402 sub match_lang ($$;@) { #{{{
403         my $page=shift;
404         my $wanted=shift;
405         my $regexp=IkiWiki::glob2re($wanted);
406         my $lang;
407         my $masterpage;
408
409         if (IkiWiki::Plugin::po::istranslation($page)) {
410                 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
411         }
412         else {
413                 $lang = $config{po_master_language}{code};
414         }
415
416         if ($lang!~/^$regexp$/i) {
417                 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
418         }
419         else {
420                 return IkiWiki::SuccessReason->new("file language is $wanted");
421         }
422 } #}}}
423
424 sub match_currentlang ($$;@) { #{{{
425         my $page=shift;
426         shift;
427         my %params=@_;
428         my ($currentmasterpage, $currentlang, $masterpage, $lang);
429
430         return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
431
432         if (IkiWiki::Plugin::po::istranslation($params{location})) {
433                 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
434         }
435         else {
436                 $currentlang = $config{po_master_language}{code};
437         }
438
439         if (IkiWiki::Plugin::po::istranslation($page)) {
440                 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
441         }
442         else {
443                 $lang = $config{po_master_language}{code};
444         }
445
446         if ($lang eq $currentlang) {
447                 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
448         }
449         else {
450                 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
451         }
452 } #}}}
453
454 1