2 # .po as a wiki page type
3 # Licensed under GPL v2 or greater
4 # Copyright (C) 2008-2009 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 eval q{use Locale::Po4a::Common qw(nowrapi18n !/.*/)};
15 print STDERR gettext("warning: Old po4a detected! Recommend upgrade to 0.35.")."\n";
16 eval q{use Locale::Po4a::Common qw(!/.*/)};
19 use Locale::Po4a::Chooser;
27 my ($master_language_code, $master_language_name);
31 my @slavelanguages; # language codes ordered as in config po_slave_languages
32 my %slavelanguages; # language code to name lookup
33 my $language_code_pattern = '[a-zA-Z]+(?:_[a-zA-Z]+)?';
35 memoize("istranslatable");
36 memoize("_istranslation");
37 memoize("percenttranslated");
40 hook(type => "getsetup", id => "po", call => \&getsetup);
41 hook(type => "checkconfig", id => "po", call => \&checkconfig,
43 hook(type => "needsbuild", id => "po", call => \&needsbuild);
44 hook(type => "scan", id => "po", call => \&scan, last => 1);
45 hook(type => "filter", id => "po", call => \&filter);
46 hook(type => "htmlize", id => "po", call => \&htmlize);
47 hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
48 hook(type => "rename", id => "po", call => \&renamepages, first => 1);
49 hook(type => "delete", id => "po", call => \&mydelete);
50 hook(type => "rendered", id => "po", call => \&rendered);
51 hook(type => "checkcontent", id => "po", call => \&checkcontent);
52 hook(type => "canremove", id => "po", call => \&canremove);
53 hook(type => "canrename", id => "po", call => \&canrename);
54 hook(type => "formbuilder_setup", id => "po", call => \&formbuilder_setup, last => 1);
55 hook(type => "formbuilder", id => "po", call => \&formbuilder);
58 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
59 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
60 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
61 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
62 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
63 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
64 $origsubs{'urlto'}=\&IkiWiki::urlto;
65 inject(name => "IkiWiki::urlto", call => \&myurlto);
66 $origsubs{'cgiurl'}=\&IkiWiki::cgiurl;
67 inject(name => "IkiWiki::cgiurl", call => \&mycgiurl);
68 if (IkiWiki->can('rootpage')) {
69 $origsubs{'rootpage'}=\&IkiWiki::rootpage;
70 inject(name => "IkiWiki::rootpage", call => \&myrootpage)
71 if defined $origsubs{'rootpage'};
73 $origsubs{'isselflink'}=\&IkiWiki::isselflink;
74 inject(name => "IkiWiki::isselflink", call => \&myisselflink);
84 # 2. Injected functions
85 # 3. Blackboxes for private data
98 rebuild => 1, # format plugin
101 po_master_language => {
103 example => "en|English",
104 description => "master language (non-PO files)",
108 po_slave_languages => {
115 description => "slave languages (translated via PO files) format: ll|Langname",
119 po_translatable_pages => {
121 example => "* and !*/Discussion",
122 description => "PageSpec controlling which pages are translatable",
123 link => "ikiwiki/PageSpec",
129 example => "current",
130 description => "internal linking behavior (default/current/negotiated)",
137 if (exists $config{po_master_language}) {
138 if (! ref $config{po_master_language}) {
139 ($master_language_code, $master_language_name)=
140 splitlangpair($config{po_master_language});
143 $master_language_code=$config{po_master_language}{code};
144 $master_language_name=$config{po_master_language}{name};
145 $config{po_master_language}=joinlangpair($master_language_code, $master_language_name);
148 if (! defined $master_language_code) {
149 $master_language_code='en';
151 if (! defined $master_language_name) {
152 $master_language_name='English';
155 if (ref $config{po_slave_languages} eq 'ARRAY') {
156 foreach my $pair (@{$config{po_slave_languages}}) {
157 my ($code, $name)=splitlangpair($pair);
158 if (defined $code && ! exists $slavelanguages{$code}) {
159 push @slavelanguages, $code;
160 $slavelanguages{$code} = $name;
164 elsif (ref $config{po_slave_languages} eq 'HASH') {
165 %slavelanguages=%{$config{po_slave_languages}};
166 @slavelanguages = sort {
167 $config{po_slave_languages}->{$a} cmp $config{po_slave_languages}->{$b};
168 } keys %slavelanguages;
169 $config{po_slave_languages}=[
170 map { joinlangpair($_, $slavelanguages{$_}) } @slavelanguages
174 delete $slavelanguages{$master_language_code};
178 or error(sprintf(gettext("%s is not a valid language code"), $_));
179 } ($master_language_code, @slavelanguages);
181 if (! exists $config{po_translatable_pages} ||
182 ! defined $config{po_translatable_pages}) {
183 $config{po_translatable_pages}="";
185 if (! exists $config{po_link_to} ||
186 ! defined $config{po_link_to}) {
187 $config{po_link_to}='default';
189 elsif ($config{po_link_to} !~ /^(default|current|negotiated)$/) {
190 warn(sprintf(gettext('%s is not a valid value for po_link_to, falling back to po_link_to=default'),
191 $config{po_link_to}));
192 $config{po_link_to}='default';
194 elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
195 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
196 $config{po_link_to}='default';
199 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
201 # Translated versions of the underlays are added if available.
202 foreach my $underlay ("basewiki",
203 map { m/^\Q$config{underlaydirbase}\E\/*(.*)/ }
204 reverse @{$config{underlaydirs}}) {
205 next if $underlay=~/^locale\//;
207 # Underlays containing the po files for slave languages.
208 foreach my $ll (@slavelanguages) {
209 add_underlay("po/$ll/$underlay")
210 if -d "$config{underlaydirbase}/po/$ll/$underlay";
213 if ($master_language_code ne 'en') {
214 # Add underlay containing translated source files
215 # for the master language.
216 add_underlay("locale/$master_language_code/$underlay")
217 if -d "$config{underlaydirbase}/locale/$master_language_code/$underlay";
223 my $needsbuild=shift;
225 # backup @needsbuild content so that change() can know whether
226 # a given master page was rendered because its source file was changed
227 @origneedsbuild=(@$needsbuild);
230 buildtranslationscache();
232 # make existing translations depend on the corresponding master page
233 foreach my $master (keys %translations) {
234 map add_depends($_, $master), values %{otherlanguages_pages($master)};
242 my $page=$params{page};
243 my $content=$params{content};
244 my $run_by_po=$params{run_by_po};
246 # Massage the recorded state of internal links so that:
247 # - it matches the actually generated links, rather than the links as
248 # written in the pages' source
249 # - backlinks are consistent in all cases
251 # A second scan pass is made over translation pages, so as an
252 # optimization, we only do so on the second pass in this case,
253 # i.e. when this hook is called by itself.
254 if ($run_by_po && istranslation($page)) {
255 # replace the occurence of $destpage in $links{$page}
256 my @orig_links = @{$links{$page}};
258 foreach my $destpage (@orig_links) {
259 if (istranslatedto($destpage, lang($page))) {
260 add_link($page, $destpage . '.' . lang($page));
263 add_link($page, $destpage);
267 # No second scan pass is done for a non-translation page, so
268 # links massaging must happen on first pass in this case.
269 elsif (! $run_by_po && ! istranslatable($page) && ! istranslation($page)) {
270 foreach my $destpage (@{$links{$page}}) {
271 if (istranslatable($destpage)) {
272 # make sure any destpage's translations has
273 # $page in its backlinks
274 foreach my $link (values %{otherlanguages_pages($destpage)}) {
275 add_link($page, $link);
281 # Re-run the preprocess hooks in scan mode, then the scan hooks,
282 # over the po-to-markup converted content
283 return if $run_by_po; # avoid looping endlessly
284 return unless istranslation($page);
285 $content = po_to_markup($page, $content);
287 IkiWiki::preprocess($page, $page, $content, 1);
288 IkiWiki::run_hooks(scan => sub {
297 # We use filter to convert PO to the master page's format,
298 # since the rest of ikiwiki should not work on PO files.
302 my $page = $params{page};
303 my $destpage = $params{destpage};
304 my $content = $params{content};
305 if (istranslation($page)) {
306 $content = po_to_markup($page, $content);
314 my $page = $params{page};
315 my $content = $params{content};
317 # ignore PO files this plugin did not create
318 return $content unless istranslation($page);
320 # force content to be htmlize'd as if it was the same type as the master page
321 return IkiWiki::htmlize($page, $page,
322 pagetype(srcfile($pagesources{masterpage($page)})),
326 sub pagetemplate (@) {
328 my $page=$params{page};
329 my $destpage=$params{destpage};
330 my $template=$params{template};
332 my ($masterpage, $lang) = istranslation($page);
334 if (istranslation($page) && $template->query(name => "percenttranslated")) {
335 $template->param(percenttranslated => percenttranslated($page));
337 if ($template->query(name => "istranslation")) {
338 $template->param(istranslation => scalar istranslation($page));
340 if ($template->query(name => "istranslatable")) {
341 $template->param(istranslatable => istranslatable($page));
343 my $lang_code = istranslation($page) ? lang($page) : $master_language_code;
344 if ($template->query(name => "lang_code")) {
345 $template->param(lang_code => $lang_code);
347 if ($template->query(name => "html_lang_code")) {
348 $template->param(html_lang_code => htmllangcode($lang_code));
350 if ($template->query(name => "html_lang_dir")) {
351 $template->param(html_lang_dir => htmllangdir($lang_code));
353 if ($template->query(name => "lang_name")) {
354 $template->param(lang_name => languagename($lang_code));
356 if ($template->query(name => "HOMEPAGEURL")) {
357 $template->param(homepageurl => homepageurl($page));
359 if ($template->query(name => "otherlanguages")) {
360 $template->param(otherlanguages => [otherlanguagesloop($page)]);
361 map add_depends($page, $_), (values %{otherlanguages_pages($page)});
363 if ($config{discussion} && istranslation($page)) {
364 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
365 (length $config{cgiurl} ||
366 exists $links{$masterpage."/".lc($config{discussionpage})})) {
367 $template->param('discussionlink' => htmllink(
370 $masterpage . '/' . $config{discussionpage},
373 linktext => $config{discussionpage},
377 # Remove broken parentlink to ./index.html on home page's translations.
378 # It works because this hook has the "last" parameter set, to ensure it
379 # runs after parentlinks' own pagetemplate hook.
380 if ($template->param('parentlinks')
381 && istranslation($page)
382 && $masterpage eq "index") {
383 $template->param('parentlinks' => []);
385 if (ishomepage($page) && $template->query(name => "title")
386 && !$template->param("title_overridden")) {
387 $template->param(title => $config{wikiname});
391 # Add the renamed page translations to the list of to-be-renamed pages.
392 sub renamepages (@) {
395 my %torename = %{$params{torename}};
396 my $session = $params{session};
398 # Save the page(s) the user asked to rename, so that our
399 # canrename hook can tell the difference between:
400 # - a translation being renamed as a consequence of its master page
402 # - a user trying to directly rename a translation
403 # This is why this hook has to be run first, before the list of pages
404 # to rename is modified by other plugins.
406 @orig_torename=@{$session->param("po_orig_torename")}
407 if defined $session->param("po_orig_torename");
408 push @orig_torename, $torename{src};
409 $session->param(po_orig_torename => \@orig_torename);
410 IkiWiki::cgi_savesession($session);
412 return () unless istranslatable($torename{src});
415 my %otherpages=%{otherlanguages_pages($torename{src})};
416 while (my ($lang, $otherpage) = each %otherpages) {
419 srcfile => $pagesources{$otherpage},
420 dest => otherlanguage_page($torename{dest}, $lang),
421 destfile => $torename{dest}.".".$lang.".po",
431 map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
437 my $updated_po_files=0;
439 # Refresh/create POT and PO files as needed.
440 foreach my $file (grep {istranslatablefile($_)} @rendered) {
441 my $masterfile=srcfile($file);
442 my $page=pagename($file);
443 my $updated_pot_file=0;
445 # Avoid touching underlay files.
446 next if $masterfile ne "$config{srcdir}/$file";
448 # Only refresh POT file if it does not exist, or if
449 # the source was changed: don't if only the HTML was
450 # refreshed, e.g. because of a dependency.
451 if ((grep { $_ eq $pagesources{$page} } @origneedsbuild) ||
452 ! -e potfile($masterfile)) {
453 refreshpot($masterfile);
457 foreach my $po (pofiles($masterfile)) {
458 next if ! $updated_pot_file && -e $po;
459 next if grep { $po=~/\Q$_\E/ } @{$config{underlaydirs}};
463 refreshpofiles($masterfile, @pofiles);
464 map { s/^\Q$config{srcdir}\E\/*//; IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
469 if ($updated_po_files) {
471 gettext("updated PO files"));
475 sub checkcontent (@) {
478 if (istranslation($params{page})) {
479 my $res = isvalidpo($params{content});
493 if (istranslation($params{page})) {
494 return gettext("Can not remove a translation. If the master page is removed, ".
495 "however, its translations will be removed as well.");
502 my $session = $params{session};
504 if (istranslation($params{src})) {
505 my $masterpage = masterpage($params{src});
506 # Tell the difference between:
507 # - a translation being renamed as a consequence of its master page
508 # being renamed, which is allowed
509 # - a user trying to directly rename a translation, which is forbidden
510 # by looking for the master page in the list of to-be-renamed pages we
511 # saved early in the renaming process.
512 my $orig_torename = $session->param("po_orig_torename");
513 unless (grep { $_ eq $masterpage } @{$orig_torename}) {
514 return gettext("Can not rename a translation. If the master page is renamed, ".
515 "however, its translations will be renamed as well.");
521 sub formbuilder_setup (@) {
523 my $form=$params{form};
526 return unless defined $form->field("do");
528 if ($form->field("do") eq "create") {
529 # Warn the user: new pages must be written in master language.
530 my $template=template("pocreatepage.tmpl");
531 $template->param(LANG => $master_language_name);
532 $form->tmpl_param(message => $template->output);
534 elsif ($form->field("do") eq "edit") {
535 # Remove the rename/remove buttons on slave pages.
536 # This has to be done after the rename/remove plugins have added
537 # their buttons, which is why this hook must be run last.
538 # The canrename/canremove hooks already ensure this is forbidden
539 # at the backend level, so this is only UI sugar.
540 if (istranslation(scalar $form->field("page"))) {
542 for (my $i = 0; $i < @{$params{buttons}}; $i++) {
543 if (@{$params{buttons}}[$i] eq $_) {
544 delete @{$params{buttons}}[$i];
553 sub formbuilder (@) {
555 my $form=$params{form};
558 return unless defined $form->field("do");
560 # Do not allow to create pages of type po: they are automatically created.
561 # The main reason to do so is to bypass the "favor the type of linking page
562 # on page creation" logic, which is unsuitable when a broken link is clicked
563 # on a slave (PO) page.
564 # This cannot be done in the formbuilder_setup hook as the list of types is
566 if ($form->field("do") eq "create") {
567 foreach my $field ($form->field) {
568 next unless "$field" eq "type";
569 next unless $field->type eq 'select';
570 my $orig_value = $field->value;
571 # remove po from the list of types
572 my @types = grep { $_->[0] ne 'po' } $field->options;
573 $field->options(\@types) if @types;
574 # favor the type of linking page's masterpage
575 if ($orig_value eq 'po') {
577 if (defined $form->field('from')) {
578 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
579 $from = masterpage($from);
581 if (defined $from && exists $pagesources{$from}) {
582 $type=pagetype($pagesources{$from});
584 $type=$config{default_pageext} unless defined $type;
585 $field->value($type) ;
592 # | Injected functions
595 # Implement po_link_to 'current' and 'negotiated' settings.
596 sub mybestlink ($$) {
600 return $origsubs{'bestlink'}->($page, $link)
601 if defined $config{po_link_to} && $config{po_link_to} eq "default";
603 my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
604 my @caller = caller(1);
606 && istranslatedto($res, lang($page))
607 && istranslation($page)
608 && !(exists $caller[3] && defined $caller[3]
609 && ($caller[3] eq "IkiWiki::PageSpec::match_link"))) {
610 return $res . "." . lang($page);
615 sub mybeautify_urlpath ($) {
618 my $res=$origsubs{'beautify_urlpath'}->($url);
619 if (defined $config{po_link_to} && $config{po_link_to} eq "negotiated") {
620 $res =~ s!/\Qindex.$master_language_code.$config{htmlext}\E$!/!;
621 $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
623 $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
629 sub mytargetpage ($$;$) {
634 if (istranslation($page) || istranslatable($page)) {
635 my ($masterpage, $lang) = (masterpage($page), lang($page));
636 if (defined $filename) {
637 return $masterpage . "/" . $filename . "." . $lang . "." . $ext;
639 elsif (! $config{usedirs} || $masterpage eq 'index') {
640 return $masterpage . "." . $lang . "." . $ext;
643 return $masterpage . "/index." . $lang . "." . $ext;
646 return $origsubs{'targetpage'}->($page, $ext, $filename);
654 # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
656 && $config{po_link_to} eq "current"
657 && istranslatable('index')) {
659 return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
662 return $origsubs{'urlto'}->($to,$from,$absolute);
665 # avoid using our injected beautify_urlpath if run by cgi_editpage,
666 # so that one is redirected to the just-edited page rather than to the
667 # negociated translation; to prevent unnecessary fiddling with caller/inject,
668 # we only do so when our beautify_urlpath would actually do what we want to
669 # avoid, i.e. when po_link_to = negotiated.
670 # also avoid doing so when run by cgi_goto, so that the links on recentchanges
671 # page actually lead to the exact page they pretend to.
672 if ($config{po_link_to} eq "negotiated") {
673 my @caller = caller(1);
675 $use_orig = 1 if (exists $caller[3] && defined $caller[3]
676 && ($caller[3] eq "IkiWiki::cgi_editpage" ||
677 $caller[3] eq "IkiWiki::Plugin::goto::cgi_goto")
679 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'})
681 my $res = $origsubs{'urlto'}->($to,$from,$absolute);
682 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath)
687 return $origsubs{'urlto'}->($to,$from,$absolute)
694 # slave pages have no subpages
695 if (istranslation($params{'from'})) {
696 $params{'from'} = masterpage($params{'from'});
698 return $origsubs{'cgiurl'}->(%params);
705 if (exists $params{rootpage}) {
706 $rootpage=$origsubs{'bestlink'}->($params{page}, $params{rootpage});
707 if (!length $rootpage) {
708 $rootpage=$params{rootpage};
712 $rootpage=masterpage($params{page});
717 sub myisselflink ($$) {
721 return 1 if $origsubs{'isselflink'}->($page, $link);
722 if (istranslation($page)) {
723 return $origsubs{'isselflink'}->(masterpage($page), $link);
732 sub maybe_add_leading_slash ($;$) {
735 $add=1 unless defined $add;
736 return '/' . $str if $add;
740 sub istranslatablefile ($) {
743 return 0 unless defined $file;
744 my $type=pagetype($file);
745 return 0 if ! defined $type || $type eq 'po';
746 return 0 if $file =~ /\.pot$/;
747 return 0 if ! defined $config{po_translatable_pages};
748 return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
752 sub istranslatable ($) {
756 return 1 if istranslatablefile($pagesources{$page});
760 sub istranslatedto ($$) {
762 my $destlang = shift;
765 return 0 unless istranslatable($page);
766 exists $pagesources{otherlanguage_page($page, $destlang)};
769 sub _istranslation ($) {
772 $page='' unless defined $page && length $page;
773 my $hasleadingslash = ($page=~s#^/##);
774 my $file=$pagesources{$page};
775 return 0 unless defined $file
776 && defined pagetype($file)
777 && pagetype($file) eq 'po';
778 return 0 if $file =~ /\.pot$/;
780 my ($masterpage, $lang) = ($page =~ /(.*)[.]($language_code_pattern)$/);
781 return 0 unless defined $masterpage && defined $lang
782 && length $masterpage && length $lang
783 && defined $pagesources{$masterpage}
784 && defined $slavelanguages{$lang};
786 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
787 if istranslatable($masterpage);
790 sub istranslation ($) {
793 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
794 my $hasleadingslash = ($masterpage=~s#^/##);
795 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
796 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
804 if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
813 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
816 return $master_language_code;
819 sub htmllangcode ($) {
820 (my $lang = shift) =~ tr/_/-/;
824 sub htmllangdir ($) {
826 if ($lang =~ /^(ar|fa|he)/) {
832 sub islanguagecode ($) {
835 return $code =~ /^$language_code_pattern$/;
838 sub otherlanguage_page ($$) {
842 return masterpage($page) if $code eq $master_language_code;
843 return masterpage($page) . '.' . $code;
846 # Returns the list of other languages codes: the master language comes first,
847 # then the codes are ordered the same way as in po_slave_languages, if it is
848 # an array, or in the language name lexical order, if it is a hash.
849 sub otherlanguages_codes ($) {
853 return \@ret unless istranslation($page) || istranslatable($page);
854 my $curlang=lang($page);
856 ($master_language_code, @slavelanguages) {
857 next if $lang eq $curlang;
858 if ($lang eq $master_language_code ||
859 istranslatedto(masterpage($page), $lang)) {
866 sub otherlanguages_pages ($) {
871 $ret{$_} = otherlanguage_page($page, $_)
872 } @{otherlanguages_codes($page)};
878 my $masterfile=shift;
880 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
881 $dir='' if $dir eq './';
882 return File::Spec->catpath('', $dir, $name . ".pot");
886 my $masterfile=shift;
889 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
890 $dir='' if $dir eq './';
891 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
895 my $masterfile=shift;
897 return map pofile($masterfile, $_), @slavelanguages;
901 my $masterfile=shift;
903 my $potfile=potfile($masterfile);
904 my $doc=Locale::Po4a::Chooser::new(po4a_type($masterfile),
905 po4a_options($masterfile));
906 $doc->{TT}{utf_mode} = 1;
907 $doc->{TT}{file_in_charset} = 'UTF-8';
908 $doc->{TT}{file_out_charset} = 'UTF-8';
909 $doc->read($masterfile);
910 # let's cheat a bit to force porefs option to be passed to
911 # Locale::Po4a::Po; this is undocument use of internal
912 # Locale::Po4a::TransTractor's data, compulsory since this module
913 # prevents us from using the porefs option.
914 $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
915 $doc->{TT}{po_out}->set_charset('UTF-8');
918 IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
919 $doc->writepo($potfile);
922 sub refreshpofiles ($@) {
923 my $masterfile=shift;
926 my $potfile=potfile($masterfile);
928 error("po(refreshpofiles) ".sprintf(gettext("POT file (%s) does not exist"), $potfile));
931 foreach my $pofile (@pofiles) {
932 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
935 # If the po file exists in an underlay, copy it
937 my ($pobase)=$pofile=~/^\Q$config{srcdir}\E\/?(.*)$/;
938 foreach my $dir (@{$config{underlaydirs}}) {
939 if (-e "$dir/$pobase") {
940 File::Copy::syscopy("$dir/$pobase",$pofile)
941 or error("po(refreshpofiles) ".
942 sprintf(gettext("failed to copy underlay PO file to %s"),
949 if (! (system("msgmerge", "--previous", "-q", "-U", "--backup=none", $pofile, $potfile) == 0)) {
950 print STDERR ("po(refreshpofiles) ". sprintf(gettext("failed to update %s"), $pofile));
954 File::Copy::syscopy($potfile,$pofile)
955 or error("po(refreshpofiles) ".
956 sprintf(gettext("failed to copy the POT file to %s"),
962 sub buildtranslationscache() {
963 # use istranslation's side-effect
964 map istranslation($_), (keys %pagesources);
967 sub resettranslationscache() {
971 sub flushmemoizecache() {
972 Memoize::flush_cache("istranslatable");
973 Memoize::flush_cache("_istranslation");
974 Memoize::flush_cache("percenttranslated");
977 sub urlto_with_orig_beautiful_urlpath($$) {
981 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
982 my $res=urlto($to, $from);
983 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
988 sub percenttranslated ($) {
992 return gettext("N/A") unless istranslation($page);
993 my $file=srcfile($pagesources{$page});
994 my $masterfile = srcfile($pagesources{masterpage($page)});
995 my $doc=Locale::Po4a::Chooser::new(po4a_type($masterfile),
996 po4a_options($masterfile));
998 'po_in_name' => [ $file ],
999 'file_in_name' => [ $masterfile ],
1000 'file_in_charset' => 'UTF-8',
1001 'file_out_charset' => 'UTF-8',
1002 ) or error("po(percenttranslated) ".
1003 sprintf(gettext("failed to translate %s"), $page));
1004 my ($percent,$hit,$queries) = $doc->stats();
1005 $percent =~ s/\.[0-9]+$//;
1009 sub languagename ($) {
1012 return $master_language_name
1013 if $code eq $master_language_code;
1014 return $slavelanguages{$code}
1015 if defined $slavelanguages{$code};
1019 sub otherlanguagesloop ($) {
1023 if (istranslation($page)) {
1025 url => urlto_with_orig_beautiful_urlpath(masterpage($page), $page),
1026 code => $master_language_code,
1027 html_code => htmllangcode($master_language_code),
1028 html_dir => htmllangdir($master_language_code),
1029 language => $master_language_name,
1033 foreach my $lang (@{otherlanguages_codes($page)}) {
1034 next if $lang eq $master_language_code;
1035 my $otherpage = otherlanguage_page($page, $lang);
1037 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
1039 html_code => htmllangcode($lang),
1040 html_dir => htmllangdir($lang),
1041 language => languagename($lang),
1042 percent => percenttranslated($otherpage),
1048 sub homepageurl (;$) {
1051 return urlto('', $page);
1054 sub ishomepage ($) {
1057 return 1 if $page eq 'index';
1058 map { return 1 if $page eq 'index.'.$_ } @slavelanguages;
1062 sub deletetranslations ($) {
1063 my $deletedmasterfile=shift;
1065 my $deletedmasterpage=pagename($deletedmasterfile);
1068 my $file = newpagefile($deletedmasterpage.'.'.$_, 'po');
1069 my $absfile = "$config{srcdir}/$file";
1070 if (-e $absfile && ! -l $absfile && ! -d $absfile) {
1071 push @todelete, $file;
1077 IkiWiki::rcs_remove($_);
1080 IkiWiki::prune("$config{srcdir}/$_", $config{srcdir});
1086 gettext("removed obsolete PO files"));
1090 sub commit_and_refresh ($) {
1094 IkiWiki::disable_commit_hook();
1095 IkiWiki::rcs_commit_staged(
1098 IkiWiki::enable_commit_hook();
1099 IkiWiki::rcs_update();
1101 # Reinitialize module's private variables.
1102 resettranslationscache();
1103 flushmemoizecache();
1104 # Trigger a wiki refresh.
1105 require IkiWiki::Render;
1106 # without preliminary saveindex/loadindex, refresh()
1107 # complains about a lot of uninitialized variables
1108 IkiWiki::saveindex();
1109 IkiWiki::loadindex();
1111 IkiWiki::saveindex();
1114 sub po_to_markup ($$) {
1115 my ($page, $content) = (shift, shift);
1117 $content = '' unless defined $content;
1118 $content = decode_utf8(encode_utf8($content));
1119 # CRLF line terminators make poor Locale::Po4a feel bad
1120 $content=~s/\r\n/\n/g;
1122 # There are incompatibilities between some File::Temp versions
1123 # (including 0.18, bundled with Lenny's perl-modules package)
1124 # and others (e.g. 0.20, previously present in the archive as
1125 # a standalone package): under certain circumstances, some
1126 # return a relative filename, whereas others return an absolute one;
1127 # we here use this module in a way that is at least compatible
1128 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
1129 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
1130 DIR => File::Spec->tmpdir,
1131 UNLINK => 1)->filename;
1132 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
1133 DIR => File::Spec->tmpdir,
1134 UNLINK => 1)->filename;
1136 my $fail = sub ($) {
1137 my $msg = "po(po_to_markup) - $page : " . shift;
1138 error($msg, sub { unlink $infile, $outfile});
1141 writefile(basename($infile), File::Spec->tmpdir, $content)
1142 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
1144 my $masterfile = srcfile($pagesources{masterpage($page)});
1145 my $doc=Locale::Po4a::Chooser::new(po4a_type($masterfile),
1146 po4a_options($masterfile));
1148 'po_in_name' => [ $infile ],
1149 'file_in_name' => [ $masterfile ],
1150 'file_in_charset' => 'UTF-8',
1151 'file_out_charset' => 'UTF-8',
1152 ) or return $fail->(gettext("failed to translate"));
1153 $doc->write($outfile)
1154 or return $fail->(sprintf(gettext("failed to write %s"), $outfile));
1156 $content = readfile($outfile);
1158 # Unlinking should happen automatically, thanks to File::Temp,
1159 # but it does not work here, probably because of the way writefile()
1160 # and Locale::Po4a::write() work.
1161 unlink $infile, $outfile;
1166 # returns a SuccessReason or FailReason object
1168 my $content = shift;
1170 # NB: we don't use po_to_markup here, since Po4a parser does
1171 # not mind invalid PO content
1172 $content = '' unless defined $content;
1173 $content = decode_utf8(encode_utf8($content));
1175 # There are incompatibilities between some File::Temp versions
1176 # (including 0.18, bundled with Lenny's perl-modules package)
1177 # and others (e.g. 0.20, previously present in the archive as
1178 # a standalone package): under certain circumstances, some
1179 # return a relative filename, whereas others return an absolute one;
1180 # we here use this module in a way that is at least compatible
1181 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
1182 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-isvalidpo.XXXXXXXXXX",
1183 DIR => File::Spec->tmpdir,
1184 UNLINK => 1)->filename;
1186 my $fail = sub ($) {
1187 my $msg = '[po/isvalidpo] ' . shift;
1189 return IkiWiki::FailReason->new("$msg");
1192 writefile(basename($infile), File::Spec->tmpdir, $content)
1193 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
1195 my $res = (system("msgfmt", "--check", $infile, "-o", "/dev/null") == 0);
1197 # Unlinking should happen automatically, thanks to File::Temp,
1198 # but it does not work here, probably because of the way writefile()
1199 # and Locale::Po4a::write() work.
1203 return IkiWiki::SuccessReason->new("valid gettext data");
1205 return IkiWiki::FailReason->new(gettext("invalid gettext data, go back ".
1206 "to previous page to continue edit"));
1212 my $pagetype = pagetype($file);
1213 if ($pagetype eq 'html') {
1219 sub po4a_options($) {
1223 my $pagetype = pagetype($file);
1225 if ($pagetype eq 'html') {
1226 # how to disable options is not consistent across po4a modules
1227 $options{includessi} = '';
1228 $options{includeexternal} = 0;
1229 $options{ontagerror} = 'warn';
1231 elsif ($pagetype eq 'mdwn') {
1232 $options{markdown} = 1;
1235 $options{markdown} = 0;
1241 sub splitlangpair ($) {
1244 my ($code, $name) = ( $pair =~ /^($language_code_pattern)\|(.+)$/ );
1245 if (! defined $code || ! defined $name ||
1246 ! length $code || ! length $name) {
1247 # not a fatal error to avoid breaking if used with web setup
1248 warn sprintf(gettext("%s has invalid syntax: must use CODE|NAME"),
1252 return $code, $name;
1255 sub joinlangpair ($$) {
1259 return "$code|$name";
1266 package IkiWiki::PageSpec;
1268 sub match_istranslation ($;@) {
1271 if (IkiWiki::Plugin::po::istranslation($page)) {
1272 return IkiWiki::SuccessReason->new("is a translation page");
1275 return IkiWiki::FailReason->new("is not a translation page");
1279 sub match_istranslatable ($;@) {
1282 if (IkiWiki::Plugin::po::istranslatable($page)) {
1283 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
1286 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
1290 sub match_lang ($$;@) {
1294 my $regexp=IkiWiki::glob2re($wanted);
1295 my $lang=IkiWiki::Plugin::po::lang($page);
1296 if ($lang !~ $regexp) {
1297 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
1300 return IkiWiki::SuccessReason->new("file language is $wanted");
1304 sub match_currentlang ($$;@) {
1309 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
1311 my $currentlang=IkiWiki::Plugin::po::lang($params{location});
1312 my $lang=IkiWiki::Plugin::po::lang($page);
1314 if ($lang eq $currentlang) {
1315 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
1318 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
1322 sub match_needstranslation ($$;@) {
1326 if (defined $wanted && $wanted ne "") {
1327 if ($wanted !~ /^\d+$/) {
1328 return IkiWiki::FailReason->new("parameter is not an integer");
1330 elsif ($wanted > 100) {
1331 return IkiWiki::FailReason->new("parameter is greater than 100");
1338 my $percenttranslated=IkiWiki::Plugin::po::percenttranslated($page);
1339 if ($percenttranslated eq 'N/A') {
1340 return IkiWiki::FailReason->new("file is not a translatable page");
1342 elsif ($percenttranslated < $wanted) {
1343 return IkiWiki::SuccessReason->new("file has $percenttranslated translated");
1346 return IkiWiki::FailReason->new("file is translated enough");