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 => "targetpage", id => "po", call => \&targetpage);
23 hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
24 hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
25 hook(type => "filter", id => "po", call => \&filter);
26 hook(type => "htmlize", id => "po", call => \&htmlize);
29 sub getsetup () { #{{{
33 rebuild => 1, # format plugin
35 po_master_language => {
41 description => "master language (non-PO files)",
45 po_slave_languages => {
47 example => {'fr' => { 'name' => 'Français' },
48 'es' => { 'name' => 'Castellano' },
49 'de' => { 'name' => 'Deutsch' },
51 description => "slave languages (PO files)",
55 po_translatable_pages => {
57 example => "!*/Discussion",
58 description => "PageSpec controlling which pages are translatable",
59 link => "ikiwiki/PageSpec",
66 description => "internal linking behavior (default/current/negotiated)",
72 sub checkconfig () { #{{{
73 foreach my $field (qw{po_master_language po_slave_languages}) {
74 if (! exists $config{$field} || ! defined $config{$field}) {
75 error(sprintf(gettext("Must specify %s"), $field));
78 if (! exists $config{po_link_to} ||
79 ! defined $config{po_link_to}) {
80 $config{po_link_to}="default";
82 if (! exists $config{po_translatable_pages} ||
83 ! defined $config{po_translatable_pages}) {
84 $config{po_translatable_pages}="";
86 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
87 error(gettext("po_link_to=negotiated requires usedirs to be set"));
89 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
92 sub targetpage (@) { #{{{
94 my $page=$params{page};
97 if (istranslation($page)) {
98 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
99 if (! $config{usedirs} || $page eq 'index') {
100 return $masterpage . "." . $lang . "." . $ext;
103 return $masterpage . "/index." . $lang . "." . $ext;
106 elsif (istranslatable($page)) {
107 if (! $config{usedirs} || $page eq 'index') {
108 return $page . "." . $config{po_master_language}{code} . "." . $ext;
111 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
117 sub tweakurlpath ($) { #{{{
119 my $url=$params{url};
120 if ($config{po_link_to} eq "negotiated") {
121 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
126 sub tweakbestlink ($$) { #{{{
128 my $page=$params{page};
129 my $link=$params{link};
130 if ($config{po_link_to} eq "current"
131 && istranslatable($link)
132 && istranslation($page)) {
133 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
134 return $link . "." . $curlang;
140 # We use filter to convert PO to the master page's type,
141 # since other plugins should not work on PO files
142 sub filter (@) { #{{{
144 my $page = $params{page};
145 my $destpage = $params{destpage};
146 my $content = decode_utf8(encode_utf8($params{content}));
148 # decide if this is a PO file that should be converted into a translated document,
149 # and perform various sanity checks
150 if (! istranslation($page) || $filtered{$page}{$destpage}) {
154 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
155 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
156 my $masterfile = srcfile($pagesources{$masterpage});
159 push @masters,$masterfile;
161 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
163 my $doc=Locale::Po4a::Chooser::new('text',%options);
165 'po_in_name' => \@pos,
166 'file_in_name' => \@masters,
167 'file_in_charset' => 'utf-8',
168 'file_out_charset' => 'utf-8',
169 ) or error("[po/filter:$file]: failed to translate");
170 my ($percent,$hit,$queries) = $doc->stats();
171 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
172 my $tmpout = $tmpfh->filename;
173 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
174 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
175 $filtered{$page}{$destpage}=1;
179 sub htmlize (@) { #{{{
181 my $page = $params{page};
182 my $content = $params{content};
183 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
184 my $masterfile = srcfile($pagesources{$masterpage});
186 # force content to be htmlize'd as if it was the same type as the master page
187 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
190 sub istranslatable ($) { #{{{
192 my $file=$pagesources{$page};
195 || (defined pagetype($file) && pagetype($file) eq 'po')
196 || $file =~ /\.pot$/) {
199 return pagespec_match($page, $config{po_translatable_pages});
202 sub _istranslation ($) { #{{{
204 my $file=$pagesources{$page};
205 if (! defined $file) {
206 return IkiWiki::FailReason->new("no file specified");
210 || ! defined pagetype($file)
211 || ! pagetype($file) eq 'po'
212 || $file =~ /\.pot$/) {
216 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
217 if (! defined $masterpage || ! defined $lang
218 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
219 || ! defined $pagesources{$masterpage}
220 || ! defined $config{po_slave_languages}{$lang}) {
224 return istranslatable($masterpage);
227 sub istranslation ($) { #{{{
229 if (_istranslation($page)) {
230 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
231 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
238 package IkiWiki::PageSpec;
243 sub match_istranslation ($;@) { #{{{
245 if (IkiWiki::Plugin::po::istranslation($page)) {
246 return IkiWiki::SuccessReason->new("is a translation page");
249 return IkiWiki::FailReason->new("is not a translation page");
253 sub match_istranslatable ($;@) { #{{{
255 if (IkiWiki::Plugin::po::istranslatable($page)) {
256 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
259 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");