]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/po.pm
po: gettext-ize error messages
[ikiwiki.git] / IkiWiki / Plugin / po.pm
index 236b5984fd2c75df6defe3ba1279e3878edbf2ca..75344b4b625d4b14e53d9f7ca0e50362f9b51071 100644 (file)
@@ -8,7 +8,7 @@ package IkiWiki::Plugin::po;
 
 use warnings;
 use strict;
-use IkiWiki 2.00;
+use IkiWiki 3.00;
 use Encode;
 use Locale::Po4a::Chooser;
 use Locale::Po4a::Po;
@@ -43,6 +43,8 @@ sub import {
        hook(type => "canremove", id => "po", call => \&canremove);
        hook(type => "canrename", id => "po", call => \&canrename);
        hook(type => "editcontent", id => "po", call => \&editcontent);
+       hook(type => "formbuilder_setup", id => "po", call => \&formbuilder_setup);
+       hook(type => "formbuilder", id => "po", call => \&formbuilder);
 
        $origsubs{'bestlink'}=\&IkiWiki::bestlink;
        inject(name => "IkiWiki::bestlink", call => \&mybestlink);
@@ -126,11 +128,12 @@ sub getsetup () {
 sub checkconfig () {
        foreach my $field (qw{po_master_language po_slave_languages}) {
                if (! exists $config{$field} || ! defined $config{$field}) {
-                       error(sprintf(gettext("Must specify %s"), $field));
+                       error(sprintf(gettext("Must specify %s when using the %s plugin"), $field, 'po'));
                }
        }
        if (! (keys %{$config{po_slave_languages}})) {
-               error(gettext("At least one slave language must be defined in po_slave_languages"));
+               error(gettext("At least one slave language must be defined ".
+                             "in po_slave_languages when using the po plugin"));
        }
        map {
                islanguagecode($_)
@@ -439,6 +442,43 @@ sub editcontent () {
        return $params{content};
 }
 
+sub formbuilder_setup (@) {
+       my %params=@_;
+       my $form=$params{form};
+       my $q=$params{cgi};
+
+       return unless (defined $form->field("do") && $form->field("do") eq "create");
+
+       my $template=template("pocreatepage.tmpl");
+       $template->param(LANG => $config{po_master_language}{name});
+       $form->tmpl_param(message => $template->output);
+}
+
+# Do not allow to create pages of type po: they are automatically created.
+# The main reason to do so is to bypass the "favor the type of linking page
+# on page creation" logic, which is unsuitable when a broken link is clicked
+# on a slave (PO) page.
+sub formbuilder (@) {
+       my %params=@_;
+       my $form=$params{form};
+       my $q=$params{cgi};
+
+       return unless (defined $form->field("do") && $form->field("do") eq "create");
+
+        for my $field ($form->field) {
+               next unless "$field" eq "type";
+               if ($field->type eq 'select') {
+                       # remove po from the types list
+                       my @types = grep { $_ ne 'po' } $field->options;
+                       $field->options(\@types) if scalar @types;
+               }
+               else {
+                       # make sure the default value is not po;
+                       # does this case actually happen?
+                       debug sprintf("po(formbuilder) type field is not select - not implemented yet");
+               }
+       }
+}
 
 # ,----
 # | Injected functions
@@ -528,7 +568,7 @@ sub mynicepagetitle ($;$) {
        my $res = $origsubs{'nicepagetitle'}->($page, $unescaped);
        return $res unless istranslation($page);
        return $res unless $config{po_translation_status_in_links};
-       return $res.' ('.percenttranslated($page).' %)';
+       return $res.' ('.percenttranslated($page).' %)';
 }
 
 # ,----
@@ -723,17 +763,21 @@ sub refreshpofiles ($@) {
        my @pofiles=@_;
 
        my $potfile=potfile($masterfile);
-       error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
+       (-e $potfile)
+               or error(sprintf(gettext("po(refreshpofiles) POT file (%s) does not exist"),
+                                $potfile));
 
        foreach my $pofile (@pofiles) {
                IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
                if (-e $pofile) {
                        system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
-                               or error("[po/refreshpofiles:$pofile] failed to update");
+                               or error(sprintf(gettext("po(refreshpofiles) failed to update %s"),
+                                                $pofile));
                }
                else {
                        File::Copy::syscopy($potfile,$pofile)
-                               or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
+                               or error(sprintf(gettext("po(refreshpofiles) failed to copy the POT file to %s"),
+                                                $pofile));
                }
        }
 }
@@ -780,8 +824,10 @@ sub percenttranslated ($) {
                'file_in_name'  => [ $masterfile ],
                'file_in_charset'  => 'utf-8',
                'file_out_charset' => 'utf-8',
-       ) or error("[po/percenttranslated:$page]: failed to translate");
+       ) or error(sprintf(gettext("po(percenttranslated) failed to translate %s"),
+                          $page));
        my ($percent,$hit,$queries) = $doc->stats();
+       $percent =~ s/\.[0-9]+$//;
        return $percent;
 }
 
@@ -909,7 +955,7 @@ sub po_to_markup ($$;$) {
                                     UNLINK => 1)->filename;
 
        sub failure ($) {
-               my $msg = '[po/po_to_markup:'.$page.'] ' . shift;
+               my $msg = "po(po_to_markup) - $page : " . shift;
                if ($nonfatal) {
                        warn $msg;
                        return undef;
@@ -918,7 +964,7 @@ sub po_to_markup ($$;$) {
        }
 
        writefile(basename($infile), File::Spec->tmpdir, $content)
-               or return failure("failed to write $infile");
+               or return failure(sprintf(gettext("failed to write %s"), $infile));
 
        my $masterfile = srcfile($pagesources{masterpage($page)});
        my %options = (
@@ -930,10 +976,12 @@ sub po_to_markup ($$;$) {
                'file_in_name'  => [ $masterfile ],
                'file_in_charset'  => 'utf-8',
                'file_out_charset' => 'utf-8',
-       ) or return failure("failed to translate");
-       $doc->write($outfile) or return failure("could not write $outfile");
+       ) or return failure(gettext("failed to translate"));
+       $doc->write($outfile)
+               or return failure(sprintf(gettext("failed to write %s"), $outfile));
 
-       $content = readfile($outfile) or return failure("could not read $outfile");
+       $content = readfile($outfile)
+               or return failure(sprintf(gettext("failed to read %s"), $outfile));
 
        # Unlinking should happen automatically, thanks to File::Temp,
        # but it does not work here, probably because of the way writefile()
@@ -970,9 +1018,9 @@ sub isvalidpo ($) {
        }
 
        writefile(basename($infile), File::Spec->tmpdir, $content)
-               or return failure("failed to write $infile");
+               or return failure(sprintf(gettext("failed to write %s"), $infile));
 
-       my $res = (system("msgfmt", "--check", $infile) == 0);
+       my $res = (system("msgfmt", "--check", $infile, "-o", "/dev/null") == 0);
 
        # Unlinking should happen automatically, thanks to File::Temp,
        # but it does not work here, probably because of the way writefile()