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 => "targetpage", id => "po", call => \&targetpage);
17 hook(type => "filter", id => "po", call => \&filter);
18 hook(type => "preprocess", id => "translatable", call => \&preprocess_translatable);
19 hook(type => "htmlize", id => "po", call => \&htmlize);
22 sub getsetup () { #{{{
26 rebuild => 1, # format plugin
28 po_master_language => {
34 description => "master language (non-PO files)",
38 po_slave_languages => {
40 example => {'fr' => { 'name' => 'Français' },
41 'es' => { 'name' => 'Castellano' },
42 'de' => { 'name' => 'Deutsch' },
44 description => "slave languages (PO files)",
50 sub targetpage (@) { #{{{
52 my $page=$params{page};
55 if (pagespec_match($page,"istranslation()")) {
56 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
57 if (! $config{usedirs} || $page eq 'index') {
58 return $masterpage . "." . $lang . "." . $ext;
61 return $masterpage . "/index." . $lang . "." . $ext;
65 if (! $config{usedirs} || $page eq 'index') {
66 return $page . "." . $config{po_master_language}{code} . "." . $ext;
69 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
74 # We use filter to convert PO to the master page's type,
75 # since other plugins should not work on PO files
78 my $page = $params{page};
79 my $content = decode_utf8(encode_utf8($params{content}));
81 # decide if this is a PO file that should be converted into a translated document,
82 # and perform various sanity checks
83 if (! pagespec_match($page, "istranslation()")) {
87 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
88 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
89 my $masterfile = srcfile($pagesources{$masterpage});
92 push @masters,$masterfile;
94 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
96 my $doc=Locale::Po4a::Chooser::new('text',%options);
98 'po_in_name' => \@pos,
99 'file_in_name' => \@masters,
100 'file_in_charset' => 'utf-8',
101 'file_out_charset' => 'utf-8',
102 ) or error("[po/filter:$file]: failed to translate");
103 my ($percent,$hit,$queries) = $doc->stats();
104 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
105 my $tmpout = $tmpfh->filename;
106 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
107 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
111 sub preprocess_translatable (@) { #{{{
113 my $match = exists $params{match} ? $params{match} : $params{page};
115 $pagestate{$params{page}}{po_translatable}{$match}=1;
117 return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
118 return sprintf(gettext("pages %s set as translatable"), $params{match});
122 sub htmlize (@) { #{{{
124 my $page = $params{page};
125 my $content = $params{content};
126 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
127 my $masterfile = srcfile($pagesources{$masterpage});
129 # force content to be htmlize'd as if it was the same type as the master page
130 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
133 package IkiWiki::PageSpec;
135 sub match_istranslation ($;@) { #{{{
140 my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
141 if (! defined $file) {
142 return IkiWiki::FailReason->new("no file specified");
145 if (! IkiWiki::pagetype($page) eq 'po') {
146 return IkiWiki::FailReason->new("is not a PO file");
149 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
150 if (! defined $masterpage || ! defined $lang
151 || ! (length($masterpage) > 0) || ! (length($lang) > 0)) {
152 return IkiWiki::FailReason->new("is not named like a translation file");
155 if (! defined $IkiWiki::pagesources{$masterpage}) {
156 return IkiWiki::FailReason->new("the master page does not exist");
159 if (! defined $IkiWiki::config{po_slave_languages}{$lang}) {
160 return IkiWiki::FailReason->new("language $lang is not supported");
163 return IkiWiki::SuccessReason->new("page $page is a translation");