summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 63be0e38979d23a972843ba283f5d55ab76af201 (plain)
  1. #!/usr/bin/perl
  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;
  6. use warnings;
  7. use strict;
  8. use IkiWiki 2.00;
  9. use Encode;
  10. use Locale::Po4a::Chooser;
  11. use File::Temp;
  12. sub import {
  13. hook(type => "getsetup", id => "po", call => \&getsetup);
  14. hook(type => "checkconfig", id => "po", call => \&checkconfig);
  15. hook(type => "targetpage", id => "po", call => \&targetpage);
  16. hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
  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);
  20. }
  21. sub getsetup () { #{{{
  22. return
  23. plugin => {
  24. safe => 0,
  25. rebuild => 1, # format plugin
  26. },
  27. po_master_language => {
  28. type => "string",
  29. example => {
  30. 'code' => 'en',
  31. 'name' => 'English'
  32. },
  33. description => "master language (non-PO files)",
  34. safe => 1,
  35. rebuild => 1,
  36. },
  37. po_slave_languages => {
  38. type => "string",
  39. example => {'fr' => { 'name' => 'Français' },
  40. 'es' => { 'name' => 'Castellano' },
  41. 'de' => { 'name' => 'Deutsch' },
  42. },
  43. description => "slave languages (PO files)",
  44. safe => 1,
  45. rebuild => 1,
  46. },
  47. po_link_to_current_language => {
  48. type => "boolean",
  49. example => 1,
  50. description => "internal links point to pages in the current language (useful if Content Negotiation is not supported)",
  51. safe => 1,
  52. rebuild => 1,
  53. },
  54. } #}}}
  55. sub checkconfig () { #{{{
  56. foreach my $field (qw{po_master_language po_slave_languages}) {
  57. if (! exists $config{$field} || ! defined $config{$field}) {
  58. error(sprintf(gettext("Must specify %s"), $field));
  59. }
  60. }
  61. if (! exists $config{po_link_to_current_language} ||
  62. ! defined $config{po_link_to_current_language}) {
  63. $config{po_link_to_current_language}=0;
  64. }
  65. } #}}}
  66. sub targetpage (@) { #{{{
  67. my %params = @_;
  68. my $page=$params{page};
  69. my $ext=$params{ext};
  70. if (pagespec_match($page,"istranslation()")) {
  71. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  72. if (! $config{usedirs} || $page eq 'index') {
  73. return $masterpage . "." . $lang . "." . $ext;
  74. }
  75. else {
  76. return $masterpage . "/index." . $lang . "." . $ext;
  77. }
  78. }
  79. else {
  80. if (! $config{usedirs} || $page eq 'index') {
  81. return $page . "." . $config{po_master_language}{code} . "." . $ext;
  82. }
  83. else {
  84. return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
  85. }
  86. }
  87. } #}}}
  88. sub tweakurlpath ($) { #{{{
  89. my %params = @_;
  90. my $url=$params{url};
  91. if (! $config{po_link_to_current_language} && $config{usedirs}) {
  92. $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
  93. }
  94. return $url;
  95. } #}}}
  96. # We use filter to convert PO to the master page's type,
  97. # since other plugins should not work on PO files
  98. sub filter (@) { #{{{
  99. my %params = @_;
  100. my $page = $params{page};
  101. my $content = decode_utf8(encode_utf8($params{content}));
  102. # decide if this is a PO file that should be converted into a translated document,
  103. # and perform various sanity checks
  104. if (! pagespec_match($page, "istranslation()")) {
  105. return $content;
  106. }
  107. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  108. my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
  109. my $masterfile = srcfile($pagesources{$masterpage});
  110. my (@pos,@masters);
  111. push @pos,$file;
  112. push @masters,$masterfile;
  113. my %options = (
  114. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  115. );
  116. my $doc=Locale::Po4a::Chooser::new('text',%options);
  117. $doc->process(
  118. 'po_in_name' => \@pos,
  119. 'file_in_name' => \@masters,
  120. 'file_in_charset' => 'utf-8',
  121. 'file_out_charset' => 'utf-8',
  122. ) or error("[po/filter:$file]: failed to translate");
  123. my ($percent,$hit,$queries) = $doc->stats();
  124. my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
  125. my $tmpout = $tmpfh->filename;
  126. $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
  127. $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
  128. return $content;
  129. } #}}}
  130. sub preprocess_translatable (@) { #{{{
  131. my %params = @_;
  132. my $match = exists $params{match} ? $params{match} : $params{page};
  133. $pagestate{$params{page}}{po_translatable}{$match}=1;
  134. return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
  135. return sprintf(gettext("pages %s set as translatable"), $params{match});
  136. } #}}}
  137. sub htmlize (@) { #{{{
  138. my %params=@_;
  139. my $page = $params{page};
  140. my $content = $params{content};
  141. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  142. my $masterfile = srcfile($pagesources{$masterpage});
  143. # force content to be htmlize'd as if it was the same type as the master page
  144. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  145. } #}}}
  146. package IkiWiki::PageSpec;
  147. sub match_istranslation ($;@) { #{{{
  148. my $page=shift;
  149. my $wanted=shift;
  150. my %params=@_;
  151. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  152. if (! defined $file) {
  153. return IkiWiki::FailReason->new("no file specified");
  154. }
  155. if (! IkiWiki::pagetype($page) eq 'po') {
  156. return IkiWiki::FailReason->new("is not a PO file");
  157. }
  158. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  159. if (! defined $masterpage || ! defined $lang
  160. || ! (length($masterpage) > 0) || ! (length($lang) > 0)) {
  161. return IkiWiki::FailReason->new("is not named like a translation file");
  162. }
  163. if (! defined $IkiWiki::pagesources{$masterpage}) {
  164. return IkiWiki::FailReason->new("the master page does not exist");
  165. }
  166. if (! defined $IkiWiki::config{po_slave_languages}{$lang}) {
  167. return IkiWiki::FailReason->new("language $lang is not supported");
  168. }
  169. return IkiWiki::SuccessReason->new("page $page is a translation");
  170. } #}}}
  171. 1