hgbook
diff tools/po4a/po4a-translate @ 651:c15e039d98b8
appB-mq-ref.xml: Typo fix
author | Dongsheng Song <dongsheng.song@gmail.com> |
---|---|
date | Fri Mar 20 16:50:51 2009 +0800 (2009-03-20) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/po4a/po4a-translate Fri Mar 20 16:50:51 2009 +0800 1.3 @@ -0,0 +1,257 @@ 1.4 +#! /usr/bin/env perl 1.5 +eval 'exec perl -S $0 ${1+"$@"}' 1.6 + if $running_under_some_shell; 1.7 + 1.8 +# po4a-translate -- translate doc files using a message catalog(ie, po file) 1.9 +# $Id: po4a-translate,v 1.41 2009-03-07 12:33:10 nekral-guest Exp $ 1.10 +# 1.11 +# Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org) 1.12 +# 1.13 +# This program is free software; you can redistribute it and/or modify it 1.14 +# under the terms of GPL (see COPYING). 1.15 + 1.16 +=head1 NAME 1.17 + 1.18 +po4a-translate - convert a po file back to documentation format 1.19 + 1.20 +=head1 SYNOPSIS 1.21 + 1.22 +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> 1.23 + 1.24 +(XX.doc is the output, all others are inputs) 1.25 + 1.26 +=head1 DESCRIPTION 1.27 + 1.28 +The po4a (po for anything) project goal is to ease translations (and more 1.29 +interestingly, the maintenance of translations) using gettext tools on 1.30 +areas where they were not expected like documentation. 1.31 + 1.32 +The C<po4a-translate> script is in charge of converting the translation 1.33 +(which was done in a po file) under the documentation format back. The 1.34 +provided C<po> file should be the translation of the C<pot> file which were 1.35 +produced by po4a-gettextize(1). 1.36 + 1.37 +=head1 OPTIONS 1.38 + 1.39 +=over 4 1.40 + 1.41 +=item -f, --format 1.42 + 1.43 +Format of the documentation you want to handle. Use the --help-format 1.44 +option to see the list of available formats. 1.45 + 1.46 +=item -a, --addendum 1.47 + 1.48 +Add a file to the resulting file (to put translator's name or a section 1.49 +"About this translation", for example). The first line of the file to insert 1.50 +should be a PO4A header indicating where it should be added (see section 1.51 +I<HOWTO add extra text to translations> in po4a(7)). 1.52 + 1.53 +=item -A, --addendum-charset 1.54 + 1.55 +Charset of the addenda. Note that all the addenda should be in the same 1.56 +charset. 1.57 + 1.58 +=item -m, --master 1.59 + 1.60 +File containing the master document to translate. 1.61 + 1.62 +=item -M, --master-charset 1.63 + 1.64 +Charset of the file containing the document to translate. 1.65 + 1.66 +=item -l, --localized 1.67 + 1.68 +File where the localized (translated) document should be written. 1.69 + 1.70 +=item -L, --localized-charset 1.71 + 1.72 +Charset of the file containing the localized document. 1.73 + 1.74 +=item -p, --po 1.75 + 1.76 +File from which the message catalog should be read. 1.77 + 1.78 +=item -o, --option 1.79 + 1.80 +Extra option(s) to pass to the format plugin. Specify each option in the 1.81 +'name=value' format. See the documentation of each plugin for more 1.82 +information about the valid options and their meanings. 1.83 + 1.84 +=item -k, --keep 1.85 + 1.86 +Minimal threshold for translation percentage to keep (ie, write) the 1.87 +resulting file (default: 80). Ie, by default, files have to be translated 1.88 +at at least 80% to get written. 1.89 + 1.90 +=item -w, --width 1.91 + 1.92 +Column at which we should wrap the resulting file. 1.93 + 1.94 +=item -h, --help 1.95 + 1.96 +Show a short help message. 1.97 + 1.98 +=item --help-format 1.99 + 1.100 +List the documentation format understood by po4a. 1.101 + 1.102 +=item -V, --version 1.103 + 1.104 +Display the version of the script and exit. 1.105 + 1.106 +=item -v, --verbose 1.107 + 1.108 +Increase the verbosity of the program. 1.109 + 1.110 +=item -d, --debug 1.111 + 1.112 +Output some debugging information. 1.113 + 1.114 +=back 1.115 + 1.116 +=head1 Adding content (beside translations) to generated files 1.117 + 1.118 +To add some extra content to the generated document beside what you 1.119 +translated (like the name of the translator, or a "about this translation" 1.120 +section), you should use the C<--addendum> option. 1.121 + 1.122 +The first line of the addendum must be a header indicating where to put 1.123 +it in the document (it can be before or after a given part of the 1.124 +document). The rest of the file will be added verbatim to the resulting 1.125 +file without further processing. 1.126 + 1.127 +Note that if po4a-translate fails to add one of the given files, it discards 1.128 +the whole translation (because the missing file could be the one indicating 1.129 +the author, what would prevent the users to contact him to report bugs in 1.130 +the translation). 1.131 + 1.132 +The header has a pretty rigid syntax. For more information on how to use 1.133 +this feature and how it works, please refer to the po4a(7) man page. 1.134 + 1.135 +=head1 SEE ALSO 1.136 + 1.137 +L<po4a(7)>, L<po4a-gettextize(1)>, L<po4a-updatepo(1)>, L<po4a-normalize(1)>. 1.138 + 1.139 + 1.140 +=head1 AUTHORS 1.141 + 1.142 + Denis Barbier <barbier@linuxfr.org> 1.143 + Martin Quinson (mquinson#debian.org) 1.144 + 1.145 +=head1 COPYRIGHT AND LICENSE 1.146 + 1.147 +Copyright 2002, 2003, 2004 by SPI, inc. 1.148 + 1.149 +This program is free software; you may redistribute it and/or modify it 1.150 +under the terms of GPL (see the COPYING file). 1.151 + 1.152 +=cut 1.153 + 1.154 +use 5.006; 1.155 +use strict; 1.156 +use warnings; 1.157 + 1.158 +use Locale::Po4a::Chooser; 1.159 +use Locale::Po4a::TransTractor; 1.160 +use Locale::Po4a::Common; 1.161 + 1.162 +use Pod::Usage qw(pod2usage); 1.163 +use Getopt::Long qw(GetOptions); 1.164 + 1.165 +Locale::Po4a::Common::textdomain("po4a"); 1.166 + 1.167 +sub show_version { 1.168 + Locale::Po4a::Common::show_version("po4a-translate"); 1.169 + exit 0; 1.170 +} 1.171 + 1.172 + 1.173 +Getopt::Long::Configure('no_auto_abbrev','no_ignore_case'); 1.174 +my ($outfile,$width,$threshold)=('-',80,80); 1.175 +my ($help,$help_fmt,@verbose,$debug,@addfiles,$format,@options); 1.176 +my ($master_filename,$po_filename); 1.177 +my ($mastchar,$locchar,$addchar); 1.178 +GetOptions( 1.179 + 'help|h' => \$help, 1.180 + 'help-format' => \$help_fmt, 1.181 + 1.182 + 'master|m=s' => \$master_filename, 1.183 + 'localized|l=s' => \$outfile, 1.184 + 'po|p=s' => \$po_filename, 1.185 + 'addendum|a=s' => \@addfiles, 1.186 + 'format|f=s' => \$format, 1.187 + 1.188 + 'master-charset|M=s' => \$mastchar, 1.189 + 'localized-charset|L=s' => \$locchar, 1.190 + 'addendum-charset|A=s' => \$addchar, 1.191 + 1.192 + 'option|o=s' => \@options, 1.193 + 1.194 + 'width|w=s' => \$width, 1.195 + 'verbose|v' => \@verbose, 1.196 + 'debug|d' => \$debug, 1.197 + 'keep|k=s' => \$threshold, 1.198 + 1.199 + 'version|V' => \&show_version 1.200 +) or pod2usage(); 1.201 + 1.202 +$help && pod2usage(-verbose => 1, -exitval => 0); 1.203 +$help_fmt && Locale::Po4a::Chooser::list(0); 1.204 + 1.205 +(defined($master_filename) && length($master_filename))||pod2usage(); 1.206 +(defined($po_filename) && length($po_filename)) ||pod2usage(); 1.207 +-e $master_filename || die wrap_msg(gettext("File %s does not exist."), $master_filename); 1.208 +-e $po_filename || die wrap_msg(gettext("File %s does not exist."), $po_filename); 1.209 + 1.210 +my (@pos,@masters); 1.211 +push @pos,$po_filename; 1.212 +push @masters,$master_filename; 1.213 + 1.214 +my %options = ( 1.215 + "verbose" => scalar @verbose, 1.216 + "debug" => $debug); 1.217 + 1.218 +foreach (@options) { 1.219 + if (m/^([^=]*)=(.*)$/) { 1.220 + $options{$1}="$2"; 1.221 + } else { 1.222 + $options{$_}=1; 1.223 + } 1.224 +} 1.225 +# parser 1.226 +my $doc=Locale::Po4a::Chooser::new($format,%options); 1.227 + 1.228 + 1.229 +# Prepare the document to be used as translator, but not parser 1.230 +$doc->process('po_in_name' => \@pos, 1.231 + 'file_in_name' => \@masters, 1.232 + 'file_in_charset' => $mastchar, 1.233 + 'file_out_charset' => $locchar, 1.234 + 'addendum_charset' => $addchar); 1.235 + 1.236 +my ($percent,$hit,$queries) = $doc->stats(); 1.237 +my $error=0; 1.238 + 1.239 +print STDERR wrap_msg(gettext("%s is %s%% translated (%s of %s strings)."), 1.240 + $master_filename, $percent, $hit, $queries) 1.241 + if (scalar @verbose) && ($percent>=$threshold); 1.242 + 1.243 + 1.244 +if ($percent<$threshold) { 1.245 + print STDERR wrap_msg(gettext("Discard the translation of %s (only %s%% translated; need %s%%)."), 1.246 + $master_filename, $percent, $threshold); 1.247 + unlink($outfile) if (-e $outfile); 1.248 +} else { 1.249 + foreach my $add (@addfiles) { 1.250 + unless ($doc->addendum($add)) { 1.251 + unlink($outfile) if (-e $outfile); 1.252 + die wrap_msg(gettext("Discard the translation of %s (addendum %s does not apply)."), 1.253 + $master_filename, $add); 1.254 + } 1.255 + } 1.256 + $doc->write($outfile); 1.257 +} 1.258 + 1.259 +1; 1.260 +