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 use Locale::Po4a::Common qw(nowrapi18n !/.*/);
14 use Locale::Po4a::Chooser;
27 memoize("istranslatable");
28 memoize("_istranslation");
29 memoize("percenttranslated");
32 hook(type => "getsetup", id => "po", call => \&getsetup);
33 hook(type => "checkconfig", id => "po", call => \&checkconfig);
34 hook(type => "needsbuild", id => "po", call => \&needsbuild);
35 hook(type => "scan", id => "po", call => \&scan, last => 1);
36 hook(type => "filter", id => "po", call => \&filter);
37 hook(type => "htmlize", id => "po", call => \&htmlize);
38 hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
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 => "checkcontent", id => "po", call => \&checkcontent);
43 hook(type => "canremove", id => "po", call => \&canremove);
44 hook(type => "canrename", id => "po", call => \&canrename);
45 hook(type => "editcontent", id => "po", call => \&editcontent);
46 hook(type => "formbuilder_setup", id => "po", call => \&formbuilder_setup, last => 1);
47 hook(type => "formbuilder", id => "po", call => \&formbuilder);
49 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
50 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
51 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
52 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
53 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
54 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
55 $origsubs{'urlto'}=\&IkiWiki::urlto;
56 inject(name => "IkiWiki::urlto", call => \&myurlto);
57 $origsubs{'cgiurl'}=\&IkiWiki::cgiurl;
58 inject(name => "IkiWiki::cgiurl", call => \&mycgiurl);
67 # 2. Injected functions
68 # 3. Blackboxes for private data
83 po_master_language => {
89 description => "master language (non-PO files)",
93 po_slave_languages => {
100 description => "slave languages (PO files)",
104 po_translatable_pages => {
106 example => "* and !*/Discussion",
107 description => "PageSpec controlling which pages are translatable",
108 link => "ikiwiki/PageSpec",
114 example => "current",
115 description => "internal linking behavior (default/current/negotiated)",
122 foreach my $field (qw{po_master_language}) {
123 if (! exists $config{$field} || ! defined $config{$field}) {
124 error(sprintf(gettext("Must specify %s when using the %s plugin"),
131 or error(sprintf(gettext("%s is not a valid language code"), $_));
132 } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
134 if (! exists $config{po_translatable_pages} ||
135 ! defined $config{po_translatable_pages}) {
136 $config{po_translatable_pages}="";
138 if (! exists $config{po_link_to} ||
139 ! defined $config{po_link_to}) {
140 $config{po_link_to}='default';
142 elsif ($config{po_link_to} !~ /^(default|current|negotiated)$/) {
143 warn(sprintf(gettext('%s is not a valid value for po_link_to, falling back to po_link_to=default'),
144 $config{po_link_to}));
145 $config{po_link_to}='default';
147 elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
148 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
149 $config{po_link_to}='default';
152 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
154 # Translated versions of the underlays are added if available.
155 foreach my $underlay ("basewiki", map { m/^\Q$config{underlaydirbase}\E\/*(.*)/ } reverse @{$config{underlaydirs}}) {
156 next if $underlay=~/^locale\//;
158 # Add underlay containing the pot files.
159 #add_underlay("locale/pot/$underlay")
160 # if -d "$config{underlaydirbase}/locale/pot/$underlay";
162 # Add underlays containing the po files for slave languages.
163 foreach my $ll (keys %{$config{po_slave_languages}}) {
164 add_underlay("locale/mo/$underlay")
165 if -d "$config{underlaydirbase}/locale/mo/$underlay";
168 if ($config{po_master_language}{code} ne 'en') {
169 # Add underlay containing translated source files
170 # for the master language.
171 add_underlay("locale/$config{po_master_language}{code}/$underlay");
177 my $needsbuild=shift;
179 # backup @needsbuild content so that change() can know whether
180 # a given master page was rendered because its source file was changed
181 @origneedsbuild=(@$needsbuild);
184 buildtranslationscache();
186 # make existing translations depend on the corresponding master page
187 foreach my $master (keys %translations) {
188 map add_depends($_, $master), values %{otherlanguages($master)};
192 # Massage the recorded state of internal links so that:
193 # - it matches the actually generated links, rather than the links as written
194 # in the pages' source
195 # - backlinks are consistent in all cases
198 my $page=$params{page};
199 my $content=$params{content};
201 if (istranslation($page)) {
202 foreach my $destpage (@{$links{$page}}) {
203 if (istranslatable($destpage)) {
204 # replace one occurence of $destpage in $links{$page}
205 # (we only want to replace the one that was added by
206 # IkiWiki::Plugin::link::scan, other occurences may be
207 # there for other reasons)
208 for (my $i=0; $i<@{$links{$page}}; $i++) {
209 if (@{$links{$page}}[$i] eq $destpage) {
210 @{$links{$page}}[$i] = $destpage . '.' . lang($page);
217 elsif (! istranslatable($page) && ! istranslation($page)) {
218 foreach my $destpage (@{$links{$page}}) {
219 if (istranslatable($destpage)) {
220 # make sure any destpage's translations has
221 # $page in its backlinks
222 push @{$links{$page}},
223 values %{otherlanguages($destpage)};
229 # We use filter to convert PO to the master page's format,
230 # since the rest of ikiwiki should not work on PO files.
234 my $page = $params{page};
235 my $destpage = $params{destpage};
236 my $content = $params{content};
237 if (istranslation($page) && ! alreadyfiltered($page, $destpage)) {
238 $content = po_to_markup($page, $content);
239 setalreadyfiltered($page, $destpage);
247 my $page = $params{page};
248 my $content = $params{content};
250 # ignore PO files this plugin did not create
251 return $content unless istranslation($page);
253 # force content to be htmlize'd as if it was the same type as the master page
254 return IkiWiki::htmlize($page, $page,
255 pagetype(srcfile($pagesources{masterpage($page)})),
259 sub pagetemplate (@) {
261 my $page=$params{page};
262 my $destpage=$params{destpage};
263 my $template=$params{template};
265 my ($masterpage, $lang) = istranslation($page);
267 if (istranslation($page) && $template->query(name => "percenttranslated")) {
268 $template->param(percenttranslated => percenttranslated($page));
270 if ($template->query(name => "istranslation")) {
271 $template->param(istranslation => scalar istranslation($page));
273 if ($template->query(name => "istranslatable")) {
274 $template->param(istranslatable => istranslatable($page));
276 if ($template->query(name => "HOMEPAGEURL")) {
277 $template->param(homepageurl => homepageurl($page));
279 if ($template->query(name => "otherlanguages")) {
280 $template->param(otherlanguages => [otherlanguagesloop($page)]);
281 map add_depends($page, $_), (values %{otherlanguages($page)});
283 if ($config{discussion} && istranslation($page)) {
284 my $discussionlink=gettext("discussion");
285 if ($page !~ /.*\/\Q$discussionlink\E$/i &&
286 (length $config{cgiurl} ||
287 exists $links{$masterpage."/".$discussionlink})) {
288 $template->param('discussionlink' => htmllink(
291 $masterpage . '/' . gettext("Discussion"),
294 linktext => gettext("Discussion"),
298 # Remove broken parentlink to ./index.html on home page's translations.
299 # It works because this hook has the "last" parameter set, to ensure it
300 # runs after parentlinks' own pagetemplate hook.
301 if ($template->param('parentlinks')
302 && istranslation($page)
303 && $masterpage eq "index") {
304 $template->param('parentlinks' => []);
308 # Add the renamed page translations to the list of to-be-renamed pages.
309 sub renamepages (@) {
312 my %torename = %{$params{torename}};
313 my $session = $params{session};
315 # Save the page(s) the user asked to rename, so that our
316 # canrename hook can tell the difference between:
317 # - a translation being renamed as a consequence of its master page
319 # - a user trying to directly rename a translation
320 # This is why this hook has to be run first, before the list of pages
321 # to rename is modified by other plugins.
323 @orig_torename=@{$session->param("po_orig_torename")}
324 if defined $session->param("po_orig_torename");
325 push @orig_torename, $torename{src};
326 $session->param(po_orig_torename => \@orig_torename);
327 IkiWiki::cgi_savesession($session);
329 return () unless istranslatable($torename{src});
332 my %otherpages=%{otherlanguages($torename{src})};
333 while (my ($lang, $otherpage) = each %otherpages) {
336 srcfile => $pagesources{$otherpage},
337 dest => otherlanguage($torename{dest}, $lang),
338 destfile => $torename{dest}.".".$lang.".po",
348 map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
354 # All meta titles are first extracted at scan time, i.e. before we turn
355 # PO files back into translated markdown; escaping of double-quotes in
356 # PO files breaks the meta plugin's parsing enough to save ugly titles
357 # to %pagestate at this time.
359 # Then, at render time, every page passes in turn through the Great
360 # Rendering Chain (filter->preprocess->linkify->htmlize), and the meta
361 # plugin's preprocess hook is this time in a position to correctly
362 # extract the titles from slave pages.
364 # This is, unfortunately, too late: if the page A, linking to the page
365 # B, is rendered before B, it will display the wrongly-extracted meta
366 # title as the link text to B.
368 # On the one hand, such a corner case only happens on rebuild: on
369 # refresh, every rendered page is fixed to contain correct meta titles.
370 # On the other hand, it can take some time to get every page fixed.
371 # We therefore re-render every rendered page after a rebuild to fix them
372 # at once. As this more or less doubles the time needed to rebuild the
373 # wiki, we do so only when really needed.
376 && exists $config{rebuild} && defined $config{rebuild} && $config{rebuild}
377 && UNIVERSAL::can("IkiWiki::Plugin::meta", "getsetup")
378 && exists $config{meta_overrides_page_title}
379 && defined $config{meta_overrides_page_title}
380 && $config{meta_overrides_page_title}) {
381 debug(sprintf(gettext("re-rendering all pages to fix meta titles")));
382 resetalreadyfiltered();
383 require IkiWiki::Render;
384 foreach my $file (@rendered) {
385 debug(sprintf(gettext("rendering %s"), $file));
386 IkiWiki::render($file);
390 my $updated_po_files=0;
392 # Refresh/create POT and PO files as needed.
393 # (But avoid doing so if they are in an underlay directory.)
394 foreach my $file (grep {istranslatablefile($_)} @rendered) {
395 my $masterfile=srcfile($file);
396 my $page=pagename($file);
397 my $updated_pot_file=0;
398 # Only refresh POT file if it does not exist, or if
399 # $pagesources{$page} was changed: don't if only the HTML was
400 # refreshed, e.g. because of a dependency.
401 if ($masterfile eq "$config{srcdir}/$file" &&
402 ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
403 || ! -e potfile($masterfile))) {
404 refreshpot($masterfile);
408 foreach my $po (pofiles($masterfile)) {
409 next if ! $updated_pot_file && ! -e $po;
410 next if grep { $po=~/\Q$_\E/ } @{$config{underlaydirs}};
414 refreshpofiles($masterfile, @pofiles);
415 map { IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
420 if ($updated_po_files) {
422 gettext("updated PO files"),
423 "IkiWiki::Plugin::po::change");
427 sub checkcontent (@) {
430 if (istranslation($params{page})) {
431 my $res = isvalidpo($params{content});
445 if (istranslation($params{page})) {
446 return gettext("Can not remove a translation. Removing the master page, ".
447 "though, removes its translations as well.");
454 my $session = $params{session};
456 if (istranslation($params{src})) {
457 my $masterpage = masterpage($params{src});
458 # Tell the difference between:
459 # - a translation being renamed as a consequence of its master page
460 # being renamed, which is allowed
461 # - a user trying to directly rename a translation, which is forbidden
462 # by looking for the master page in the list of to-be-renamed pages we
463 # saved early in the renaming process.
464 my $orig_torename = $session->param("po_orig_torename");
465 unless (grep { $_ eq $masterpage } @{$orig_torename}) {
466 return gettext("Can not rename a translation. Renaming the master page, ".
467 "though, renames its translations as well.");
473 # As we're previewing or saving a page, the content may have
474 # changed, so tell the next filter() invocation it must not be lazy.
478 unsetalreadyfiltered($params{page}, $params{page});
479 return $params{content};
482 sub formbuilder_setup (@) {
484 my $form=$params{form};
487 return unless defined $form->field("do");
489 if ($form->field("do") eq "create") {
490 # Warn the user: new pages must be written in master language.
491 my $template=template("pocreatepage.tmpl");
492 $template->param(LANG => $config{po_master_language}{name});
493 $form->tmpl_param(message => $template->output);
495 elsif ($form->field("do") eq "edit") {
496 # Remove the rename/remove buttons on slave pages.
497 # This has to be done after the rename/remove plugins have added
498 # their buttons, which is why this hook must be run last.
499 # The canrename/canremove hooks already ensure this is forbidden
500 # at the backend level, so this is only UI sugar.
501 if (istranslation($form->field("page"))) {
503 for (my $i = 0; $i < @{$params{buttons}}; $i++) {
504 if (@{$params{buttons}}[$i] eq $_) {
505 delete @{$params{buttons}}[$i];
514 sub formbuilder (@) {
516 my $form=$params{form};
519 return unless defined $form->field("do");
521 # Do not allow to create pages of type po: they are automatically created.
522 # The main reason to do so is to bypass the "favor the type of linking page
523 # on page creation" logic, which is unsuitable when a broken link is clicked
524 # on a slave (PO) page.
525 # This cannot be done in the formbuilder_setup hook as the list of types is
527 if ($form->field("do") eq "create") {
528 foreach my $field ($form->field) {
529 next unless "$field" eq "type";
530 if ($field->type eq 'select') {
531 # remove po from the list of types
532 my @types = grep { $_ ne 'po' } $field->options;
533 $field->options(\@types) if @types;
540 # | Injected functions
543 # Implement po_link_to 'current' and 'negotiated' settings.
544 sub mybestlink ($$) {
548 my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
550 && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
551 && istranslatable($res)
552 && istranslation($page)) {
553 return $res . "." . lang($page);
558 sub mybeautify_urlpath ($) {
561 my $res=$origsubs{'beautify_urlpath'}->($url);
562 if ($config{po_link_to} eq "negotiated") {
563 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
564 $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
566 $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
567 } (keys %{$config{po_slave_languages}});
572 sub mytargetpage ($$) {
576 if (istranslation($page) || istranslatable($page)) {
577 my ($masterpage, $lang) = (masterpage($page), lang($page));
578 if (! $config{usedirs} || $masterpage eq 'index') {
579 return $masterpage . "." . $lang . "." . $ext;
582 return $masterpage . "/index." . $lang . "." . $ext;
585 return $origsubs{'targetpage'}->($page, $ext);
593 # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
595 && $config{po_link_to} eq "current"
596 && istranslatable('index')) {
597 return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
599 # avoid using our injected beautify_urlpath if run by cgi_editpage,
600 # so that one is redirected to the just-edited page rather than to the
601 # negociated translation; to prevent unnecessary fiddling with caller/inject,
602 # we only do so when our beautify_urlpath would actually do what we want to
603 # avoid, i.e. when po_link_to = negotiated
604 if ($config{po_link_to} eq "negotiated") {
605 my @caller = caller(1);
606 my $run_by_editpage = 0;
607 $run_by_editpage = 1 if (exists $caller[3] && defined $caller[3]
608 && $caller[3] eq "IkiWiki::cgi_editpage");
609 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'})
611 my $res = $origsubs{'urlto'}->($to,$from,$absolute);
612 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath)
617 return $origsubs{'urlto'}->($to,$from,$absolute)
624 # slave pages have no subpages
625 if (istranslation($params{'from'})) {
626 $params{'from'} = masterpage($params{'from'});
628 return $origsubs{'cgiurl'}->(%params);
632 # | Blackboxes for private data
638 sub alreadyfiltered($$) {
642 return exists $filtered{$page}{$destpage}
643 && $filtered{$page}{$destpage} eq 1;
646 sub setalreadyfiltered($$) {
650 $filtered{$page}{$destpage}=1;
653 sub unsetalreadyfiltered($$) {
657 if (exists $filtered{$page}{$destpage}) {
658 delete $filtered{$page}{$destpage};
662 sub resetalreadyfiltered() {
671 sub maybe_add_leading_slash ($;$) {
674 $add=1 unless defined $add;
675 return '/' . $str if $add;
679 sub istranslatablefile ($) {
682 return 0 unless defined $file;
683 my $type=pagetype($file);
684 return 0 if ! defined $type || $type eq 'po';
685 return 0 if $file =~ /\.pot$/;
686 return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
690 sub istranslatable ($) {
694 return 1 if istranslatablefile($pagesources{$page});
698 sub _istranslation ($) {
701 $page='' unless defined $page && length $page;
702 my $hasleadingslash = ($page=~s#^/##);
703 my $file=$pagesources{$page};
704 return 0 unless defined $file
705 && defined pagetype($file)
706 && pagetype($file) eq 'po';
707 return 0 if $file =~ /\.pot$/;
709 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
710 return 0 unless defined $masterpage && defined $lang
711 && length $masterpage && length $lang
712 && defined $pagesources{$masterpage}
713 && defined $config{po_slave_languages}{$lang};
715 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
716 if istranslatable($masterpage);
719 sub istranslation ($) {
722 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
723 my $hasleadingslash = ($masterpage=~s#^/##);
724 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
725 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
733 if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
742 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
745 return $config{po_master_language}{code};
748 sub islanguagecode ($) {
751 return $code =~ /^[a-z]{2}$/;
754 sub otherlanguage ($$) {
758 return masterpage($page) if $code eq $config{po_master_language}{code};
759 return masterpage($page) . '.' . $code;
762 sub otherlanguages ($) {
766 return \%ret unless istranslation($page) || istranslatable($page);
767 my $curlang=lang($page);
769 ($config{po_master_language}{code}, keys %{$config{po_slave_languages}}) {
770 next if $lang eq $curlang;
771 $ret{$lang}=otherlanguage($page, $lang);
777 my $masterfile=shift;
779 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
780 $dir='' if $dir eq './';
781 return File::Spec->catpath('', $dir, $name . ".pot");
785 my $masterfile=shift;
788 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
789 $dir='' if $dir eq './';
790 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
794 my $masterfile=shift;
796 return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
800 my $masterfile=shift;
802 my $potfile=potfile($masterfile);
803 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
804 my $doc=Locale::Po4a::Chooser::new('text',%options);
805 $doc->{TT}{utf_mode} = 1;
806 $doc->{TT}{file_in_charset} = 'utf-8';
807 $doc->{TT}{file_out_charset} = 'utf-8';
808 $doc->read($masterfile);
809 # let's cheat a bit to force porefs option to be passed to
810 # Locale::Po4a::Po; this is undocument use of internal
811 # Locale::Po4a::TransTractor's data, compulsory since this module
812 # prevents us from using the porefs option.
813 $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
814 $doc->{TT}{po_out}->set_charset('utf-8');
817 IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
818 $doc->writepo($potfile);
821 sub refreshpofiles ($@) {
822 my $masterfile=shift;
825 my $potfile=potfile($masterfile);
827 error("po(refreshpofiles) ".sprintf(gettext("POT file (%s) does not exist"), $potfile));
830 foreach my $pofile (@pofiles) {
831 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
833 system("msgmerge", "-q", "-U", "--backup=none", $pofile, $potfile) == 0
834 or error("po(refreshpofiles) ".
835 sprintf(gettext("failed to update %s"),
839 File::Copy::syscopy($potfile,$pofile)
840 or error("po(refreshpofiles) ".
841 sprintf(gettext("failed to copy the POT file to %s"),
847 sub buildtranslationscache() {
848 # use istranslation's side-effect
849 map istranslation($_), (keys %pagesources);
852 sub resettranslationscache() {
856 sub flushmemoizecache() {
857 Memoize::flush_cache("istranslatable");
858 Memoize::flush_cache("_istranslation");
859 Memoize::flush_cache("percenttranslated");
862 sub urlto_with_orig_beautiful_urlpath($$) {
866 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
867 my $res=urlto($to, $from);
868 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
873 sub percenttranslated ($) {
877 return gettext("N/A") unless istranslation($page);
878 my $file=srcfile($pagesources{$page});
879 my $masterfile = srcfile($pagesources{masterpage($page)});
881 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
883 my $doc=Locale::Po4a::Chooser::new('text',%options);
885 'po_in_name' => [ $file ],
886 'file_in_name' => [ $masterfile ],
887 'file_in_charset' => 'utf-8',
888 'file_out_charset' => 'utf-8',
889 ) or error("po(percenttranslated) ".
890 sprintf(gettext("failed to translate %s"), $page));
891 my ($percent,$hit,$queries) = $doc->stats();
892 $percent =~ s/\.[0-9]+$//;
896 sub languagename ($) {
899 return $config{po_master_language}{name}
900 if $code eq $config{po_master_language}{code};
901 return $config{po_slave_languages}{$code}
902 if defined $config{po_slave_languages}{$code};
906 sub otherlanguagesloop ($) {
910 my %otherpages=%{otherlanguages($page)};
911 while (my ($lang, $otherpage) = each %otherpages) {
912 if (istranslation($page) && masterpage($page) eq $otherpage) {
914 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
916 language => languagename($lang),
922 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
924 language => languagename($lang),
925 percent => percenttranslated($otherpage),
930 return -1 if $a->{code} eq $config{po_master_language}{code};
931 return 1 if $b->{code} eq $config{po_master_language}{code};
932 return $a->{language} cmp $b->{language};
936 sub homepageurl (;$) {
939 return urlto('', $page);
942 sub deletetranslations ($) {
943 my $deletedmasterfile=shift;
945 my $deletedmasterpage=pagename($deletedmasterfile);
948 my $file = newpagefile($deletedmasterpage.'.'.$_, 'po');
949 my $absfile = "$config{srcdir}/$file";
950 if (-e $absfile && ! -l $absfile && ! -d $absfile) {
951 push @todelete, $file;
953 } keys %{$config{po_slave_languages}};
957 IkiWiki::rcs_remove($_);
960 IkiWiki::prune("$config{srcdir}/$_");
966 gettext("removed obsolete PO files"),
967 "IkiWiki::Plugin::po::deletetranslations");
971 sub commit_and_refresh ($$) {
972 my ($msg, $author) = (shift, shift);
975 IkiWiki::disable_commit_hook();
976 IkiWiki::rcs_commit_staged($msg, $author, "127.0.0.1");
977 IkiWiki::enable_commit_hook();
978 IkiWiki::rcs_update();
980 # Reinitialize module's private variables.
981 resetalreadyfiltered();
982 resettranslationscache();
984 # Trigger a wiki refresh.
985 require IkiWiki::Render;
986 # without preliminary saveindex/loadindex, refresh()
987 # complains about a lot of uninitialized variables
988 IkiWiki::saveindex();
989 IkiWiki::loadindex();
991 IkiWiki::saveindex();
994 # on success, returns the filtered content.
995 # on error, if $nonfatal, warn and return undef; else, error out.
996 sub po_to_markup ($$;$) {
997 my ($page, $content) = (shift, shift);
998 my $nonfatal = shift;
1000 $content = '' unless defined $content;
1001 $content = decode_utf8(encode_utf8($content));
1002 # CRLF line terminators make poor Locale::Po4a feel bad
1003 $content=~s/\r\n/\n/g;
1005 # There are incompatibilities between some File::Temp versions
1006 # (including 0.18, bundled with Lenny's perl-modules package)
1007 # and others (e.g. 0.20, previously present in the archive as
1008 # a standalone package): under certain circumstances, some
1009 # return a relative filename, whereas others return an absolute one;
1010 # we here use this module in a way that is at least compatible
1011 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
1012 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
1013 DIR => File::Spec->tmpdir,
1014 UNLINK => 1)->filename;
1015 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
1016 DIR => File::Spec->tmpdir,
1017 UNLINK => 1)->filename;
1019 my $fail = sub ($) {
1020 my $msg = "po(po_to_markup) - $page : " . shift;
1025 error($msg, sub { unlink $infile, $outfile});
1028 writefile(basename($infile), File::Spec->tmpdir, $content)
1029 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
1031 my $masterfile = srcfile($pagesources{masterpage($page)});
1033 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
1035 my $doc=Locale::Po4a::Chooser::new('text',%options);
1037 'po_in_name' => [ $infile ],
1038 'file_in_name' => [ $masterfile ],
1039 'file_in_charset' => 'utf-8',
1040 'file_out_charset' => 'utf-8',
1041 ) or return $fail->(gettext("failed to translate"));
1042 $doc->write($outfile)
1043 or return $fail->(sprintf(gettext("failed to write %s"), $outfile));
1045 $content = readfile($outfile)
1046 or return $fail->(sprintf(gettext("failed to read %s"), $outfile));
1048 # Unlinking should happen automatically, thanks to File::Temp,
1049 # but it does not work here, probably because of the way writefile()
1050 # and Locale::Po4a::write() work.
1051 unlink $infile, $outfile;
1056 # returns a SuccessReason or FailReason object
1058 my $content = shift;
1060 # NB: we don't use po_to_markup here, since Po4a parser does
1061 # not mind invalid PO content
1062 $content = '' unless defined $content;
1063 $content = decode_utf8(encode_utf8($content));
1065 # There are incompatibilities between some File::Temp versions
1066 # (including 0.18, bundled with Lenny's perl-modules package)
1067 # and others (e.g. 0.20, previously present in the archive as
1068 # a standalone package): under certain circumstances, some
1069 # return a relative filename, whereas others return an absolute one;
1070 # we here use this module in a way that is at least compatible
1071 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
1072 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-isvalidpo.XXXXXXXXXX",
1073 DIR => File::Spec->tmpdir,
1074 UNLINK => 1)->filename;
1076 my $fail = sub ($) {
1077 my $msg = '[po/isvalidpo] ' . shift;
1079 return IkiWiki::FailReason->new("$msg");
1082 writefile(basename($infile), File::Spec->tmpdir, $content)
1083 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
1085 my $res = (system("msgfmt", "--check", $infile, "-o", "/dev/null") == 0);
1087 # Unlinking should happen automatically, thanks to File::Temp,
1088 # but it does not work here, probably because of the way writefile()
1089 # and Locale::Po4a::write() work.
1093 return IkiWiki::SuccessReason->new("valid gettext data");
1095 return IkiWiki::FailReason->new("invalid gettext data, go back ".
1096 "to previous page to go on with edit");
1103 package IkiWiki::PageSpec;
1105 sub match_istranslation ($;@) {
1108 if (IkiWiki::Plugin::po::istranslation($page)) {
1109 return IkiWiki::SuccessReason->new("is a translation page");
1112 return IkiWiki::FailReason->new("is not a translation page");
1116 sub match_istranslatable ($;@) {
1119 if (IkiWiki::Plugin::po::istranslatable($page)) {
1120 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
1123 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
1127 sub match_lang ($$;@) {
1131 my $regexp=IkiWiki::glob2re($wanted);
1132 my $lang=IkiWiki::Plugin::po::lang($page);
1133 if ($lang !~ /^$regexp$/i) {
1134 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
1137 return IkiWiki::SuccessReason->new("file language is $wanted");
1141 sub match_currentlang ($$;@) {
1146 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
1148 my $currentlang=IkiWiki::Plugin::po::lang($params{location});
1149 my $lang=IkiWiki::Plugin::po::lang($page);
1151 if ($lang eq $currentlang) {
1152 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
1155 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");