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;
19 memoize("istranslatable");
20 memoize("_istranslation");
21 memoize("percenttranslated");
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);
35 sub getsetup () { #{{{
39 rebuild => 1, # format plugin
41 po_master_language => {
47 description => "master language (non-PO files)",
51 po_slave_languages => {
58 description => "slave languages (PO files)",
62 po_translatable_pages => {
64 example => "!*/Discussion",
65 description => "PageSpec controlling which pages are translatable",
66 link => "ikiwiki/PageSpec",
73 description => "internal linking behavior (default/current/negotiated)",
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));
85 if (! exists $config{po_link_to} ||
86 ! defined $config{po_link_to}) {
87 $config{po_link_to}="default";
89 if (! exists $config{po_translatable_pages} ||
90 ! defined $config{po_translatable_pages}) {
91 $config{po_translatable_pages}="";
93 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
94 error(gettext("po_link_to=negotiated requires usedirs to be set"));
96 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
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';
110 $doc->writepo($potfile);
113 sub refreshpofiles ($@) { #{{{
114 my $masterfile=shift;
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);
121 foreach my $pofile (@pofiles) {
123 my $cmd = "msgmerge -U $pofile $potfile";
125 or error("[po/refreshpofiles:$pofile] failed to update");
128 File::Copy::syscopy($potfile,$pofile)
129 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
134 sub needsbuild () { #{{{
135 my $needsbuild=shift;
137 # build %translations, using istranslation's side-effect
138 foreach my $page (keys %pagesources) {
139 istranslation($page);
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));
148 foreach my $lang (keys %{$translations{$page}}) {
149 push @pofiles, $pagesources{$translations{$page}{$lang}};
151 refreshpofiles(srcfile($file), map { srcfile($_) } @pofiles);
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);
163 sub targetpage (@) { #{{{
165 my $page=$params{page};
166 my $ext=$params{ext};
168 if (istranslation($page)) {
169 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
170 if (! $config{usedirs} || $page eq 'index') {
171 return $masterpage . "." . $lang . "." . $ext;
174 return $masterpage . "/index." . $lang . "." . $ext;
177 elsif (istranslatable($page)) {
178 if (! $config{usedirs} || $page eq 'index') {
179 return $page . "." . $config{po_master_language}{code} . "." . $ext;
182 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
188 sub tweakurlpath ($) { #{{{
190 my $url=$params{url};
191 if ($config{po_link_to} eq "negotiated") {
192 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
197 sub tweakbestlink ($$) { #{{{
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;
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 (@) { #{{{
215 my $page = $params{page};
216 my $destpage = $params{destpage};
217 my $content = decode_utf8(encode_utf8($params{content}));
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}) {
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});
230 push @masters,$masterfile;
232 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
234 my $doc=Locale::Po4a::Chooser::new('text',%options);
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;
249 sub htmlize (@) { #{{{
251 my $page = $params{page};
252 my $content = $params{content};
253 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
254 my $masterfile = srcfile($pagesources{$masterpage});
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);
260 sub percenttranslated ($) { #{{{
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});
268 push @masters,$masterfile;
270 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
272 my $doc=Locale::Po4a::Chooser::new('text',%options);
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();
283 sub otherlanguages ($) { #{{{
286 if (istranslatable($page)) {
287 foreach my $lang (sort keys %{$translations{$page}}) {
288 my $translation = $translations{$page}{$lang};
290 url => urlto($translation, $page),
292 language => $config{po_slave_languages}{$lang},
293 percent => percenttranslated($translation),
297 elsif (istranslation($page)) {
298 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
300 url => urlto($masterpage, $page),
301 code => $config{po_master_language}{code},
302 language => $config{po_master_language}{name},
305 foreach my $lang (sort keys %{$translations{$masterpage}}) {
307 url => urlto($translations{$masterpage}{$lang}, $page),
309 language => $config{po_slave_languages}{$lang},
310 percent => percenttranslated($page),
311 } unless ($lang eq $curlang);
317 sub pagetemplate (@) { #{{{
319 my $page=$params{page};
320 my $template=$params{template};
322 if (istranslation($page) && $template->query(name => "percenttranslated")) {
323 $template->param(percenttranslated => percenttranslated($page));
325 if ($template->query(name => "otherlanguages")) {
326 $template->param(otherlanguages => [otherlanguages($page)]);
330 sub istranslatable ($) { #{{{
332 my $file=$pagesources{$page};
335 || (defined pagetype($file) && pagetype($file) eq 'po')
336 || $file =~ /\.pot$/) {
339 return pagespec_match($page, $config{po_translatable_pages});
342 sub _istranslation ($) { #{{{
344 my $file=$pagesources{$page};
345 if (! defined $file) {
346 return IkiWiki::FailReason->new("no file specified");
350 || ! defined pagetype($file)
351 || ! pagetype($file) eq 'po'
352 || $file =~ /\.pot$/) {
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}) {
364 return istranslatable($masterpage);
367 sub istranslation ($) { #{{{
369 if (_istranslation($page)) {
370 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
371 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
377 package IkiWiki::PageSpec;
382 sub match_istranslation ($;@) { #{{{
384 if (IkiWiki::Plugin::po::istranslation($page)) {
385 return IkiWiki::SuccessReason->new("is a translation page");
388 return IkiWiki::FailReason->new("is not a translation page");
392 sub match_istranslatable ($;@) { #{{{
394 if (IkiWiki::Plugin::po::istranslatable($page)) {
395 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
398 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
402 sub match_lang ($$;@) { #{{{
405 my $regexp=IkiWiki::glob2re($wanted);
409 if (IkiWiki::Plugin::po::istranslation($page)) {
410 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
413 $lang = $config{po_master_language}{code};
416 if ($lang!~/^$regexp$/i) {
417 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
420 return IkiWiki::SuccessReason->new("file language is $wanted");
424 sub match_currentlang ($$;@) { #{{{
428 my ($currentmasterpage, $currentlang, $masterpage, $lang);
430 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
432 if (IkiWiki::Plugin::po::istranslation($params{location})) {
433 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
436 $currentlang = $config{po_master_language}{code};
439 if (IkiWiki::Plugin::po::istranslation($page)) {
440 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
443 $lang = $config{po_master_language}{code};
446 if ($lang eq $currentlang) {
447 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
450 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");