hgbook

view tools/po4a/po4a-translate @ 623:082bb76417f1

Add Po4a 0.37-dev(2009-03-08)
author Dongsheng Song <dongsheng.song@gmail.com>
date Thu Mar 12 15:43:56 2009 +0800 (2009-03-12)
parents
children
line source
1 #! /usr/bin/env perl
2 eval 'exec perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
5 # po4a-translate -- translate doc files using a message catalog(ie, po file)
6 # $Id: po4a-translate,v 1.41 2009-03-07 12:33:10 nekral-guest Exp $
7 #
8 # Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
9 #
10 # This program is free software; you can redistribute it and/or modify it
11 # under the terms of GPL (see COPYING).
13 =head1 NAME
15 po4a-translate - convert a po file back to documentation format
17 =head1 SYNOPSIS
19 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>
21 (XX.doc is the output, all others are inputs)
23 =head1 DESCRIPTION
25 The po4a (po for anything) project goal is to ease translations (and more
26 interestingly, the maintenance of translations) using gettext tools on
27 areas where they were not expected like documentation.
29 The C<po4a-translate> script is in charge of converting the translation
30 (which was done in a po file) under the documentation format back. The
31 provided C<po> file should be the translation of the C<pot> file which were
32 produced by po4a-gettextize(1).
34 =head1 OPTIONS
36 =over 4
38 =item -f, --format
40 Format of the documentation you want to handle. Use the --help-format
41 option to see the list of available formats.
43 =item -a, --addendum
45 Add a file to the resulting file (to put translator's name or a section
46 "About this translation", for example). The first line of the file to insert
47 should be a PO4A header indicating where it should be added (see section
48 I<HOWTO add extra text to translations> in po4a(7)).
50 =item -A, --addendum-charset
52 Charset of the addenda. Note that all the addenda should be in the same
53 charset.
55 =item -m, --master
57 File containing the master document to translate.
59 =item -M, --master-charset
61 Charset of the file containing the document to translate.
63 =item -l, --localized
65 File where the localized (translated) document should be written.
67 =item -L, --localized-charset
69 Charset of the file containing the localized document.
71 =item -p, --po
73 File from which the message catalog should be read.
75 =item -o, --option
77 Extra option(s) to pass to the format plugin. Specify each option in the
78 'name=value' format. See the documentation of each plugin for more
79 information about the valid options and their meanings.
81 =item -k, --keep
83 Minimal threshold for translation percentage to keep (ie, write) the
84 resulting file (default: 80). Ie, by default, files have to be translated
85 at at least 80% to get written.
87 =item -w, --width
89 Column at which we should wrap the resulting file.
91 =item -h, --help
93 Show a short help message.
95 =item --help-format
97 List the documentation format understood by po4a.
99 =item -V, --version
101 Display the version of the script and exit.
103 =item -v, --verbose
105 Increase the verbosity of the program.
107 =item -d, --debug
109 Output some debugging information.
111 =back
113 =head1 Adding content (beside translations) to generated files
115 To add some extra content to the generated document beside what you
116 translated (like the name of the translator, or a "about this translation"
117 section), you should use the C<--addendum> option.
119 The first line of the addendum must be a header indicating where to put
120 it in the document (it can be before or after a given part of the
121 document). The rest of the file will be added verbatim to the resulting
122 file without further processing.
124 Note that if po4a-translate fails to add one of the given files, it discards
125 the whole translation (because the missing file could be the one indicating
126 the author, what would prevent the users to contact him to report bugs in
127 the translation).
129 The header has a pretty rigid syntax. For more information on how to use
130 this feature and how it works, please refer to the po4a(7) man page.
132 =head1 SEE ALSO
134 L<po4a(7)>, L<po4a-gettextize(1)>, L<po4a-updatepo(1)>, L<po4a-normalize(1)>.
137 =head1 AUTHORS
139 Denis Barbier <barbier@linuxfr.org>
140 Martin Quinson (mquinson#debian.org)
142 =head1 COPYRIGHT AND LICENSE
144 Copyright 2002, 2003, 2004 by SPI, inc.
146 This program is free software; you may redistribute it and/or modify it
147 under the terms of GPL (see the COPYING file).
149 =cut
151 use 5.006;
152 use strict;
153 use warnings;
155 use Locale::Po4a::Chooser;
156 use Locale::Po4a::TransTractor;
157 use Locale::Po4a::Common;
159 use Pod::Usage qw(pod2usage);
160 use Getopt::Long qw(GetOptions);
162 Locale::Po4a::Common::textdomain("po4a");
164 sub show_version {
165 Locale::Po4a::Common::show_version("po4a-translate");
166 exit 0;
167 }
170 Getopt::Long::Configure('no_auto_abbrev','no_ignore_case');
171 my ($outfile,$width,$threshold)=('-',80,80);
172 my ($help,$help_fmt,@verbose,$debug,@addfiles,$format,@options);
173 my ($master_filename,$po_filename);
174 my ($mastchar,$locchar,$addchar);
175 GetOptions(
176 'help|h' => \$help,
177 'help-format' => \$help_fmt,
179 'master|m=s' => \$master_filename,
180 'localized|l=s' => \$outfile,
181 'po|p=s' => \$po_filename,
182 'addendum|a=s' => \@addfiles,
183 'format|f=s' => \$format,
185 'master-charset|M=s' => \$mastchar,
186 'localized-charset|L=s' => \$locchar,
187 'addendum-charset|A=s' => \$addchar,
189 'option|o=s' => \@options,
191 'width|w=s' => \$width,
192 'verbose|v' => \@verbose,
193 'debug|d' => \$debug,
194 'keep|k=s' => \$threshold,
196 'version|V' => \&show_version
197 ) or pod2usage();
199 $help && pod2usage(-verbose => 1, -exitval => 0);
200 $help_fmt && Locale::Po4a::Chooser::list(0);
202 (defined($master_filename) && length($master_filename))||pod2usage();
203 (defined($po_filename) && length($po_filename)) ||pod2usage();
204 -e $master_filename || die wrap_msg(gettext("File %s does not exist."), $master_filename);
205 -e $po_filename || die wrap_msg(gettext("File %s does not exist."), $po_filename);
207 my (@pos,@masters);
208 push @pos,$po_filename;
209 push @masters,$master_filename;
211 my %options = (
212 "verbose" => scalar @verbose,
213 "debug" => $debug);
215 foreach (@options) {
216 if (m/^([^=]*)=(.*)$/) {
217 $options{$1}="$2";
218 } else {
219 $options{$_}=1;
220 }
221 }
222 # parser
223 my $doc=Locale::Po4a::Chooser::new($format,%options);
226 # Prepare the document to be used as translator, but not parser
227 $doc->process('po_in_name' => \@pos,
228 'file_in_name' => \@masters,
229 'file_in_charset' => $mastchar,
230 'file_out_charset' => $locchar,
231 'addendum_charset' => $addchar);
233 my ($percent,$hit,$queries) = $doc->stats();
234 my $error=0;
236 print STDERR wrap_msg(gettext("%s is %s%% translated (%s of %s strings)."),
237 $master_filename, $percent, $hit, $queries)
238 if (scalar @verbose) && ($percent>=$threshold);
241 if ($percent<$threshold) {
242 print STDERR wrap_msg(gettext("Discard the translation of %s (only %s%% translated; need %s%%)."),
243 $master_filename, $percent, $threshold);
244 unlink($outfile) if (-e $outfile);
245 } else {
246 foreach my $add (@addfiles) {
247 unless ($doc->addendum($add)) {
248 unlink($outfile) if (-e $outfile);
249 die wrap_msg(gettext("Discard the translation of %s (addendum %s does not apply)."),
250 $master_filename, $add);
251 }
252 }
253 $doc->write($outfile);
254 }
256 1;