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;
20 memoize("istranslatable");
21 memoize("_istranslation");
22 memoize("percenttranslated");
25 hook(type => "getsetup", id => "po", call => \&getsetup);
26 hook(type => "checkconfig", id => "po", call => \&checkconfig);
27 hook(type => "needsbuild", id => "po", call => \&needsbuild);
28 hook(type => "targetpage", id => "po", call => \&targetpage);
29 hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
30 hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
31 hook(type => "filter", id => "po", call => \&filter);
32 hook(type => "htmlize", id => "po", call => \&htmlize);
33 hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
36 sub getsetup () { #{{{
40 rebuild => 1, # format plugin
42 po_master_language => {
48 description => "master language (non-PO files)",
52 po_slave_languages => {
59 description => "slave languages (PO files)",
63 po_translatable_pages => {
65 example => "!*/Discussion",
66 description => "PageSpec controlling which pages are translatable",
67 link => "ikiwiki/PageSpec",
74 description => "internal linking behavior (default/current/negotiated)",
80 sub checkconfig () { #{{{
81 foreach my $field (qw{po_master_language po_slave_languages}) {
82 if (! exists $config{$field} || ! defined $config{$field}) {
83 error(sprintf(gettext("Must specify %s"), $field));
86 if (! exists $config{po_link_to} ||
87 ! defined $config{po_link_to}) {
88 $config{po_link_to}="default";
90 if (! exists $config{po_translatable_pages} ||
91 ! defined $config{po_translatable_pages}) {
92 $config{po_translatable_pages}="";
94 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
95 error(gettext("po_link_to=negotiated requires usedirs to be set"));
97 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
100 sub potfile ($) { #{{{
101 my $masterfile=shift;
102 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
103 return File::Spec->catfile($dir, $name . ".pot");
106 sub pofile ($$) { #{{{
107 my $masterfile=shift;
109 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
110 return File::Spec->catfile($dir, $name . "." . $lang . ".po");
113 sub refreshpot ($) { #{{{
114 my $masterfile=shift;
115 my $potfile=potfile($masterfile);
116 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
117 my $doc=Locale::Po4a::Chooser::new('text',%options);
118 $doc->read($masterfile);
119 $doc->{TT}{utf_mode} = 1;
120 $doc->{TT}{file_in_charset} = 'utf-8';
121 $doc->{TT}{file_out_charset} = 'utf-8';
123 $doc->writepo($potfile);
126 sub refreshpofiles ($@) { #{{{
127 my $masterfile=shift;
130 my $potfile=potfile($masterfile);
131 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
133 foreach my $pofile (@pofiles) {
135 my $cmd = "msgmerge -U $pofile $potfile";
137 or error("[po/refreshpofiles:$pofile] failed to update");
140 File::Copy::syscopy($potfile,$pofile)
141 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
146 sub needsbuild () { #{{{
147 my $needsbuild=shift;
149 # build %translations, using istranslation's side-effect
150 foreach my $page (keys %pagesources) {
151 istranslation($page);
154 # refresh/create POT and PO files as needed
155 my $updated_po_files=0;
156 foreach my $page (keys %pagesources) {
157 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
158 if (istranslatable($page)) {
159 my $file=srcfile($pagesources{$page});
160 if ($pageneedsbuild || ! -e potfile($file)) {
164 foreach my $lang (keys %{$config{po_slave_languages}}) {
165 my $pofile=pofile($file, $lang);
166 if ($pageneedsbuild || ! -e $pofile) {
167 push @pofiles, $pofile;
171 refreshpofiles($file, @pofiles) ;
172 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
173 $updated_po_files = 1;
178 # check staged changes in and trigger a wiki refresh.
179 if ($updated_po_files) {
181 IkiWiki::disable_commit_hook();
182 IkiWiki::rcs_commit_staged(gettext("updated PO files"),
183 "refreshpofiles", "127.0.0.1");
184 IkiWiki::enable_commit_hook();
185 IkiWiki::rcs_update();
188 IkiWiki::saveindex();
192 # refresh %translations, using istranslation's side-effect
193 # (not needed yet, but when newly created POT/PO files are
194 # added to %pagesources and other data structures, we'll need
196 foreach my $page (keys %pagesources) {
197 istranslation($page);
200 # make existing translations depend on the corresponding master page
201 foreach my $master (keys %translations) {
202 foreach my $slave (values %{$translations{$master}}) {
203 add_depends($slave, $master);
208 sub targetpage (@) { #{{{
210 my $page=$params{page};
211 my $ext=$params{ext};
213 if (istranslation($page)) {
214 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
215 if (! $config{usedirs} || $page eq 'index') {
216 return $masterpage . "." . $lang . "." . $ext;
219 return $masterpage . "/index." . $lang . "." . $ext;
222 elsif (istranslatable($page)) {
223 if (! $config{usedirs} || $page eq 'index') {
224 return $page . "." . $config{po_master_language}{code} . "." . $ext;
227 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
233 sub tweakurlpath ($) { #{{{
235 my $url=$params{url};
236 if ($config{po_link_to} eq "negotiated") {
237 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
242 sub tweakbestlink ($$) { #{{{
244 my $page=$params{page};
245 my $link=$params{link};
246 if ($config{po_link_to} eq "current"
247 && istranslatable($link)
248 && istranslation($page)) {
249 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
250 return $link . "." . $curlang;
255 # We use filter to convert PO to the master page's type,
256 # since other plugins should not work on PO files
257 sub filter (@) { #{{{
259 my $page = $params{page};
260 my $destpage = $params{destpage};
261 my $content = decode_utf8(encode_utf8($params{content}));
263 # decide if this is a PO file that should be converted into a translated document,
264 # and perform various sanity checks
265 if (! istranslation($page) || $filtered{$page}{$destpage}) {
269 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
270 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
271 my $masterfile = srcfile($pagesources{$masterpage});
274 push @masters,$masterfile;
276 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
278 my $doc=Locale::Po4a::Chooser::new('text',%options);
280 'po_in_name' => \@pos,
281 'file_in_name' => \@masters,
282 'file_in_charset' => 'utf-8',
283 'file_out_charset' => 'utf-8',
284 ) or error("[po/filter:$file]: failed to translate");
285 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
286 my $tmpout = $tmpfh->filename;
287 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
288 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
289 $filtered{$page}{$destpage}=1;
293 sub htmlize (@) { #{{{
295 my $page = $params{page};
296 my $content = $params{content};
297 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
298 my $masterfile = srcfile($pagesources{$masterpage});
300 # force content to be htmlize'd as if it was the same type as the master page
301 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
304 sub percenttranslated ($) { #{{{
306 return "N/A" unless (istranslation($page));
307 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
308 my $file=srcfile($pagesources{$page});
309 my $masterfile = srcfile($pagesources{$masterpage});
312 push @masters,$masterfile;
314 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
316 my $doc=Locale::Po4a::Chooser::new('text',%options);
318 'po_in_name' => \@pos,
319 'file_in_name' => \@masters,
320 'file_in_charset' => 'utf-8',
321 'file_out_charset' => 'utf-8',
322 ) or error("[po/percenttranslated:$file]: failed to translate");
323 my ($percent,$hit,$queries) = $doc->stats();
327 sub otherlanguages ($) { #{{{
330 if (istranslatable($page)) {
331 foreach my $lang (sort keys %{$translations{$page}}) {
332 my $translation = $translations{$page}{$lang};
334 url => urlto($translation, $page),
336 language => $config{po_slave_languages}{$lang},
337 percent => percenttranslated($translation),
341 elsif (istranslation($page)) {
342 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
344 url => urlto($masterpage, $page),
345 code => $config{po_master_language}{code},
346 language => $config{po_master_language}{name},
349 foreach my $lang (sort keys %{$translations{$masterpage}}) {
351 url => urlto($translations{$masterpage}{$lang}, $page),
353 language => $config{po_slave_languages}{$lang},
354 percent => percenttranslated($translations{$masterpage}{$lang}),
355 } unless ($lang eq $curlang);
361 sub pagetemplate (@) { #{{{
363 my $page=$params{page};
364 my $template=$params{template};
366 if (istranslation($page) && $template->query(name => "percenttranslated")) {
367 $template->param(percenttranslated => percenttranslated($page));
369 if ($template->query(name => "otherlanguages")) {
370 $template->param(otherlanguages => [otherlanguages($page)]);
371 if (istranslatable($page)) {
372 foreach my $translation (values %{$translations{$page}}) {
373 add_depends($page, $translation);
376 elsif (istranslation($page)) {
377 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
378 add_depends($page, $masterpage);
379 foreach my $translation (values %{$translations{$masterpage}}) {
380 add_depends($page, $translation);
386 sub istranslatable ($) { #{{{
388 my $file=$pagesources{$page};
391 || (defined pagetype($file) && pagetype($file) eq 'po')
392 || $file =~ /\.pot$/) {
395 return pagespec_match($page, $config{po_translatable_pages});
398 sub _istranslation ($) { #{{{
400 my $file=$pagesources{$page};
401 if (! defined $file) {
402 return IkiWiki::FailReason->new("no file specified");
406 || ! defined pagetype($file)
407 || ! pagetype($file) eq 'po'
408 || $file =~ /\.pot$/) {
412 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
413 if (! defined $masterpage || ! defined $lang
414 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
415 || ! defined $pagesources{$masterpage}
416 || ! defined $config{po_slave_languages}{$lang}) {
420 return istranslatable($masterpage);
423 sub istranslation ($) { #{{{
425 if (_istranslation($page)) {
426 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
427 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
433 package IkiWiki::PageSpec;
438 sub match_istranslation ($;@) { #{{{
440 if (IkiWiki::Plugin::po::istranslation($page)) {
441 return IkiWiki::SuccessReason->new("is a translation page");
444 return IkiWiki::FailReason->new("is not a translation page");
448 sub match_istranslatable ($;@) { #{{{
450 if (IkiWiki::Plugin::po::istranslatable($page)) {
451 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
454 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
458 sub match_lang ($$;@) { #{{{
461 my $regexp=IkiWiki::glob2re($wanted);
465 if (IkiWiki::Plugin::po::istranslation($page)) {
466 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
469 $lang = $config{po_master_language}{code};
472 if ($lang!~/^$regexp$/i) {
473 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
476 return IkiWiki::SuccessReason->new("file language is $wanted");
480 sub match_currentlang ($$;@) { #{{{
484 my ($currentmasterpage, $currentlang, $masterpage, $lang);
486 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
488 if (IkiWiki::Plugin::po::istranslation($params{location})) {
489 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
492 $currentlang = $config{po_master_language}{code};
495 if (IkiWiki::Plugin::po::istranslation($page)) {
496 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
499 $lang = $config{po_master_language}{code};
502 if ($lang eq $currentlang) {
503 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
506 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");