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;
16 memoize("istranslatable");
17 memoize("_istranslation");
20 hook(type => "getsetup", id => "po", call => \&getsetup);
21 hook(type => "checkconfig", id => "po", call => \&checkconfig);
22 hook(type => "scan", id => "po", call => \&scan);
23 hook(type => "targetpage", id => "po", call => \&targetpage);
24 hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
25 hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
26 hook(type => "filter", id => "po", call => \&filter);
27 hook(type => "htmlize", id => "po", call => \&htmlize);
30 sub getsetup () { #{{{
34 rebuild => 1, # format plugin
36 po_master_language => {
42 description => "master language (non-PO files)",
46 po_slave_languages => {
53 description => "slave languages (PO files)",
57 po_translatable_pages => {
59 example => "!*/Discussion",
60 description => "PageSpec controlling which pages are translatable",
61 link => "ikiwiki/PageSpec",
68 description => "internal linking behavior (default/current/negotiated)",
74 sub checkconfig () { #{{{
75 foreach my $field (qw{po_master_language po_slave_languages}) {
76 if (! exists $config{$field} || ! defined $config{$field}) {
77 error(sprintf(gettext("Must specify %s"), $field));
80 if (! exists $config{po_link_to} ||
81 ! defined $config{po_link_to}) {
82 $config{po_link_to}="default";
84 if (! exists $config{po_translatable_pages} ||
85 ! defined $config{po_translatable_pages}) {
86 $config{po_translatable_pages}="";
88 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
89 error(gettext("po_link_to=negotiated requires usedirs to be set"));
91 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
96 my $page=$params{page};
97 # let's build %translations, using istranslation's
98 # side-effect, so that we can consider it is complete at
100 istranslation($page);
103 sub targetpage (@) { #{{{
105 my $page=$params{page};
106 my $ext=$params{ext};
108 if (istranslation($page)) {
109 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
110 if (! $config{usedirs} || $page eq 'index') {
111 return $masterpage . "." . $lang . "." . $ext;
114 return $masterpage . "/index." . $lang . "." . $ext;
117 elsif (istranslatable($page)) {
118 if (! $config{usedirs} || $page eq 'index') {
119 return $page . "." . $config{po_master_language}{code} . "." . $ext;
122 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
128 sub tweakurlpath ($) { #{{{
130 my $url=$params{url};
131 if ($config{po_link_to} eq "negotiated") {
132 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
137 sub tweakbestlink ($$) { #{{{
139 my $page=$params{page};
140 my $link=$params{link};
141 if ($config{po_link_to} eq "current"
142 && istranslatable($link)
143 && istranslation($page)) {
144 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
145 return $link . "." . $curlang;
151 # We use filter to convert PO to the master page's type,
152 # since other plugins should not work on PO files
153 sub filter (@) { #{{{
155 my $page = $params{page};
156 my $destpage = $params{destpage};
157 my $content = decode_utf8(encode_utf8($params{content}));
159 # decide if this is a PO file that should be converted into a translated document,
160 # and perform various sanity checks
161 if (! istranslation($page) || $filtered{$page}{$destpage}) {
165 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
166 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
167 my $masterfile = srcfile($pagesources{$masterpage});
170 push @masters,$masterfile;
172 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
174 my $doc=Locale::Po4a::Chooser::new('text',%options);
176 'po_in_name' => \@pos,
177 'file_in_name' => \@masters,
178 'file_in_charset' => 'utf-8',
179 'file_out_charset' => 'utf-8',
180 ) or error("[po/filter:$file]: failed to translate");
181 my ($percent,$hit,$queries) = $doc->stats();
182 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
183 my $tmpout = $tmpfh->filename;
184 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
185 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
186 $filtered{$page}{$destpage}=1;
190 sub htmlize (@) { #{{{
192 my $page = $params{page};
193 my $content = $params{content};
194 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
195 my $masterfile = srcfile($pagesources{$masterpage});
197 # force content to be htmlize'd as if it was the same type as the master page
198 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
201 sub istranslatable ($) { #{{{
203 my $file=$pagesources{$page};
206 || (defined pagetype($file) && pagetype($file) eq 'po')
207 || $file =~ /\.pot$/) {
210 return pagespec_match($page, $config{po_translatable_pages});
213 sub _istranslation ($) { #{{{
215 my $file=$pagesources{$page};
216 if (! defined $file) {
217 return IkiWiki::FailReason->new("no file specified");
221 || ! defined pagetype($file)
222 || ! pagetype($file) eq 'po'
223 || $file =~ /\.pot$/) {
227 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
228 if (! defined $masterpage || ! defined $lang
229 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
230 || ! defined $pagesources{$masterpage}
231 || ! defined $config{po_slave_languages}{$lang}) {
235 return istranslatable($masterpage);
238 sub istranslation ($) { #{{{
240 if (_istranslation($page)) {
241 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
242 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
248 package IkiWiki::PageSpec;
253 sub match_istranslation ($;@) { #{{{
255 if (IkiWiki::Plugin::po::istranslation($page)) {
256 return IkiWiki::SuccessReason->new("is a translation page");
259 return IkiWiki::FailReason->new("is not a translation page");
263 sub match_istranslatable ($;@) { #{{{
265 if (IkiWiki::Plugin::po::istranslatable($page)) {
266 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
269 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");