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;
13 use Locale::Po4a::Chooser;
26 memoize("istranslatable");
27 memoize("_istranslation");
28 memoize("percenttranslated");
31 hook(type => "getsetup", id => "po", call => \&getsetup);
32 hook(type => "checkconfig", id => "po", call => \&checkconfig);
33 hook(type => "needsbuild", id => "po", call => \&needsbuild);
34 hook(type => "scan", id => "po", call => \&scan, last =>1);
35 hook(type => "filter", id => "po", call => \&filter);
36 hook(type => "htmlize", id => "po", call => \&htmlize);
37 hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
38 hook(type => "postscan", id => "po", call => \&postscan);
39 hook(type => "rename", id => "po", call => \&renamepages, first => 1);
40 hook(type => "delete", id => "po", call => \&mydelete);
41 hook(type => "change", id => "po", call => \&change);
42 hook(type => "cansave", id => "po", call => \&cansave);
43 hook(type => "canremove", id => "po", call => \&canremove);
44 hook(type => "canrename", id => "po", call => \&canrename);
45 hook(type => "editcontent", id => "po", call => \&editcontent);
47 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
48 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
49 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
50 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
51 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
52 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
53 $origsubs{'urlto'}=\&IkiWiki::urlto;
54 inject(name => "IkiWiki::urlto", call => \&myurlto);
55 $origsubs{'nicepagetitle'}=\&IkiWiki::nicepagetitle;
56 inject(name => "IkiWiki::nicepagetitle", call => \&mynicepagetitle);
65 # 2. Injected functions
66 # 3. Blackboxes for private data
81 po_master_language => {
87 description => "master language (non-PO files)",
91 po_slave_languages => {
98 description => "slave languages (PO files)",
102 po_translatable_pages => {
104 example => "!*/Discussion",
105 description => "PageSpec controlling which pages are translatable",
106 link => "ikiwiki/PageSpec",
112 example => "current",
113 description => "internal linking behavior (default/current/negotiated)",
117 po_translation_status_in_links => {
120 description => "display translation status in links to translations",
127 foreach my $field (qw{po_master_language po_slave_languages}) {
128 if (! exists $config{$field} || ! defined $config{$field}) {
129 error(sprintf(gettext("Must specify %s"), $field));
132 if (! (keys %{$config{po_slave_languages}})) {
133 error(gettext("At least one slave language must be defined in po_slave_languages"));
137 or error(sprintf(gettext("%s is not a valid language code"), $_));
138 } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
139 if (! exists $config{po_translatable_pages} ||
140 ! defined $config{po_translatable_pages}) {
141 $config{po_translatable_pages}="";
143 if (! exists $config{po_link_to} ||
144 ! defined $config{po_link_to}) {
145 $config{po_link_to}='default';
148 $config{po_link_to} eq $_
149 } ('default', 'current', 'negotiated')) {
150 warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
151 $config{po_link_to}));
152 $config{po_link_to}='default';
154 elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
155 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
156 $config{po_link_to}='default';
158 if (! exists $config{po_translation_status_in_links} ||
159 ! defined $config{po_translation_status_in_links}) {
160 $config{po_translation_status_in_links}=1;
162 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
166 my $needsbuild=shift;
168 # backup @needsbuild content so that change() can know whether
169 # a given master page was rendered because its source file was changed
170 @origneedsbuild=(@$needsbuild);
173 buildtranslationscache();
175 # make existing translations depend on the corresponding master page
176 foreach my $master (keys %translations) {
177 map add_depends($_, $master), values %{otherlanguages($master)};
181 # Massage the recorded state of internal links so that:
182 # - it matches the actually generated links, rather than the links as written
183 # in the pages' source
184 # - backlinks are consistent in all cases
187 my $page=$params{page};
188 my $content=$params{content};
190 return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
192 if (istranslation($page)) {
193 foreach my $destpage (@{$links{$page}}) {
194 if (istranslatable($destpage)) {
195 # replace one occurence of $destpage in $links{$page}
196 # (we only want to replace the one that was added by
197 # IkiWiki::Plugin::link::scan, other occurences may be
198 # there for other reasons)
199 for (my $i=0; $i<@{$links{$page}}; $i++) {
200 if (@{$links{$page}}[$i] eq $destpage) {
201 @{$links{$page}}[$i] = $destpage . '.' . lang($page);
208 elsif (! istranslatable($page) && ! istranslation($page)) {
209 foreach my $destpage (@{$links{$page}}) {
210 if (istranslatable($destpage)) {
211 # make sure any destpage's translations has
212 # $page in its backlinks
213 push @{$links{$page}},
214 values %{otherlanguages($destpage)};
220 # We use filter to convert PO to the master page's format,
221 # since the rest of ikiwiki should not work on PO files.
225 my $page = $params{page};
226 my $destpage = $params{destpage};
227 my $content = $params{content};
228 if (istranslation($page) && ! alreadyfiltered($page, $destpage)) {
229 $content = po_to_markup($page, $content);
230 setalreadyfiltered($page, $destpage);
238 my $page = $params{page};
239 my $content = $params{content};
241 # ignore PO files this plugin did not create
242 return $content unless istranslation($page);
244 # force content to be htmlize'd as if it was the same type as the master page
245 return IkiWiki::htmlize($page, $page,
246 pagetype(srcfile($pagesources{masterpage($page)})),
250 sub pagetemplate (@) {
252 my $page=$params{page};
253 my $destpage=$params{destpage};
254 my $template=$params{template};
256 my ($masterpage, $lang) = istranslation($page);
258 if (istranslation($page) && $template->query(name => "percenttranslated")) {
259 $template->param(percenttranslated => percenttranslated($page));
261 if ($template->query(name => "istranslation")) {
262 $template->param(istranslation => scalar istranslation($page));
264 if ($template->query(name => "istranslatable")) {
265 $template->param(istranslatable => istranslatable($page));
267 if ($template->query(name => "HOMEPAGEURL")) {
268 $template->param(homepageurl => homepageurl($page));
270 if ($template->query(name => "otherlanguages")) {
271 $template->param(otherlanguages => [otherlanguagesloop($page)]);
272 map add_depends($page, $_), (values %{otherlanguages($page)});
274 # Rely on IkiWiki::Render's genpage() to decide wether
275 # a discussion link should appear on $page; this is not
276 # totally accurate, though: some broken links may be generated
277 # when cgiurl is disabled.
278 # This compromise avoids some code duplication, and will probably
279 # prevent future breakage when ikiwiki internals change.
280 # Known limitations are preferred to future random bugs.
281 if ($template->param('discussionlink') && istranslation($page)) {
282 $template->param('discussionlink' => htmllink(
285 $masterpage . '/' . gettext("Discussion"),
288 linktext => gettext("Discussion"),
291 # Remove broken parentlink to ./index.html on home page's translations.
292 # It works because this hook has the "last" parameter set, to ensure it
293 # runs after parentlinks' own pagetemplate hook.
294 if ($template->param('parentlinks')
295 && istranslation($page)
296 && $masterpage eq "index") {
297 $template->param('parentlinks' => []);
303 my $page = $params{page};
305 # backlinks involve back-dependencies, so that nicepagetitle effects,
306 # such as translation status displayed in links, are updated
308 map add_depends($page, $_), keys %{$IkiWiki::backlinks{$page}};
311 # Add the renamed page translations to the list of to-be-renamed pages.
312 sub renamepages($$$) {
313 my ($torename, $cgi, $session) = (shift, shift, shift);
315 # copy the initial array, so that we can iterate on it AND
316 # modify it at the same time, without iterating on the items we
317 # pushed on it ourselves
318 my @torename=@{$torename};
320 # Save the page(s) the user asked to rename, so that our
321 # canrename hook can tell the difference between:
322 # - a translation being renamed as a consequence of its master page
324 # - a user trying to directly rename a translation
325 # This is why this hook has to be run first, before @torename is modified
327 $session->param(po_orig_torename => [ @torename ]);
328 IkiWiki::cgi_savesession($session);
330 foreach my $rename (@torename) {
331 next unless istranslatable($rename->{src});
332 my %otherpages=%{otherlanguages($rename->{src})};
333 while (my ($lang, $otherpage) = each %otherpages) {
336 srcfile => $pagesources{$otherpage},
337 dest => otherlanguage($rename->{dest}, $lang),
338 destfile => $rename->{dest}.".".$lang.".po",
348 map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
354 my $updated_po_files=0;
356 # Refresh/create POT and PO files as needed.
357 foreach my $file (grep {istranslatablefile($_)} @rendered) {
358 my $page=pagename($file);
359 my $masterfile=srcfile($file);
360 my $updated_pot_file=0;
361 # Only refresh Pot file if it does not exist, or if
362 # $pagesources{$page} was changed: don't if only the HTML was
363 # refreshed, e.g. because of a dependency.
364 if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
365 || ! -e potfile($masterfile)) {
366 refreshpot($masterfile);
371 push @pofiles, $_ if ($updated_pot_file || ! -e $_);
372 } (pofiles($masterfile));
374 refreshpofiles($masterfile, @pofiles);
375 map { IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
380 if ($updated_po_files) {
382 gettext("updated PO files"),
383 "IkiWiki::Plugin::po::change");
388 my ($page, $content, $cgi, $session) = (shift, shift, shift, shift);
390 if (istranslation($page)) {
391 if (defined po_to_markup($page, $content, "nonfatal")) {
395 return "Could not parse this page's content; is this valid gettext?";
401 sub canremove ($$$) {
402 my ($page, $cgi, $session) = (shift, shift, shift);
404 if (istranslation($page)) {
405 return gettext("Can not remove a translation. Removing the master page, ".
406 "though, removes its translations as well.");
411 sub canrename ($$@) {
412 my ($cgi, $session) = (shift, shift);
415 if (istranslation($params{src})) {
416 my $masterpage = masterpage($params{src});
417 # Tell the difference between:
418 # - a translation being renamed as a consequence of its master page
419 # being renamed, which is allowed
420 # - a user trying to directly rename a translation, which is forbidden
421 # by looking for the master page in the list of to-be-renamed pages we
422 # saved early in the renaming process.
423 my $orig_torename = $session->param("po_orig_torename");
424 unless (scalar grep { $_->{src} eq $masterpage } @{$orig_torename}) {
425 return gettext("Can not rename a translation. Renaming the master page, ".
426 "though, renames its translations as well.");
432 # As we're previewing or saving a page, the content may have
433 # changed, so tell the next filter() invocation it must not be lazy.
437 unsetalreadyfiltered($params{page}, $params{page});
438 return $params{content};
443 # | Injected functions
446 # Implement po_link_to 'current' and 'negotiated' settings.
447 sub mybestlink ($$) {
451 my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
453 && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
454 && istranslatable($res)
455 && istranslation($page)) {
456 return $res . "." . lang($page);
461 sub mybeautify_urlpath ($) {
464 my $res=$origsubs{'beautify_urlpath'}->($url);
465 if ($config{po_link_to} eq "negotiated") {
466 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
467 $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
469 $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
470 } (keys %{$config{po_slave_languages}});
475 sub mytargetpage ($$) {
479 if (istranslation($page) || istranslatable($page)) {
480 my ($masterpage, $lang) = (masterpage($page), lang($page));
481 if (! $config{usedirs} || $masterpage eq 'index') {
482 return $masterpage . "." . $lang . "." . $ext;
485 return $masterpage . "/index." . $lang . "." . $ext;
488 return $origsubs{'targetpage'}->($page, $ext);
496 # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
498 && $config{po_link_to} eq "current"
499 && istranslatable('index')) {
500 return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
502 # avoid using our injected beautify_urlpath if run by cgi_editpage,
503 # so that one is redirected to the just-edited page rather than to the
504 # negociated translation; to prevent unnecessary fiddling with caller/inject,
505 # we only do so when our beautify_urlpath would actually do what we want to
506 # avoid, i.e. when po_link_to = negotiated
507 if ($config{po_link_to} eq "negotiated") {
508 my @caller = caller(1);
509 my $run_by_editpage = 0;
510 $run_by_editpage = 1 if (exists $caller[3] && defined $caller[3]
511 && $caller[3] eq "IkiWiki::cgi_editpage");
512 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'})
514 my $res = $origsubs{'urlto'}->($to,$from,$absolute);
515 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath)
520 return $origsubs{'urlto'}->($to,$from,$absolute)
524 sub mynicepagetitle ($;$) {
525 my ($page, $unescaped) = (shift, shift);
527 my $res = $origsubs{'nicepagetitle'}->($page, $unescaped);
528 return $res unless istranslation($page);
529 return $res unless $config{po_translation_status_in_links};
530 return $res.' ('.percenttranslated($page).' %)';
534 # | Blackboxes for private data
540 sub alreadyfiltered($$) {
544 return ( exists $filtered{$page}{$destpage}
545 && $filtered{$page}{$destpage} eq 1 );
548 sub setalreadyfiltered($$) {
552 $filtered{$page}{$destpage}=1;
555 sub unsetalreadyfiltered($$) {
559 if (exists $filtered{$page}{$destpage}) {
560 delete $filtered{$page}{$destpage};
564 sub resetalreadyfiltered() {
573 sub maybe_add_leading_slash ($;$) {
576 $add=1 unless defined $add;
577 return '/' . $str if $add;
581 sub istranslatablefile ($) {
584 return 0 unless defined $file;
585 return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
586 return 0 if $file =~ /\.pot$/;
587 return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
591 sub istranslatable ($) {
595 return 1 if istranslatablefile($pagesources{$page});
599 sub _istranslation ($) {
602 my $hasleadingslash = ($page=~s#^/##);
603 my $file=$pagesources{$page};
604 return 0 unless (defined $file
605 && defined pagetype($file)
606 && pagetype($file) eq 'po');
607 return 0 if $file =~ /\.pot$/;
609 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
610 return 0 unless (defined $masterpage && defined $lang
611 && length $masterpage && length $lang
612 && defined $pagesources{$masterpage}
613 && defined $config{po_slave_languages}{$lang});
615 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
616 if istranslatable($masterpage);
619 sub istranslation ($) {
622 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
623 my $hasleadingslash = ($masterpage=~s#^/##);
624 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
625 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
633 if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
642 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
645 return $config{po_master_language}{code};
648 sub islanguagecode ($) {
651 return ($code =~ /^[a-z]{2}$/);
654 sub otherlanguage ($$) {
658 return masterpage($page) if $code eq $config{po_master_language}{code};
659 return masterpage($page) . '.' . $code;
662 sub otherlanguages ($) {
666 return \%ret unless (istranslation($page) || istranslatable($page));
667 my $curlang=lang($page);
669 ($config{po_master_language}{code}, keys %{$config{po_slave_languages}}) {
670 next if $lang eq $curlang;
671 $ret{$lang}=otherlanguage($page, $lang);
677 my $masterfile=shift;
679 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
680 $dir='' if $dir eq './';
681 return File::Spec->catpath('', $dir, $name . ".pot");
685 my $masterfile=shift;
688 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
689 $dir='' if $dir eq './';
690 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
694 my $masterfile=shift;
696 return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
700 my $masterfile=shift;
702 my $potfile=potfile($masterfile);
703 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
704 my $doc=Locale::Po4a::Chooser::new('text',%options);
705 $doc->{TT}{utf_mode} = 1;
706 $doc->{TT}{file_in_charset} = 'utf-8';
707 $doc->{TT}{file_out_charset} = 'utf-8';
708 $doc->read($masterfile);
709 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
710 # this is undocument use of internal Locale::Po4a::TransTractor's data,
711 # compulsory since this module prevents us from using the porefs option.
712 $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
713 $doc->{TT}{po_out}->set_charset('utf-8');
716 IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
717 $doc->writepo($potfile);
720 sub refreshpofiles ($@) {
721 my $masterfile=shift;
724 my $potfile=potfile($masterfile);
725 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
727 foreach my $pofile (@pofiles) {
728 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
730 system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
731 or error("[po/refreshpofiles:$pofile] failed to update");
734 File::Copy::syscopy($potfile,$pofile)
735 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
740 sub buildtranslationscache() {
741 # use istranslation's side-effect
742 map istranslation($_), (keys %pagesources);
745 sub resettranslationscache() {
749 sub flushmemoizecache() {
750 Memoize::flush_cache("istranslatable");
751 Memoize::flush_cache("_istranslation");
752 Memoize::flush_cache("percenttranslated");
755 sub urlto_with_orig_beautiful_urlpath($$) {
759 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
760 my $res=urlto($to, $from);
761 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
766 sub percenttranslated ($) {
770 return gettext("N/A") unless istranslation($page);
771 my $file=srcfile($pagesources{$page});
772 my $masterfile = srcfile($pagesources{masterpage($page)});
774 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
776 my $doc=Locale::Po4a::Chooser::new('text',%options);
778 'po_in_name' => [ $file ],
779 'file_in_name' => [ $masterfile ],
780 'file_in_charset' => 'utf-8',
781 'file_out_charset' => 'utf-8',
782 ) or error("[po/percenttranslated:$page]: failed to translate");
783 my ($percent,$hit,$queries) = $doc->stats();
787 sub languagename ($) {
790 return $config{po_master_language}{name}
791 if $code eq $config{po_master_language}{code};
792 return $config{po_slave_languages}{$code}
793 if defined $config{po_slave_languages}{$code};
797 sub otherlanguagesloop ($) {
801 my %otherpages=%{otherlanguages($page)};
802 while (my ($lang, $otherpage) = each %otherpages) {
803 if (istranslation($page) && masterpage($page) eq $otherpage) {
805 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
807 language => languagename($lang),
813 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
815 language => languagename($lang),
816 percent => percenttranslated($otherpage),
821 return -1 if $a->{code} eq $config{po_master_language}{code};
822 return 1 if $b->{code} eq $config{po_master_language}{code};
823 return $a->{language} cmp $b->{language};
827 sub homepageurl (;$) {
830 return urlto('', $page);
833 sub deletetranslations ($) {
834 my $deletedmasterfile=shift;
836 my $deletedmasterpage=pagename($deletedmasterfile);
839 my $file = newpagefile($deletedmasterpage.'.'.$_, 'po');
840 my $absfile = "$config{srcdir}/$file";
841 if (-e $absfile && ! -l $absfile && ! -d $absfile) {
842 push @todelete, $file;
844 } keys %{$config{po_slave_languages}};
848 IkiWiki::rcs_remove($_);
851 IkiWiki::prune("$config{srcdir}/$_");
855 if (scalar @todelete) {
857 gettext("removed obsolete PO files"),
858 "IkiWiki::Plugin::po::deletetranslations");
862 sub commit_and_refresh ($$) {
863 my ($msg, $author) = (shift, shift);
866 IkiWiki::disable_commit_hook();
867 IkiWiki::rcs_commit_staged($msg, $author, "127.0.0.1");
868 IkiWiki::enable_commit_hook();
869 IkiWiki::rcs_update();
871 # Reinitialize module's private variables.
872 resetalreadyfiltered();
873 resettranslationscache();
875 # Trigger a wiki refresh.
876 require IkiWiki::Render;
877 # without preliminary saveindex/loadindex, refresh()
878 # complains about a lot of uninitialized variables
879 IkiWiki::saveindex();
880 IkiWiki::loadindex();
882 IkiWiki::saveindex();
885 # on success, returns the filtered content.
886 # on error, if $nonfatal, warn and return undef; else, error out.
887 sub po_to_markup ($$;$) {
888 my ($page, $content) = (shift, shift);
889 my $nonfatal = shift;
891 $content = '' unless defined $content;
892 $content = decode_utf8(encode_utf8($content));
893 # CRLF line terminators make poor Locale::Po4a feel bad
894 $content=~s/\r\n/\n/g;
896 # There are incompatibilities between some File::Temp versions
897 # (including 0.18, bundled with Lenny's perl-modules package)
898 # and others (e.g. 0.20, previously present in the archive as
899 # a standalone package): under certain circumstances, some
900 # return a relative filename, whereas others return an absolute one;
901 # we here use this module in a way that is at least compatible
902 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
903 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
904 DIR => File::Spec->tmpdir,
905 UNLINK => 1)->filename;
906 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
907 DIR => File::Spec->tmpdir,
908 UNLINK => 1)->filename;
911 my $msg = '[po/filter:'.$page.'] ' . shift;
916 error($msg, sub { unlink $infile, $outfile});
919 writefile(basename($infile), File::Spec->tmpdir, $content)
920 or return failure("failed to write $infile");
922 my $masterfile = srcfile($pagesources{masterpage($page)});
924 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
926 my $doc=Locale::Po4a::Chooser::new('text',%options);
928 'po_in_name' => [ $infile ],
929 'file_in_name' => [ $masterfile ],
930 'file_in_charset' => 'utf-8',
931 'file_out_charset' => 'utf-8',
932 ) or return failure("failed to translate");
933 $doc->write($outfile) or return failure("could not write $outfile");
935 $content = readfile($outfile) or return failure("could not read $outfile");
937 # Unlinking should happen automatically, thanks to File::Temp,
938 # but it does not work here, probably because of the way writefile()
939 # and Locale::Po4a::write() work.
940 unlink $infile, $outfile;
949 package IkiWiki::PageSpec;
954 sub match_istranslation ($;@) {
957 if (IkiWiki::Plugin::po::istranslation($page)) {
958 return IkiWiki::SuccessReason->new("is a translation page");
961 return IkiWiki::FailReason->new("is not a translation page");
965 sub match_istranslatable ($;@) {
968 if (IkiWiki::Plugin::po::istranslatable($page)) {
969 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
972 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
976 sub match_lang ($$;@) {
980 my $regexp=IkiWiki::glob2re($wanted);
981 my $lang=IkiWiki::Plugin::po::lang($page);
982 if ($lang!~/^$regexp$/i) {
983 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
986 return IkiWiki::SuccessReason->new("file language is $wanted");
990 sub match_currentlang ($$;@) {
995 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
997 my $currentlang=IkiWiki::Plugin::po::lang($params{location});
998 my $lang=IkiWiki::Plugin::po::lang($page);
1000 if ($lang eq $currentlang) {
1001 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
1004 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");