-
Notifications
You must be signed in to change notification settings - Fork 62
/
po4a-translate
executable file
·312 lines (228 loc) · 9.08 KB
/
po4a-translate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#! /usr/bin/env perl
eval 'exec perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# po4a-translate -- translate doc files using a message catalog(ie, PO file)
#
# Copyright 2002-2023 by SPI, inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of GPL v2.0 or later (see COPYING).
=encoding UTF-8
=head1 NAME
po4a-translate - convert a PO file back to documentation format
=head1 SYNOPSIS
B<po4a-translate> B<-f> I<fmt> B<-m> I<master.doc> B<-p> I<XX.po> B<-l> I<XX.doc>
(I<XX.doc> is the output, all others are inputs)
=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 B<po4a-translate> script is in charge of converting the translation
(which was done in a PO file) back into the documentation format. The
provided PO file should be the translation of the POT file which was
produced by L<po4a-gettextize(1)>.
=head1 OPTIONS
=over 4
=item B<-f>, B<--format>
Format of the documentation you want to handle. Use the B<--help-format>
option to see the list of available formats.
=item B<-a>, B<--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
B<HOWTO add extra text to translations> in L<po4a(7)>).
=item B<-A>, B<--addendum-charset>
Charset of the addenda. Note that all the addenda should be in the same
charset.
=item B<-m>, B<--master>
File containing the master document to translate.
=item B<-M>, B<--master-charset>
Charset of the file containing the document to translate.
=item B<-l>, B<--localized>
File where the localized (translated) document should be written.
=item B<-L>, B<--localized-charset>
Charset of the file containing the localized document.
=item B<-p>, B<--po>
File from which the message catalog should be read.
=item B<-o>, B<--option>
Extra option(s) to pass to the format plugin. See the documentation of each
plugin for more information about the valid options and their meanings. For
example, you could pass '-o tablecells' to the AsciiDoc parser, while the
text parser would accept '-o tabs=split'.
=item B<-k>, B<--keep>
Minimal threshold for translation percentage to keep (i.e. write) the
resulting file (default: 80). I.e. by default, files have to be translated
at least at 80% to get written.
=item B<-w>, B<--width>
Column at which we should wrap the resulting file if the format supports it (default: 76).
If the provided value is 0 or negative, then the resulting file will not be wraped at all.
This is useful with some formats that could break when rewraped.
=item B<-h>, B<--help>
Show a short help message.
=item B<--help-format>
List the documentation formats understood by po4a.
=item B<-V>, B<--version>
Display the version of the script and exit.
=item B<-v>, B<--verbose>
Increase the verbosity of the program.
=item B<-d>, B<--debug>
Output some debugging information.
=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 an "About this translation"
section), you should use the B<--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 fails to add one of the given files, 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 has a pretty rigid syntax. For more information on how to use
this feature and how it works, please refer to the L<po4a(7)> man page.
=head1 SEE ALSO
L<po4a-gettextize(1)>,
L<po4a-normalize(1)>,
L<po4a-updatepo(1)>,
L<po4a(7)>
=head1 AUTHORS
Denis Barbier <barbier@linuxfr.org>
Nicolas François <nicolas.francois@centraliens.net>
Martin Quinson (mquinson#debian.org)
=head1 COPYRIGHT AND LICENSE
Copyright 2002-2023 by SPI, inc.
This program is free software; you may redistribute it and/or modify it
under the terms of GPL v2.0 or later (see the COPYING file).
=cut
use 5.16.0;
use strict;
use warnings;
use Locale::Po4a::Chooser;
use Locale::Po4a::TransTractor;
use Locale::Po4a::Common qw(wrap_msg gettext);
use Pod::Usage qw(pod2usage);
use Getopt::Long qw(GetOptions);
Locale::Po4a::Common::textdomain("po4a");
sub show_version {
Locale::Po4a::Common::show_version("po4a-translate");
exit 0;
}
Getopt::Long::Configure( 'no_auto_abbrev', 'no_ignore_case' );
my ( $outfile, $width, $threshold ) = ( '-', 76, 80 );
my ( $help, $help_fmt, @verbose, $debug, $ignored_wrappo, @addfiles, $format, @options, $no_deprecation );
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,
# undocumented option to silence the warning about po4a-translate being deprecated
'no-deprecation' => \$no_deprecation,
# undocumented and ignored option that we must accept for the tests to pass it to the other scripts
'wrap-po=s' => \$ignored_wrappo,
'width|w=s' => \$width,
'verbose|v' => \@verbose,
'debug|d' => \$debug,
'keep|k=s' => \$threshold,
'version|V' => \&show_version
) or pod2usage();
$help && pod2usage( -verbose => 1, -exitval => 0 );
$help_fmt && Locale::Po4a::Chooser::list(0);
( defined($master_filename) && length($master_filename) ) || pod2usage();
( defined($po_filename) && length($po_filename) ) || pod2usage();
-e $master_filename || die wrap_msg( gettext("File %s does not exist."), $master_filename );
-e $po_filename || die wrap_msg( gettext("File %s does not exist."), $po_filename );
my ( @pos, @masters );
push @pos, $po_filename;
push @masters, $master_filename;
my %options = (
"verbose" => scalar @verbose,
"debug" => $debug,
"wrapcol" => $width
);
foreach (@options) {
if (m/^([^=]*)=(.*)$/) {
$options{$1} = "$2";
} else {
$options{$_} = 1;
}
}
unless ($no_deprecation) {
print wrap_msg(
gettext(
"po4a-translate is deprecated. The unified po4a(1) program is more convenient and less error prone. "
. "Once configured, `po4a --no-update` can be used as a drop-in replacement to `po4a-translate`."
)
);
}
# 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 wrap_msg( gettext("%s is %s%% translated (%s of %s strings)."),
$master_filename, $percent, $hit, $queries )
if ( scalar @verbose ) && ( $percent >= $threshold );
if ( $percent < $threshold ) {
print STDERR wrap_msg( gettext("Discard the translation of %s (only %s%% translated; need %s%%)."),
$master_filename, $percent, $threshold );
unlink($outfile) if ( -e $outfile );
} else {
my %discarded = ();
my @files = ();
while (@addfiles) {
my $add = shift(@addfiles);
my $modifiers;
$add =~ s/^([@!?]+)// and $modifiers = $1;
next if defined( $discarded{$add} );
if ( defined $modifiers ) {
if ( $modifiers =~ m/!/ ) {
$discarded{$add} = 1;
next;
}
next if ( $modifiers =~ m/\?/ and not -e $add );
if ( $modifiers =~ m/@/ ) {
open LIST, "<", "$add" or die wrap_msg( gettext("Cannot open %s: %s"), $add, $! );
my @new_list = ();
while (<LIST>) {
chomp;
s/\s+$//;
next if length($_) == 0 or $_ =~ m/^\s*#/;
push( @new_list, $_ );
}
close LIST;
unshift( @addfiles, @new_list );
} else {
push @files, $add;
}
} else {
push @files, $add;
}
}
for my $add (@files) {
unless ( $doc->addendum($add) ) {
unlink($outfile) if ( -e $outfile );
die wrap_msg( gettext("Discard the translation of %s (addendum %s does not apply)."),
$master_filename, $add );
}
}
$doc->write( $outfile, $locchar // '' );
}
1;