#! /usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;

# po4a-translate -- translate doc files using a message catalog(ie, po file)
# $Id: po4a-translate,v 1.30 2004/11/06 20:29:53 jvprat-guest Exp $
#
# Copyright 2002, 2003, 2004 by Martin Quinson <Martin.Quinson@ens-lyon.fr>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of GPL (see COPYING).

my $VERSION=$Locale::Po4a::TransTractor::VERSION;

=head1 NAME

po4a-translate - convert back po file to documentation format.

=head1 SYNOPSIS

po4a-translate -f E<lt>fmtE<gt> -m E<lt>master.docE<gt> -p E<lt>XX.poE<gt> -l E<lt>XX.docE<gt>

(XX.doc is the output, all others are input)

=head1 DESCRIPTION

The po4a (po for anything) project goal is to ease translations (and more
interestingly, the maintenance of translations) using gettext tools on
areas where they were not expected like documentation.

The C<po4a-translate> script is in charge of converting the translation
(which were done in a po file) under the documentation format back. The
provided C<po> file should be the translation of the C<pot> file which were
produced by po4a-gettextize(1).

=head1 OPTIONS

=over 4

=item -f, --format

Format type of the documentation you want to handle. Use the --help-format
option to see the list of available formats.

=item -a, --addendum

Add a file to the resulting file (to put translator's name or a section
"About this translation", for example). The first line of the file to insert
should be a PO4A header indicating where it should be added (see section
I<How to add stuff which is not a translation> in po4a(7)).

=item -A, --addendum-charset

Charset of the addenda. Note that all addenda should be in the same charset.

=item -m, --master

File containing the master document to translate.

=item -M, --master-charset

Charset of the file containing the document to translate.

=item -l, --localized

File where the localized (translated) document should be written.

=item -L, --localized-charset

Charset of the file containing the localized document.

=item -p, --po

File from which the message catalog should be read.

=item -o, --option

Pass an extra option to the format plugin. See the documentation of each
plugin for more information about the valid options and their meaning.

=item -k, --keep

Minimal threshold for translation percentage to keep (ie, write) the
resulting file (default: 80). Ie, by default, files have to be translated
at at least 80% to get written.

=item -w, --width

Column to which we should wrap the resulting file.

=item -h, --help

Show a short help message.

=item --help-format

List the documentation format understood by po4a.

=item -V, --version

Displays the version of the script and exits.

=item -v, --verbose

Increase the verbosity of the program.

=item -d, --debug

Outputs some debugging informations.

=back

=head1 Adding content (beside translations) to generated files

To add some extra content to the generated document beside what you
translated (like the name of the translator, or a "about this translation"
section), you should use the C<--addendum> option. 

The first line of the addendum must be a header indicating where to put
it in the document (it can be before or after a given part of the
document).  The rest of the file will be added verbatim to the resulting
file without further processing.

Note that if po4a-translate fail to add one of the given file, it discards
the whole translation (because the missing file could be the one indicating
the author, what would prevent the users to contact him to report bugs in
the translation).

The header have a pretty rigid syntax. For more information on how to use
this feature and how it works, please refer to the po4a(7) man page.

=head1 SEE ALSO

L<po4a(7)>, L<po4a-gettextize(1)>, L<po4a-updatepo(1)>, L<po4a-normalize(1)>.


=head1 AUTHORS

 Denis Barbier <barbier@linuxfr.org>
 Martin Quinson <martin.quinson@tuxfamily.org>

=head1 COPYRIGHT AND LICENSE

Copyright 2002, 2003, 2004 by SPI, inc.

This program is free software; you may redistribute it and/or modify it
under the terms of GPL (see COPYING file).

=cut

use 5.006;
use strict;
use warnings;

use Locale::Po4a::Chooser;
use Locale::Po4a::TransTractor;

use Pod::Usage qw(pod2usage);
use Getopt::Long qw(GetOptions);

use Locale::gettext;
use POSIX;     # Needed for setlocale()

setlocale(LC_ALL, "");
textdomain("po4a");

sub show_version {
    print sprintf(gettext(
          "%s version %s.\n".
          "written by Martin Quinson and Denis Barbier.\n\n".
          "Copyright (C) 2002, 2003, 2004 Software of Public Interest, Inc.\n".
          "This is free software; see source code for copying\n".
          "conditions. There is NO warranty; not even for\n".
          "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
          ),"po4a-translate",$VERSION)."\n";
    exit 0;
}


Getopt::Long::Configure('no_auto_abbrev','no_ignore_case');
my ($outfile,$width,$threshold)=('-',80,80);
my ($help,$help_fmt,@verbose,$debug,@addfiles,$format,@options);
my ($master_filename,$po_filename);
my ($mastchar,$locchar,$addchar);
GetOptions(
        'help|h'        => \$help,
        'help-format'   => \$help_fmt,

	'master|m=s'    => \$master_filename,
        'localized|l=s' => \$outfile,
	'po|p=s'        => \$po_filename,
	'addendum|a=s'  => \@addfiles,
	'format|f=s'    => \$format,

	'master-charset|M=s'    => \$mastchar,
	'localized-charset|L=s' => \$locchar,
	'addendum-charset|A=s' => \$addchar,

        'option|o=s'    => \@options,
    
	'width|w=s'     => \$width,
	'verbose|v'     => \@verbose,
	'debug|d'       => \$debug,
	'keep|k=s'      => \$threshold,
    
        'version|V'     => \&show_version
) or pod2usage(1);

$help && pod2usage(0);
$help_fmt && Locale::Po4a::Chooser::list(0);

(defined($master_filename) && length($master_filename))||pod2usage(1);
(defined($po_filename)     && length($po_filename))    ||pod2usage(1);
-e $master_filename || die sprintf(gettext("%s: file %s does not exist."),"po4a-translate",$master_filename)."\n";
-e $po_filename || die sprintf(gettext("%s: file %s does not exist."),"po4a-translate",$po_filename)."\n";

my (@pos,@masters);
push @pos,$po_filename;
push @masters,$master_filename;

my %options = (
    "verbose" => scalar @verbose,
    "debug" => $debug);

foreach (@options) {
    if (m/^([^=]*)=(.*)$/) {
	$options{$1}="$2";
    } else {
	$options{$_}=1;
    }
}
# parser
my $doc=Locale::Po4a::Chooser::new($format,%options);


# Prepare the document to be used as translator, but not parser
$doc->process('po_in_name'       => \@pos,
	      'file_in_name'     => \@masters,
	      'file_in_charset'  => $mastchar,
	      'file_out_charset' => $locchar,
	      'addendum_charset' => $addchar);

my ($percent,$hit,$queries) = $doc->stats();
my $error=0;

print STDERR sprintf(gettext("%s is %s%% translated (%s of %s strings)."),
		     $master_filename,$percent,$hit,$queries)."\n" 
    if (scalar @verbose) && ($percent>=$threshold);


if ($percent<$threshold)  {
    print STDERR sprintf(gettext("Discard the translation of %s (only %s%% translated; need %s%%)."),
			 $master_filename,$percent,$threshold)."\n";
    unlink($outfile) if (-e $outfile);
} else {
    foreach my $add (@addfiles) {
	unless ($doc->addendum($add)) {
	    unlink($outfile) if (-e $outfile);
	    die sprintf(gettext("Discard the translation of %s (addendum %s does not apply)."),
		$master_filename,$add)."\n";
	}
    }
    $doc->write($outfile);
}

1;

