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