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);
155 sub targetpage (@) { #{{{
157 my $page=$params{page};
158 my $ext=$params{ext};
160 if (istranslation($page)) {
161 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
162 if (! $config{usedirs} || $page eq 'index') {
163 return $masterpage . "." . $lang . "." . $ext;
166 return $masterpage . "/index." . $lang . "." . $ext;
169 elsif (istranslatable($page)) {
170 if (! $config{usedirs} || $page eq 'index') {
171 return $page . "." . $config{po_master_language}{code} . "." . $ext;
174 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
180 sub tweakurlpath ($) { #{{{
182 my $url=$params{url};
183 if ($config{po_link_to} eq "negotiated") {
184 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
189 sub tweakbestlink ($$) { #{{{
191 my $page=$params{page};
192 my $link=$params{link};
193 if ($config{po_link_to} eq "current"
194 && istranslatable($link)
195 && istranslation($page)) {
196 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
197 return $link . "." . $curlang;
203 # We use filter to convert PO to the master page's type,
204 # since other plugins should not work on PO files
205 sub filter (@) { #{{{
207 my $page = $params{page};
208 my $destpage = $params{destpage};
209 my $content = decode_utf8(encode_utf8($params{content}));
211 # decide if this is a PO file that should be converted into a translated document,
212 # and perform various sanity checks
213 if (! istranslation($page) || $filtered{$page}{$destpage}) {
217 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
218 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
219 my $masterfile = srcfile($pagesources{$masterpage});
222 push @masters,$masterfile;
224 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
226 my $doc=Locale::Po4a::Chooser::new('text',%options);
228 'po_in_name' => \@pos,
229 'file_in_name' => \@masters,
230 'file_in_charset' => 'utf-8',
231 'file_out_charset' => 'utf-8',
232 ) or error("[po/filter:$file]: failed to translate");
233 my ($percent,$hit,$queries) = $doc->stats();
234 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
235 my $tmpout = $tmpfh->filename;
236 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
237 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
238 $filtered{$page}{$destpage}=1;
242 sub htmlize (@) { #{{{
244 my $page = $params{page};
245 my $content = $params{content};
246 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
247 my $masterfile = srcfile($pagesources{$masterpage});
249 # force content to be htmlize'd as if it was the same type as the master page
250 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
253 sub otherlanguages ($) { #{{{
256 if (istranslatable($page)) {
257 foreach my $lang (sort keys %{$translations{$page}}) {
259 url => urlto($translations{$page}{$lang}, $page),
261 language => $config{po_slave_languages}{$lang},
266 elsif (istranslation($page)) {
267 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
269 url => urlto($masterpage, $page),
270 code => $config{po_master_language}{code},
271 language => $config{po_master_language}{name},
274 foreach my $lang (sort keys %{$translations{$masterpage}}) {
276 url => urlto($translations{$masterpage}{$lang}, $page),
278 language => $config{po_slave_languages}{$lang},
280 } unless ($lang eq $curlang);
286 sub pagetemplate (@) { #{{{
288 my $page=$params{page};
289 my $template=$params{template};
291 if ($template->query(name => "otherlanguages")) {
292 $template->param(otherlanguages => [otherlanguages($page)]);
296 sub istranslatable ($) { #{{{
298 my $file=$pagesources{$page};
301 || (defined pagetype($file) && pagetype($file) eq 'po')
302 || $file =~ /\.pot$/) {
305 return pagespec_match($page, $config{po_translatable_pages});
308 sub _istranslation ($) { #{{{
310 my $file=$pagesources{$page};
311 if (! defined $file) {
312 return IkiWiki::FailReason->new("no file specified");
316 || ! defined pagetype($file)
317 || ! pagetype($file) eq 'po'
318 || $file =~ /\.pot$/) {
322 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
323 if (! defined $masterpage || ! defined $lang
324 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
325 || ! defined $pagesources{$masterpage}
326 || ! defined $config{po_slave_languages}{$lang}) {
330 return istranslatable($masterpage);
333 sub istranslation ($) { #{{{
335 if (_istranslation($page)) {
336 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
337 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
343 package IkiWiki::PageSpec;
348 sub match_istranslation ($;@) { #{{{
350 if (IkiWiki::Plugin::po::istranslation($page)) {
351 return IkiWiki::SuccessReason->new("is a translation page");
354 return IkiWiki::FailReason->new("is not a translation page");
358 sub match_istranslatable ($;@) { #{{{
360 if (IkiWiki::Plugin::po::istranslatable($page)) {
361 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
364 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");