summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 45ed96c65890020295b712697d63b738f8065a86 (plain)
  1. #!/usr/bin/perl
  2. # .po as a wiki page type
  3. # Licensed under GPL v2 or greater
  4. # Copyright (C) 2008 intrigeri <intrigeri@boum.org>
  5. # inspired by the GPL'd po4a-translate,
  6. # which is Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
  7. package IkiWiki::Plugin::po;
  8. use warnings;
  9. use strict;
  10. use IkiWiki 2.00;
  11. use Encode;
  12. use Locale::Po4a::Chooser;
  13. use Locale::Po4a::Po;
  14. use File::Basename;
  15. use File::Copy;
  16. use File::Spec;
  17. use File::Temp;
  18. use Memoize;
  19. use UNIVERSAL;
  20. my %translations;
  21. my @origneedsbuild;
  22. my %origsubs;
  23. memoize("_istranslation");
  24. memoize("percenttranslated");
  25. sub import { #{{{
  26. hook(type => "getsetup", id => "po", call => \&getsetup);
  27. hook(type => "checkconfig", id => "po", call => \&checkconfig);
  28. hook(type => "needsbuild", id => "po", call => \&needsbuild);
  29. hook(type => "scan", id => "po", call => \&scan, last =>1);
  30. hook(type => "filter", id => "po", call => \&filter);
  31. hook(type => "htmlize", id => "po", call => \&htmlize);
  32. hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
  33. hook(type => "change", id => "po", call => \&change);
  34. hook(type => "editcontent", id => "po", call => \&editcontent);
  35. $origsubs{'bestlink'}=\&IkiWiki::bestlink;
  36. inject(name => "IkiWiki::bestlink", call => \&mybestlink);
  37. $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
  38. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  39. $origsubs{'targetpage'}=\&IkiWiki::targetpage;
  40. inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
  41. $origsubs{'urlto'}=\&IkiWiki::urlto;
  42. inject(name => "IkiWiki::urlto", call => \&myurlto);
  43. } #}}}
  44. # ,----
  45. # | Table of contents
  46. # `----
  47. # 1. Hooks
  48. # 2. Injected functions
  49. # 3. Blackboxes for private data
  50. # 4. Helper functions
  51. # 5. PageSpec's
  52. # ,----
  53. # | Hooks
  54. # `----
  55. sub getsetup () { #{{{
  56. return
  57. plugin => {
  58. safe => 0,
  59. rebuild => 1,
  60. },
  61. po_master_language => {
  62. type => "string",
  63. example => {
  64. 'code' => 'en',
  65. 'name' => 'English'
  66. },
  67. description => "master language (non-PO files)",
  68. safe => 1,
  69. rebuild => 1,
  70. },
  71. po_slave_languages => {
  72. type => "string",
  73. example => {
  74. 'fr' => 'Français',
  75. 'es' => 'Castellano',
  76. 'de' => 'Deutsch'
  77. },
  78. description => "slave languages (PO files)",
  79. safe => 1,
  80. rebuild => 1,
  81. },
  82. po_translatable_pages => {
  83. type => "pagespec",
  84. example => "!*/Discussion",
  85. description => "PageSpec controlling which pages are translatable",
  86. link => "ikiwiki/PageSpec",
  87. safe => 1,
  88. rebuild => 1,
  89. },
  90. po_link_to => {
  91. type => "string",
  92. example => "current",
  93. description => "internal linking behavior (default/current/negotiated)",
  94. safe => 1,
  95. rebuild => 1,
  96. },
  97. } #}}}
  98. sub checkconfig () { #{{{
  99. foreach my $field (qw{po_master_language po_slave_languages}) {
  100. if (! exists $config{$field} || ! defined $config{$field}) {
  101. error(sprintf(gettext("Must specify %s"), $field));
  102. }
  103. }
  104. if (! (keys %{$config{po_slave_languages}})) {
  105. error(gettext("At least one slave language must be defined in po_slave_languages"));
  106. }
  107. map {
  108. islanguagecode($_)
  109. or error(sprintf(gettext("%s is not a valid language code"), $_));
  110. } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
  111. if (! exists $config{po_translatable_pages} ||
  112. ! defined $config{po_translatable_pages}) {
  113. $config{po_translatable_pages}="";
  114. }
  115. if (! exists $config{po_link_to} ||
  116. ! defined $config{po_link_to}) {
  117. $config{po_link_to}='default';
  118. }
  119. elsif (! grep {
  120. $config{po_link_to} eq $_
  121. } ('default', 'current', 'negotiated')) {
  122. warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
  123. $config{po_link_to}));
  124. $config{po_link_to}='default';
  125. }
  126. elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
  127. warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
  128. $config{po_link_to}='default';
  129. }
  130. push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
  131. } #}}}
  132. sub needsbuild () { #{{{
  133. my $needsbuild=shift;
  134. # backup @needsbuild content so that change() can know whether
  135. # a given master page was rendered because its source file was changed
  136. @origneedsbuild=(@$needsbuild);
  137. buildtranslationscache();
  138. # make existing translations depend on the corresponding master page
  139. foreach my $master (keys %translations) {
  140. map add_depends($_, $master), values %{otherlanguages($master)};
  141. }
  142. } #}}}
  143. # Massage the recorded state of internal links so that:
  144. # - it matches the actually generated links, rather than the links as written
  145. # in the pages' source
  146. # - backlinks are consistent in all cases
  147. sub scan (@) { #{{{
  148. my %params=@_;
  149. my $page=$params{page};
  150. my $content=$params{content};
  151. return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
  152. if (istranslation($page)) {
  153. foreach my $destpage (@{$links{$page}}) {
  154. if (istranslatable($destpage)) {
  155. # replace one occurence of $destpage in $links{$page}
  156. # (we only want to replace the one that was added by
  157. # IkiWiki::Plugin::link::scan, other occurences may be
  158. # there for other reasons)
  159. for (my $i=0; $i<@{$links{$page}}; $i++) {
  160. if (@{$links{$page}}[$i] eq $destpage) {
  161. @{$links{$page}}[$i] = $destpage . '.' . lang($page);
  162. last;
  163. }
  164. }
  165. }
  166. }
  167. }
  168. elsif (! istranslatable($page) && ! istranslation($page)) {
  169. foreach my $destpage (@{$links{$page}}) {
  170. if (istranslatable($destpage)) {
  171. # make sure any destpage's translations has
  172. # $page in its backlinks
  173. push @{$links{$page}},
  174. values %{otherlanguages($destpage)};
  175. }
  176. }
  177. }
  178. } #}}}
  179. # We use filter to convert PO to the master page's format,
  180. # since the rest of ikiwiki should not work on PO files.
  181. sub filter (@) { #{{{
  182. my %params = @_;
  183. my $page = $params{page};
  184. my $destpage = $params{destpage};
  185. my $content = decode_utf8(encode_utf8($params{content}));
  186. return $content if ( ! istranslation($page)
  187. || alreadyfiltered($page, $destpage) );
  188. # CRLF line terminators make poor Locale::Po4a feel bad
  189. $content=~s/\r\n/\n/g;
  190. # Implementation notes
  191. #
  192. # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
  193. # to learn how to disguise a variable as a file.
  194. # 2. There are incompatibilities between some File::Temp versions
  195. # (including 0.18, bundled with Lenny's perl-modules package)
  196. # and others (e.g. 0.20, previously present in the archive as
  197. # a standalone package): under certain circumstances, some
  198. # return a relative filename, whereas others return an absolute one;
  199. # we here use this module in a way that is at least compatible
  200. # with 0.18 and 0.20. Beware, hit'n'run refactorers!
  201. my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
  202. DIR => File::Spec->tmpdir,
  203. UNLINK => 1)->filename;
  204. my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
  205. DIR => File::Spec->tmpdir,
  206. UNLINK => 1)->filename;
  207. writefile(basename($infile), File::Spec->tmpdir, $content);
  208. my $masterfile = srcfile($pagesources{masterpage($page)});
  209. my (@pos,@masters);
  210. push @pos,$infile;
  211. push @masters,$masterfile;
  212. my %options = (
  213. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  214. );
  215. my $doc=Locale::Po4a::Chooser::new('text',%options);
  216. $doc->process(
  217. 'po_in_name' => \@pos,
  218. 'file_in_name' => \@masters,
  219. 'file_in_charset' => 'utf-8',
  220. 'file_out_charset' => 'utf-8',
  221. ) or error("[po/filter:$page]: failed to translate");
  222. $doc->write($outfile) or error("[po/filter:$page] could not write $outfile");
  223. $content = readfile($outfile) or error("[po/filter:$page] could not read $outfile");
  224. # Unlinking should happen automatically, thanks to File::Temp,
  225. # but it does not work here, probably because of the way writefile()
  226. # and Locale::Po4a::write() work.
  227. unlink $infile, $outfile;
  228. setalreadyfiltered($page, $destpage);
  229. return $content;
  230. } #}}}
  231. sub htmlize (@) { #{{{
  232. my %params=@_;
  233. my $page = $params{page};
  234. my $content = $params{content};
  235. my $masterfile = srcfile($pagesources{masterpage($page)});
  236. # force content to be htmlize'd as if it was the same type as the master page
  237. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  238. } #}}}
  239. sub pagetemplate (@) { #{{{
  240. my %params=@_;
  241. my $page=$params{page};
  242. my $destpage=$params{destpage};
  243. my $template=$params{template};
  244. my ($masterpage, $lang) = istranslation($page);
  245. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  246. $template->param(percenttranslated => percenttranslated($page));
  247. }
  248. if ($template->query(name => "istranslation")) {
  249. $template->param(istranslation => scalar istranslation($page));
  250. }
  251. if ($template->query(name => "istranslatable")) {
  252. $template->param(istranslatable => istranslatable($page));
  253. }
  254. if ($template->query(name => "HOMEPAGEURL")) {
  255. $template->param(homepageurl => homepageurl($page));
  256. }
  257. if ($template->query(name => "otherlanguages")) {
  258. $template->param(otherlanguages => [otherlanguagesloop($page)]);
  259. map add_depends($page, $_), (values %{otherlanguages($page)});
  260. }
  261. # Rely on IkiWiki::Render's genpage() to decide wether
  262. # a discussion link should appear on $page; this is not
  263. # totally accurate, though: some broken links may be generated
  264. # when cgiurl is disabled.
  265. # This compromise avoids some code duplication, and will probably
  266. # prevent future breakage when ikiwiki internals change.
  267. # Known limitations are preferred to future random bugs.
  268. if ($template->param('discussionlink') && istranslation($page)) {
  269. $template->param('discussionlink' => htmllink(
  270. $page,
  271. $destpage,
  272. $masterpage . '/' . gettext("Discussion"),
  273. noimageinline => 1,
  274. forcesubpage => 0,
  275. linktext => gettext("Discussion"),
  276. ));
  277. }
  278. # Remove broken parentlink to ./index.html on home page's translations.
  279. # It works because this hook has the "last" parameter set, to ensure it
  280. # runs after parentlinks' own pagetemplate hook.
  281. if ($template->param('parentlinks')
  282. && istranslation($page)
  283. && $masterpage eq "index") {
  284. $template->param('parentlinks' => []);
  285. }
  286. } # }}}
  287. sub change(@) { #{{{
  288. my @rendered=@_;
  289. my $updated_po_files=0;
  290. # Refresh/create POT and PO files as needed.
  291. foreach my $page (map pagename($_), @rendered) {
  292. next unless istranslatable($page);
  293. my $file=srcfile($pagesources{$page});
  294. my $updated_pot_file=0;
  295. # Only refresh Pot file if it does not exist, or if
  296. # $pagesources{$page} was changed: don't if only the HTML was
  297. # refreshed, e.g. because of a dependency.
  298. if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
  299. || ! -e potfile($file)) {
  300. refreshpot($file);
  301. $updated_pot_file=1;
  302. }
  303. my @pofiles;
  304. map {
  305. push @pofiles, $_ if ($updated_pot_file || ! -e $_);
  306. } (pofiles($file));
  307. if (@pofiles) {
  308. refreshpofiles($file, @pofiles);
  309. map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
  310. $updated_po_files=1;
  311. }
  312. }
  313. if ($updated_po_files) {
  314. # Check staged changes in.
  315. if ($config{rcs}) {
  316. IkiWiki::disable_commit_hook();
  317. IkiWiki::rcs_commit_staged(gettext("updated PO files"),
  318. "IkiWiki::Plugin::po::change", "127.0.0.1");
  319. IkiWiki::enable_commit_hook();
  320. IkiWiki::rcs_update();
  321. }
  322. # Reinitialize module's private variables.
  323. resetalreadyfiltered();
  324. resettranslationscache();
  325. # Trigger a wiki refresh.
  326. require IkiWiki::Render;
  327. # without preliminary saveindex/loadindex, refresh()
  328. # complains about a lot of uninitialized variables
  329. IkiWiki::saveindex();
  330. IkiWiki::loadindex();
  331. IkiWiki::refresh();
  332. IkiWiki::saveindex();
  333. }
  334. } #}}}
  335. # As we're previewing or saving a page, the content may have
  336. # changed, so tell the next filter() invocation it must not be lazy.
  337. sub editcontent () { #{{{
  338. my %params=@_;
  339. unsetalreadyfiltered($params{page}, $params{page});
  340. return $params{content};
  341. } #}}}
  342. # ,----
  343. # | Injected functions
  344. # `----
  345. # Implement po_link_to 'current' and 'negotiated' settings.
  346. sub mybestlink ($$) { #{{{
  347. my $page=shift;
  348. my $link=shift;
  349. my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
  350. if (length $res
  351. && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
  352. && istranslatable($res)
  353. && istranslation($page)) {
  354. return $res . "." . lang($page);
  355. }
  356. return $res;
  357. } #}}}
  358. sub mybeautify_urlpath ($) { #{{{
  359. my $url=shift;
  360. my $res=$origsubs{'beautify_urlpath'}->($url);
  361. if ($config{po_link_to} eq "negotiated") {
  362. $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
  363. $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
  364. map {
  365. $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
  366. } (keys %{$config{po_slave_languages}});
  367. }
  368. return $res;
  369. } #}}}
  370. sub mytargetpage ($$) { #{{{
  371. my $page=shift;
  372. my $ext=shift;
  373. if (istranslation($page) || istranslatable($page)) {
  374. my ($masterpage, $lang) = (masterpage($page), lang($page));
  375. if (! $config{usedirs} || $masterpage eq 'index') {
  376. return $masterpage . "." . $lang . "." . $ext;
  377. }
  378. else {
  379. return $masterpage . "/index." . $lang . "." . $ext;
  380. }
  381. }
  382. return $origsubs{'targetpage'}->($page, $ext);
  383. } #}}}
  384. sub myurlto ($$;$) { #{{{
  385. my $to=shift;
  386. my $from=shift;
  387. my $absolute=shift;
  388. # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
  389. if (! length $to
  390. && $config{po_link_to} eq "current"
  391. && istranslatable('index')) {
  392. return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
  393. }
  394. return $origsubs{'urlto'}->($to,$from,$absolute);
  395. } #}}}
  396. # ,----
  397. # | Blackboxes for private data
  398. # `----
  399. {
  400. my %filtered;
  401. sub alreadyfiltered($$) { #{{{
  402. my $page=shift;
  403. my $destpage=shift;
  404. return ( exists $filtered{$page}{$destpage}
  405. && $filtered{$page}{$destpage} eq 1 );
  406. } #}}}
  407. sub setalreadyfiltered($$) { #{{{
  408. my $page=shift;
  409. my $destpage=shift;
  410. $filtered{$page}{$destpage}=1;
  411. } #}}}
  412. sub unsetalreadyfiltered($$) { #{{{
  413. my $page=shift;
  414. my $destpage=shift;
  415. if (exists $filtered{$page}{$destpage}) {
  416. delete $filtered{$page}{$destpage};
  417. }
  418. } #}}}
  419. sub resetalreadyfiltered() { #{{{
  420. undef %filtered;
  421. } #}}}
  422. }
  423. # ,----
  424. # | Helper functions
  425. # `----
  426. sub istranslatable ($) { #{{{
  427. my $page=shift;
  428. my $file=$pagesources{$page};
  429. return 0 unless defined $file;
  430. return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
  431. return 0 if $file =~ /\.pot$/;
  432. return pagespec_match($page, $config{po_translatable_pages});
  433. } #}}}
  434. sub _istranslation ($) { #{{{
  435. my $page=shift;
  436. my $file=$pagesources{$page};
  437. return 0 unless (defined $file
  438. && defined pagetype($file)
  439. && pagetype($file) eq 'po');
  440. return 0 if $file =~ /\.pot$/;
  441. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  442. return 0 unless (defined $masterpage && defined $lang
  443. && length $masterpage && length $lang
  444. && defined $pagesources{$masterpage}
  445. && defined $config{po_slave_languages}{$lang});
  446. return ($masterpage, $lang) if istranslatable($masterpage);
  447. } #}}}
  448. sub istranslation ($) { #{{{
  449. my $page=shift;
  450. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  451. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  452. return ($masterpage, $lang);
  453. }
  454. return;
  455. } #}}}
  456. sub masterpage ($) { #{{{
  457. my $page=shift;
  458. if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
  459. return $masterpage;
  460. }
  461. return $page;
  462. } #}}}
  463. sub lang ($) { #{{{
  464. my $page=shift;
  465. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  466. return $lang;
  467. }
  468. return $config{po_master_language}{code};
  469. } #}}}
  470. sub islanguagecode ($) { #{{{
  471. my $code=shift;
  472. return ($code =~ /^[a-z]{2}$/);
  473. } #}}}
  474. sub otherlanguages($) { #{{{
  475. my $page=shift;
  476. my %ret;
  477. if (istranslatable($page)) {
  478. %ret = %{$translations{$page}} if defined $translations{$page};
  479. }
  480. elsif (istranslation($page)) {
  481. my $masterpage = masterpage($page);
  482. $ret{$config{po_master_language}{code}} = $masterpage;
  483. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  484. next if $lang eq lang($page);
  485. $ret{$lang} = $translations{$masterpage}{$lang};
  486. }
  487. }
  488. return \%ret;
  489. } #}}}
  490. sub potfile ($) { #{{{
  491. my $masterfile=shift;
  492. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  493. $dir='' if $dir eq './';
  494. return File::Spec->catpath('', $dir, $name . ".pot");
  495. } #}}}
  496. sub pofile ($$) { #{{{
  497. my $masterfile=shift;
  498. my $lang=shift;
  499. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  500. $dir='' if $dir eq './';
  501. return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
  502. } #}}}
  503. sub pofiles ($) { #{{{
  504. my $masterfile=shift;
  505. return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
  506. } #}}}
  507. sub refreshpot ($) { #{{{
  508. my $masterfile=shift;
  509. my $potfile=potfile($masterfile);
  510. my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
  511. my $doc=Locale::Po4a::Chooser::new('text',%options);
  512. $doc->{TT}{utf_mode} = 1;
  513. $doc->{TT}{file_in_charset} = 'utf-8';
  514. $doc->{TT}{file_out_charset} = 'utf-8';
  515. $doc->read($masterfile);
  516. # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
  517. # this is undocument use of internal Locale::Po4a::TransTractor's data,
  518. # compulsory since this module prevents us from using the porefs option.
  519. my %po_options = ('porefs' => 'none');
  520. $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
  521. $doc->{TT}{po_out}->set_charset('utf-8');
  522. # do the actual work
  523. $doc->parse;
  524. IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
  525. $doc->writepo($potfile);
  526. } #}}}
  527. sub refreshpofiles ($@) { #{{{
  528. my $masterfile=shift;
  529. my @pofiles=@_;
  530. my $potfile=potfile($masterfile);
  531. error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
  532. foreach my $pofile (@pofiles) {
  533. IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
  534. if (-e $pofile) {
  535. system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
  536. or error("[po/refreshpofiles:$pofile] failed to update");
  537. }
  538. else {
  539. File::Copy::syscopy($potfile,$pofile)
  540. or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
  541. }
  542. }
  543. } #}}}
  544. sub buildtranslationscache() { #{{{
  545. # use istranslation's side-effect
  546. map istranslation($_), (keys %pagesources);
  547. } #}}}
  548. sub resettranslationscache() { #{{{
  549. undef %translations;
  550. } #}}}
  551. sub urlto_with_orig_beautiful_urlpath($$) { #{{{
  552. my $to=shift;
  553. my $from=shift;
  554. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
  555. my $res=urlto($to, $from);
  556. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  557. return $res;
  558. } #}}}
  559. sub percenttranslated ($) { #{{{
  560. my $page=shift;
  561. return gettext("N/A") unless istranslation($page);
  562. my $file=srcfile($pagesources{$page});
  563. my $masterfile = srcfile($pagesources{masterpage($page)});
  564. my (@pos,@masters);
  565. push @pos,$file;
  566. push @masters,$masterfile;
  567. my %options = (
  568. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  569. );
  570. my $doc=Locale::Po4a::Chooser::new('text',%options);
  571. $doc->process(
  572. 'po_in_name' => \@pos,
  573. 'file_in_name' => \@masters,
  574. 'file_in_charset' => 'utf-8',
  575. 'file_out_charset' => 'utf-8',
  576. ) or error("[po/percenttranslated:$page]: failed to translate");
  577. my ($percent,$hit,$queries) = $doc->stats();
  578. return $percent;
  579. } #}}}
  580. sub languagename ($) { #{{{
  581. my $code=shift;
  582. return $config{po_master_language}{name}
  583. if $code eq $config{po_master_language}{code};
  584. return $config{po_slave_languages}{$code}
  585. if defined $config{po_slave_languages}{$code};
  586. return;
  587. } #}}}
  588. sub otherlanguagesloop ($) { #{{{
  589. my $page=shift;
  590. my @ret;
  591. my %otherpages=%{otherlanguages($page)};
  592. while (my ($lang, $otherpage) = each %otherpages) {
  593. if (istranslation($page) && masterpage($page) eq $otherpage) {
  594. push @ret, {
  595. url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
  596. code => $lang,
  597. language => languagename($lang),
  598. master => 1,
  599. };
  600. }
  601. else {
  602. push @ret, {
  603. url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
  604. code => $lang,
  605. language => languagename($lang),
  606. percent => percenttranslated($otherpage),
  607. }
  608. }
  609. }
  610. return sort {
  611. return -1 if $a->{code} eq $config{po_master_language}{code};
  612. return 1 if $b->{code} eq $config{po_master_language}{code};
  613. return $a->{language} cmp $b->{language};
  614. } @ret;
  615. } #}}}
  616. sub homepageurl (;$) { #{{{
  617. my $page=shift;
  618. return urlto('', $page);
  619. } #}}}
  620. # ,----
  621. # | PageSpec's
  622. # `----
  623. package IkiWiki::PageSpec;
  624. use warnings;
  625. use strict;
  626. use IkiWiki 2.00;
  627. sub match_istranslation ($;@) { #{{{
  628. my $page=shift;
  629. if (IkiWiki::Plugin::po::istranslation($page)) {
  630. return IkiWiki::SuccessReason->new("is a translation page");
  631. }
  632. else {
  633. return IkiWiki::FailReason->new("is not a translation page");
  634. }
  635. } #}}}
  636. sub match_istranslatable ($;@) { #{{{
  637. my $page=shift;
  638. if (IkiWiki::Plugin::po::istranslatable($page)) {
  639. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  640. }
  641. else {
  642. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  643. }
  644. } #}}}
  645. sub match_lang ($$;@) { #{{{
  646. my $page=shift;
  647. my $wanted=shift;
  648. my $regexp=IkiWiki::glob2re($wanted);
  649. my $lang=IkiWiki::Plugin::po::lang($page);
  650. if ($lang!~/^$regexp$/i) {
  651. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  652. }
  653. else {
  654. return IkiWiki::SuccessReason->new("file language is $wanted");
  655. }
  656. } #}}}
  657. sub match_currentlang ($$;@) { #{{{
  658. my $page=shift;
  659. shift;
  660. my %params=@_;
  661. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  662. my $currentlang=IkiWiki::Plugin::po::lang($params{location});
  663. my $lang=IkiWiki::Plugin::po::lang($page);
  664. if ($lang eq $currentlang) {
  665. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  666. }
  667. else {
  668. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  669. }
  670. } #}}}
  671. 1