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");
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);
34 sub getsetup () { #{{{
38 rebuild => 1, # format plugin
40 po_master_language => {
46 description => "master language (non-PO files)",
50 po_slave_languages => {
57 description => "slave languages (PO files)",
61 po_translatable_pages => {
63 example => "!*/Discussion",
64 description => "PageSpec controlling which pages are translatable",
65 link => "ikiwiki/PageSpec",
72 description => "internal linking behavior (default/current/negotiated)",
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));
84 if (! exists $config{po_link_to} ||
85 ! defined $config{po_link_to}) {
86 $config{po_link_to}="default";
88 if (! exists $config{po_translatable_pages} ||
89 ! defined $config{po_translatable_pages}) {
90 $config{po_translatable_pages}="";
92 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
93 error(gettext("po_link_to=negotiated requires usedirs to be set"));
95 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
98 sub refreshpot ($) { #{{{
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';
109 $doc->writepo($potfile);
112 sub refreshpofiles ($@) { #{{{
113 my $masterfile=shift;
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);
120 foreach my $pofile (@pofiles) {
122 my $cmd = "msgmerge -U $pofile $potfile";
124 or error("[po/refreshpofiles:$pofile] failed to update");
127 File::Copy::syscopy($potfile,$pofile)
128 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
133 sub needsbuild () { #{{{
134 my $needsbuild=shift;
136 # build %translations, using istranslation's side-effect
137 foreach my $page (keys %pagesources) {
138 istranslation($page);
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));
147 foreach my $lang (keys %{$translations{$page}}) {
148 push @pofiles, $pagesources{$translations{$page}{$lang}};
150 refreshpofiles(srcfile($file), map { srcfile($_) } @pofiles);
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);
162 sub targetpage (@) { #{{{
164 my $page=$params{page};
165 my $ext=$params{ext};
167 if (istranslation($page)) {
168 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
169 if (! $config{usedirs} || $page eq 'index') {
170 return $masterpage . "." . $lang . "." . $ext;
173 return $masterpage . "/index." . $lang . "." . $ext;
176 elsif (istranslatable($page)) {
177 if (! $config{usedirs} || $page eq 'index') {
178 return $page . "." . $config{po_master_language}{code} . "." . $ext;
181 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
187 sub tweakurlpath ($) { #{{{
189 my $url=$params{url};
190 if ($config{po_link_to} eq "negotiated") {
191 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
196 sub tweakbestlink ($$) { #{{{
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;
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 (@) { #{{{
214 my $page = $params{page};
215 my $destpage = $params{destpage};
216 my $content = decode_utf8(encode_utf8($params{content}));
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}) {
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});
229 push @masters,$masterfile;
231 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
233 my $doc=Locale::Po4a::Chooser::new('text',%options);
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 ($percent,$hit,$queries) = $doc->stats();
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 otherlanguages ($) { #{{{
263 if (istranslatable($page)) {
264 foreach my $lang (sort keys %{$translations{$page}}) {
266 url => urlto($translations{$page}{$lang}, $page),
268 language => $config{po_slave_languages}{$lang},
273 elsif (istranslation($page)) {
274 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
276 url => urlto($masterpage, $page),
277 code => $config{po_master_language}{code},
278 language => $config{po_master_language}{name},
281 foreach my $lang (sort keys %{$translations{$masterpage}}) {
283 url => urlto($translations{$masterpage}{$lang}, $page),
285 language => $config{po_slave_languages}{$lang},
287 } unless ($lang eq $curlang);
293 sub pagetemplate (@) { #{{{
295 my $page=$params{page};
296 my $template=$params{template};
298 if ($template->query(name => "otherlanguages")) {
299 $template->param(otherlanguages => [otherlanguages($page)]);
303 sub istranslatable ($) { #{{{
305 my $file=$pagesources{$page};
308 || (defined pagetype($file) && pagetype($file) eq 'po')
309 || $file =~ /\.pot$/) {
312 return pagespec_match($page, $config{po_translatable_pages});
315 sub _istranslation ($) { #{{{
317 my $file=$pagesources{$page};
318 if (! defined $file) {
319 return IkiWiki::FailReason->new("no file specified");
323 || ! defined pagetype($file)
324 || ! pagetype($file) eq 'po'
325 || $file =~ /\.pot$/) {
329 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
330 if (! defined $masterpage || ! defined $lang
331 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
332 || ! defined $pagesources{$masterpage}
333 || ! defined $config{po_slave_languages}{$lang}) {
337 return istranslatable($masterpage);
340 sub istranslation ($) { #{{{
342 if (_istranslation($page)) {
343 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
344 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
350 package IkiWiki::PageSpec;
355 sub match_istranslation ($;@) { #{{{
357 if (IkiWiki::Plugin::po::istranslation($page)) {
358 return IkiWiki::SuccessReason->new("is a translation page");
361 return IkiWiki::FailReason->new("is not a translation page");
365 sub match_istranslatable ($;@) { #{{{
367 if (IkiWiki::Plugin::po::istranslatable($page)) {
368 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
371 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");