+# returns a SuccessReason or FailReason object
+sub isvalidpo ($) {
+ my $content = shift;
+
+ # NB: we don't use po_to_markup here, since Po4a parser does
+ # not mind invalid PO content
+ $content = '' unless defined $content;
+ $content = decode_utf8(encode_utf8($content));
+
+ # There are incompatibilities between some File::Temp versions
+ # (including 0.18, bundled with Lenny's perl-modules package)
+ # and others (e.g. 0.20, previously present in the archive as
+ # a standalone package): under certain circumstances, some
+ # return a relative filename, whereas others return an absolute one;
+ # we here use this module in a way that is at least compatible
+ # with 0.18 and 0.20. Beware, hit'n'run refactorers!
+ my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-isvalidpo.XXXXXXXXXX",
+ DIR => File::Spec->tmpdir,
+ UNLINK => 1)->filename;
+
+ my $fail = sub ($) {
+ my $msg = '[po/isvalidpo] ' . shift;
+ unlink $infile;
+ return IkiWiki::FailReason->new("$msg");
+ };
+
+ writefile(basename($infile), File::Spec->tmpdir, $content)
+ or return $fail->(sprintf(gettext("failed to write %s"), $infile));
+
+ 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()
+ # and Locale::Po4a::write() work.
+ unlink $infile;
+
+ if ($res) {
+ return IkiWiki::SuccessReason->new("valid gettext data");
+ }
+ return IkiWiki::FailReason->new(gettext("invalid gettext data, go back ".
+ "to previous page to continue edit"));
+}
+
+sub po4a_type ($) {
+ my $file = shift;
+
+ my $pagetype = pagetype($file);
+ if ($pagetype eq 'html') {
+ return 'xhtml';
+ }
+ return 'text';
+}
+
+sub po4a_options($) {
+ my $file = shift;
+
+ my %options;
+ my $pagetype = pagetype($file);
+
+ if ($pagetype eq 'html') {
+ # how to disable options is not consistent across po4a modules
+ $options{includessi} = '';
+ $options{includeexternal} = 0;
+ }
+ elsif ($pagetype eq 'mdwn') {
+ $options{markdown} = 1;
+ }
+ else {
+ $options{markdown} = 0;
+ }
+
+ return %options;
+}
+
+sub splitlangpair ($) {
+ my $pair=shift;
+
+ my ($code, $name) = ( $pair =~ /^([a-z]{2})\|(.+)$/ );
+ if (! defined $code || ! defined $name ||
+ ! length $code || ! length $name) {
+ # not a fatal error to avoid breaking if used with web setup
+ warn sprintf(gettext("%s has invalid syntax: must use CODE|NAME"),
+ $pair);
+ }
+
+ return $code, $name;
+}
+
+sub joinlangpair ($$) {
+ my $code=shift;
+ my $name=shift;
+
+ return "$code|$name";
+}
+