comparison tools/po4a/po4a-translate @ 722:082bb76417f1

Add Po4a 0.37-dev(2009-03-08)
author Dongsheng Song <dongsheng.song@gmail.com>
date Thu, 12 Mar 2009 15:43:56 +0800
parents
children
comparison
equal deleted inserted replaced
721:2180358c32c4 722:082bb76417f1
1 #! /usr/bin/env perl
2 eval 'exec perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
4
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).
12
13 =head1 NAME
14
15 po4a-translate - convert a po file back to documentation format
16
17 =head1 SYNOPSIS
18
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>
20
21 (XX.doc is the output, all others are inputs)
22
23 =head1 DESCRIPTION
24
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.
28
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).
33
34 =head1 OPTIONS
35
36 =over 4
37
38 =item -f, --format
39
40 Format of the documentation you want to handle. Use the --help-format
41 option to see the list of available formats.
42
43 =item -a, --addendum
44
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)).
49
50 =item -A, --addendum-charset
51
52 Charset of the addenda. Note that all the addenda should be in the same
53 charset.
54
55 =item -m, --master
56
57 File containing the master document to translate.
58
59 =item -M, --master-charset
60
61 Charset of the file containing the document to translate.
62
63 =item -l, --localized
64
65 File where the localized (translated) document should be written.
66
67 =item -L, --localized-charset
68
69 Charset of the file containing the localized document.
70
71 =item -p, --po
72
73 File from which the message catalog should be read.
74
75 =item -o, --option
76
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.
80
81 =item -k, --keep
82
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.
86
87 =item -w, --width
88
89 Column at which we should wrap the resulting file.
90
91 =item -h, --help
92
93 Show a short help message.
94
95 =item --help-format
96
97 List the documentation format understood by po4a.
98
99 =item -V, --version
100
101 Display the version of the script and exit.
102
103 =item -v, --verbose
104
105 Increase the verbosity of the program.
106
107 =item -d, --debug
108
109 Output some debugging information.
110
111 =back
112
113 =head1 Adding content (beside translations) to generated files
114
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.
118
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.
123
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).
128
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.
131
132 =head1 SEE ALSO
133
134 L<po4a(7)>, L<po4a-gettextize(1)>, L<po4a-updatepo(1)>, L<po4a-normalize(1)>.
135
136
137 =head1 AUTHORS
138
139 Denis Barbier <barbier@linuxfr.org>
140 Martin Quinson (mquinson#debian.org)
141
142 =head1 COPYRIGHT AND LICENSE
143
144 Copyright 2002, 2003, 2004 by SPI, inc.
145
146 This program is free software; you may redistribute it and/or modify it
147 under the terms of GPL (see the COPYING file).
148
149 =cut
150
151 use 5.006;
152 use strict;
153 use warnings;
154
155 use Locale::Po4a::Chooser;
156 use Locale::Po4a::TransTractor;
157 use Locale::Po4a::Common;
158
159 use Pod::Usage qw(pod2usage);
160 use Getopt::Long qw(GetOptions);
161
162 Locale::Po4a::Common::textdomain("po4a");
163
164 sub show_version {
165 Locale::Po4a::Common::show_version("po4a-translate");
166 exit 0;
167 }
168
169
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,
178
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,
184
185 'master-charset|M=s' => \$mastchar,
186 'localized-charset|L=s' => \$locchar,
187 'addendum-charset|A=s' => \$addchar,
188
189 'option|o=s' => \@options,
190
191 'width|w=s' => \$width,
192 'verbose|v' => \@verbose,
193 'debug|d' => \$debug,
194 'keep|k=s' => \$threshold,
195
196 'version|V' => \&show_version
197 ) or pod2usage();
198
199 $help && pod2usage(-verbose => 1, -exitval => 0);
200 $help_fmt && Locale::Po4a::Chooser::list(0);
201
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);
206
207 my (@pos,@masters);
208 push @pos,$po_filename;
209 push @masters,$master_filename;
210
211 my %options = (
212 "verbose" => scalar @verbose,
213 "debug" => $debug);
214
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);
224
225
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);
232
233 my ($percent,$hit,$queries) = $doc->stats();
234 my $error=0;
235
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);
239
240
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 }
255
256 1;
257