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;
15 hook(type => "getsetup", id => "po", call => \&getsetup);
16 hook(type => "checkconfig", id => "po", call => \&checkconfig);
17 hook(type => "targetpage", id => "po", call => \&targetpage);
18 hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
19 hook(type => "filter", id => "po", call => \&filter);
20 hook(type => "preprocess", id => "translatable", call => \&preprocess_translatable);
21 hook(type => "htmlize", id => "po", call => \&htmlize);
24 sub getsetup () { #{{{
28 rebuild => 1, # format plugin
30 po_master_language => {
36 description => "master language (non-PO files)",
40 po_slave_languages => {
42 example => {'fr' => { 'name' => 'Français' },
43 'es' => { 'name' => 'Castellano' },
44 'de' => { 'name' => 'Deutsch' },
46 description => "slave languages (PO files)",
53 description => "internal linking behavior (default/current/negotiated)",
59 sub checkconfig () { #{{{
60 foreach my $field (qw{po_master_language po_slave_languages}) {
61 if (! exists $config{$field} || ! defined $config{$field}) {
62 error(sprintf(gettext("Must specify %s"), $field));
65 if (! exists $config{po_link_to} ||
66 ! defined $config{po_link_to}) {
67 $config{po_link_to}="default";
69 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
70 error(gettext("po_link_to=negotiated requires usedirs to be set"));
74 sub targetpage (@) { #{{{
76 my $page=$params{page};
79 if (pagespec_match($page,"istranslation()")) {
80 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
81 if (! $config{usedirs} || $page eq 'index') {
82 return $masterpage . "." . $lang . "." . $ext;
85 return $masterpage . "/index." . $lang . "." . $ext;
89 if (! $config{usedirs} || $page eq 'index') {
90 return $page . "." . $config{po_master_language}{code} . "." . $ext;
93 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
98 sub tweakurlpath ($) { #{{{
100 my $url=$params{url};
101 if ($config{po_link_to} eq "negotiated") {
102 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
107 # We use filter to convert PO to the master page's type,
108 # since other plugins should not work on PO files
109 sub filter (@) { #{{{
111 my $page = $params{page};
112 my $content = decode_utf8(encode_utf8($params{content}));
114 # decide if this is a PO file that should be converted into a translated document,
115 # and perform various sanity checks
116 if (! pagespec_match($page, "istranslation()")) {
120 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
121 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
122 my $masterfile = srcfile($pagesources{$masterpage});
125 push @masters,$masterfile;
127 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
129 my $doc=Locale::Po4a::Chooser::new('text',%options);
131 'po_in_name' => \@pos,
132 'file_in_name' => \@masters,
133 'file_in_charset' => 'utf-8',
134 'file_out_charset' => 'utf-8',
135 ) or error("[po/filter:$file]: failed to translate");
136 my ($percent,$hit,$queries) = $doc->stats();
137 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
138 my $tmpout = $tmpfh->filename;
139 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
140 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
144 sub preprocess_translatable (@) { #{{{
146 my $match = exists $params{match} ? $params{match} : $params{page};
148 $pagestate{$params{page}}{po_translatable}{$match}=1;
150 return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
151 return sprintf(gettext("pages %s set as translatable"), $params{match});
155 sub htmlize (@) { #{{{
157 my $page = $params{page};
158 my $content = $params{content};
159 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
160 my $masterfile = srcfile($pagesources{$masterpage});
162 # force content to be htmlize'd as if it was the same type as the master page
163 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
166 package IkiWiki::PageSpec;
168 sub match_istranslation ($;@) { #{{{
173 my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
174 if (! defined $file) {
175 return IkiWiki::FailReason->new("no file specified");
178 if (! IkiWiki::pagetype($page) eq 'po') {
179 return IkiWiki::FailReason->new("is not a PO file");
182 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
183 if (! defined $masterpage || ! defined $lang
184 || ! (length($masterpage) > 0) || ! (length($lang) > 0)) {
185 return IkiWiki::FailReason->new("is not named like a translation file");
188 if (! defined $IkiWiki::pagesources{$masterpage}) {
189 return IkiWiki::FailReason->new("the master page does not exist");
192 if (! defined $IkiWiki::config{po_slave_languages}{$lang}) {
193 return IkiWiki::FailReason->new("language $lang is not supported");
196 return IkiWiki::SuccessReason->new("page $page is a translation");