More perldoc updates
[koha.git] / misc / translator / tmpl_process3.pl
1 #!/usr/bin/perl
2 # This file is part of Koha
3 # Parts copyright 2003-2004 Paul Poulain
4 # Parts copyright 2003-2004 Jerome Vizcaino
5 # Parts copyright 2004 Ambrose Li
6
7 =head1 NAME
8
9 tmpl_process3.pl - Alternative version of tmpl_process.pl
10 using gettext-compatible translation files
11
12 =cut
13
14 use strict;
15 use Getopt::Long;
16 use Locale::PO;
17 use File::Temp qw( :POSIX );
18 use TmplTokenizer;
19 use VerboseWarnings qw( :warn :die );
20
21 ###############################################################################
22
23 use vars qw( @in_files $in_dir $str_file $out_dir );
24 use vars qw( @excludes $exclude_regex );
25 use vars qw( $recursive_p );
26 use vars qw( $pedantic_p );
27 use vars qw( $href );
28 use vars qw( $type );   # file extension (DOS form without the dot) to match
29 use vars qw( $charset_in $charset_out );
30
31 ###############################################################################
32
33 sub find_translation ($) {
34     my($s) = @_;
35     my $key = $s;
36     if ($s =~ /\S/s) {
37         $key = TmplTokenizer::string_canon($key);
38         $key = TmplTokenizer::charset_convert($key, $charset_in, $charset_out);
39         $key = TmplTokenizer::quote_po($key);
40     }
41     return defined $href->{$key}
42                 && !$href->{$key}->fuzzy
43                 && length Locale::PO->dequote($href->{$key}->msgstr)?
44            Locale::PO->dequote($href->{$key}->msgstr): $s;
45 }
46
47 sub text_replace_tag ($$) {
48     my($t, $attr) = @_;
49     my $it;
50     # value [tag=input], meta
51     my $tag = lc($1) if $t =~ /^<(\S+)/s;
52     my $translated_p = 0;
53     for my $a ('alt', 'content', 'title', 'value') {
54         if ($attr->{$a}) {
55             next if $a eq 'content' && $tag ne 'meta';
56             next if $a eq 'value' && ($tag ne 'input'
57                 || (ref $attr->{'type'} && $attr->{'type'}->[1] =~ /^(?:hidden|radio)$/)); # FIXME
58             my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
59             if ($val =~ /\S/s) {
60                 my $s = find_translation($val);
61                 if ($attr->{$a}->[1] ne $s) { #FIXME
62                     $attr->{$a}->[1] = $s; # FIXME
63                     $attr->{$a}->[2] = ($s =~ /"/s)? "'$s'": "\"$s\""; #FIXME
64                     $translated_p = 1;
65                 }
66             }
67         }
68     }
69     if ($translated_p) {
70         $it = "<$tag"
71             . join('', map {
72                     sprintf(' %s=%s', $_, $attr->{$_}->[2]) #FIXME
73                 } sort {
74                     $attr->{$a}->[3] <=> $attr->{$b}->[3] #FIXME
75                 } keys %$attr)
76             . '>';
77     } else {
78         $it = $t;
79     }
80     return $it;
81 }
82
83 sub text_replace (**) {
84     my($h, $output) = @_;
85     for (;;) {
86         my $s = TmplTokenizer::next_token $h;
87     last unless defined $s;
88         my($kind, $t, $attr) = ($s->type, $s->string, $s->attributes);
89         if ($kind eq TmplTokenType::TEXT) {
90             print $output find_translation($t);
91         } elsif ($kind eq TmplTokenType::TEXT_PARAMETRIZED) {
92             my $fmt = find_translation($s->form);
93             print $output TmplTokenizer::parametrize($fmt, 1, $s, sub {
94                 $_ = $_[0];
95                 my($kind, $t, $attr) = ($_->type, $_->string, $_->attributes);
96                 $kind == TmplTokenType::TAG && %$attr?
97                     text_replace_tag($t, $attr): $t });
98         } elsif ($kind eq TmplTokenType::TAG && %$attr) {
99             print $output text_replace_tag($t, $attr);
100         } elsif (defined $t) {
101             print $output $t;
102         }
103     }
104 }
105
106 sub listfiles ($$) {
107     my($dir, $type) = @_;
108     my @it = ();
109     if (opendir(DIR, $dir)) {
110         my @dirent = readdir DIR;       # because DIR is shared when recursing
111         closedir DIR;
112         for my $dirent (@dirent) {
113             my $path = "$dir/$dirent";
114             if ($dirent =~ /^\./ || $dirent eq 'CVS' || $dirent eq 'RCS'
115             || (defined $exclude_regex && $dirent =~ /^(?:$exclude_regex)$/)) {
116                 ;
117             } elsif (-f $path) {
118                 push @it, $path if !defined $type || $dirent =~ /\.(?:$type)$/;
119             } elsif (-d $path && $recursive_p) {
120                 push @it, listfiles($path, $type);
121             }
122         }
123     } else {
124         warn_normal "$dir: $!", undef;
125     }
126     return @it;
127 }
128
129 ###############################################################################
130
131 sub usage ($) {
132     my($exitcode) = @_;
133     my $h = $exitcode? *STDERR: *STDOUT;
134     print $h <<EOF;
135 Usage: $0 create [OPTION]
136   or:  $0 update [OPTION]
137   or:  $0 install [OPTION]
138   or:  $0 --help
139 Create or update PO files from templates, or install translated templates.
140
141   -i, --input=SOURCE          Get or update strings from SOURCE file.
142                               SOURCE is a directory if -r is also specified.
143   -o, --outputdir=DIRECTORY   Install translation(s) to specified DIRECTORY
144       --pedantic-warnings     Issue warnings even for detected problems
145                               which are likely to be harmless
146   -r, --recursive             SOURCE in the -i option is a directory
147   -s, --str-file=FILE         Specify FILE as the translation (po) file
148                               for input (install) or output (create, update)
149   -x, --exclude=REGEXP        Exclude files matching the given REGEXP
150       --help                  Display this help and exit
151
152 The -o option is ignored for the "create" and "update" actions.
153 Try `perldoc $0' for perhaps more information.
154 EOF
155     exit($exitcode);
156 }
157
158 ###############################################################################
159
160 sub usage_error (;$) {
161     for my $msg (split(/\n/, $_[0])) {
162         print STDERR "$msg\n";
163     }
164     print STDERR "Try `$0 --help' for more information.\n";
165     exit(-1);
166 }
167
168 ###############################################################################
169
170 GetOptions(
171     'input|i=s'                         => \@in_files,
172     'outputdir|o=s'                     => \$out_dir,
173     'recursive|r'                       => \$recursive_p,
174     'str-file|s=s'                      => \$str_file,
175     'exclude|x=s'                       => \@excludes,
176     'pedantic-warnings|pedantic'        => sub { $pedantic_p = 1 },
177     'help'                              => \&usage,
178 ) || usage_error;
179
180 VerboseWarnings::set_application_name $0;
181 VerboseWarnings::set_pedantic_mode $pedantic_p;
182
183 # keep the buggy Locale::PO quiet if it says stupid things
184 $SIG{__WARN__} = sub {
185         my($s) = @_;
186         print STDERR $s unless $s =~ /^Strange line in [^:]+: #~/s
187     };
188
189 my $action = shift or usage_error('You must specify an ACTION.');
190 usage_error('You must at least specify input and string list filenames.')
191     if !@in_files || !defined $str_file;
192
193 # Type match defaults to *.tmpl plus *.inc if not specified
194 $type = "tmpl|inc" if !defined($type);
195
196 # Check the inputs for being files or directories
197 for my $input (@in_files) {
198     usage_error("$input: Input must be a file or directory.\n"
199             . "(Symbolic links are not supported at the moment)")
200         unless -d $input || -f $input;;
201 }
202
203 # Generates the global exclude regular expression
204 $exclude_regex =  '(?:'.join('|', @excludes).')' if @excludes;
205
206 # Generate the list of input files if a directory is specified
207 if (-d $in_files[0]) {
208     die "If you specify a directory as input, you must specify only it.\n"
209             if @in_files > 1;
210
211     # input is a directory, generates list of files to process
212     $in_dir = $in_files[0];
213     $in_dir =~ s/\/$//; # strips the trailing / if any
214     @in_files = listfiles($in_dir, $type);
215 } else {
216     for my $input (@in_files) {
217         die "You cannot specify input files and directories at the same time.\n"
218                 unless -f $input;
219     }
220 }
221
222 # restores the string list from file
223 $href = Locale::PO->load_file_ashash($str_file);
224
225 # guess the charsets. HTML::Templates defaults to iso-8859-1
226 if (defined $href) {
227     die "$str_file: PO file is corrupted, or not a PO file\n"
228             unless defined $href->{'""'};
229     $charset_out = TmplTokenizer::charset_canon $2
230             if $href->{'""'}->msgstr =~ /\bcharset=(["']?)([^;\s"'\\]+)\1/;
231     for my $msgid (keys %$href) {
232         if ($msgid =~ /\bcharset=(["']?)([^;\s"'\\]+)\1/) {
233             my $candidate = TmplTokenizer::charset_canon $2;
234             die "Conflicting charsets in msgid: $charset_in vs $candidate\n"
235                     if defined $charset_in && $charset_in ne $candidate;
236             $charset_in = $candidate;
237         }
238     }
239 }
240 if (!defined $charset_in) {
241     $charset_in = TmplTokenizer::charset_canon 'iso8859-1';
242     warn "Warning: Can't determine original templates' charset, defaulting to $charset_in\n";
243 }
244
245 my $xgettext = './xgettext.pl'; # actual text extractor script
246 my $st;
247
248 if ($action eq 'create')  {
249     # updates the list. As the list is empty, every entry will be added
250     if (!-s $str_file) {
251         warn "Removing empty file $str_file\n";
252         unlink $str_file || die "$str_file: $!\n";
253     }
254     die "$str_file: Output file already exists\n" if -f $str_file;
255     my($tmph, $tmpfile) = tmpnam();
256     # Generate the temporary file that acts as <MODULE>/POTFILES.in
257     for my $input (@in_files) {
258         print $tmph "$input\n";
259     }
260     close $tmph;
261     # Generate the specified po file ($str_file)
262     $st = system ($xgettext, '-s', '-f', $tmpfile, '-o', $str_file);
263     warn_normal "Text extraction failed: $xgettext: $!\n", undef if $st != 0;
264 #   unlink $tmpfile || warn_normal "$tmpfile: unlink failed: $!\n", undef;
265
266 } elsif ($action eq 'update') {
267     my($tmph1, $tmpfile1) = tmpnam();
268     my($tmph2, $tmpfile2) = tmpnam();
269     close $tmph2; # We just want a name
270     # Generate the temporary file that acts as <MODULE>/POTFILES.in
271     for my $input (@in_files) {
272         print $tmph1 "$input\n";
273     }
274     close $tmph1;
275     # Generate the temporary file that acts as <MODULE>/<LANG>.pot
276     $st = system($xgettext, '-s', '-f', $tmpfile1, '-o', $tmpfile2,
277             '--po-mode',
278             (defined $charset_in? ('-I', $charset_in): ()),
279             (defined $charset_out? ('-O', $charset_out): ()));
280     if ($st == 0) {
281         # Merge the temporary "pot file" with the specified po file ($str_file)
282         # FIXME: msgmerge(1) is a Unix dependency
283         # FIXME: need to check the return value
284         $st = system('msgmerge', '-U', '-s', $str_file, $tmpfile2);
285     } else {
286         error_normal "Text extraction failed: $xgettext: $!\n", undef;
287         error_additional "Will not run msgmerge\n", undef;
288     }
289 #   unlink $tmpfile1 || warn_normal "$tmpfile1: unlink failed: $!\n", undef;
290 #   unlink $tmpfile2 || warn_normal "$tmpfile2: unlink failed: $!\n", undef;
291
292 } elsif ($action eq 'install') {
293     if(!defined($out_dir)) {
294         usage_error("You must specify an output directory when using the install method.");
295     }
296         
297     if ($in_dir eq $out_dir) {
298         warn "You must specify a different input and output directory.\n";
299         exit -1;
300     }
301
302     # Make sure the output directory exists
303     # (It will auto-create it, but for compatibility we should not)
304     -d $out_dir || die "$out_dir: The directory does not exist\n";
305
306     # Try to open the file, because Locale::PO doesn't check :-/
307     open(INPUT, "<$str_file") || die "$str_file: $!\n";
308     close INPUT;
309
310     # creates the new tmpl file using the new translation
311     for my $input (@in_files) {
312         die "Assertion failed"
313                 unless substr($input, 0, length($in_dir) + 1) eq "$in_dir/";
314
315         my $h = TmplTokenizer->new( $input );
316         $h->set_allow_cformat( 1 );
317         VerboseWarnings::set_input_file_name $input;
318
319         my $target = $out_dir . substr($input, length($in_dir));
320         my $targetdir = $` if $target =~ /[^\/]+$/s;
321         if (!-d $targetdir) {
322             print STDERR "Making directory $targetdir...";
323             # creates with rwxrwxr-x permissions
324             mkdir($targetdir, 0775) || warn_normal "$targetdir: $!", undef;
325         }
326         print STDERR "Creating $target...\n";
327         open( OUTPUT, ">$target" ) || die "$target: $!\n";
328         text_replace( $h, *OUTPUT );
329         close OUTPUT;
330     }
331
332 } else {
333     usage_error('Unknown action specified.');
334 }
335
336 if ($st == 0) {
337     printf "The %s seems to be successful.\n", $action;
338 } else {
339     printf "%s FAILED.\n", "\u$action";
340 }
341 exit 0;
342
343 ###############################################################################
344
345 =head1 SYNOPSIS
346
347 ./tmpl_process3.pl [ I<tmpl_process.pl options> ]
348
349 =head1 DESCRIPTION
350
351 This is an alternative version of the tmpl_process.pl script,
352 using standard gettext-style PO files.  While there still might
353 be changes made to the way it extracts strings, at this moment
354 it should be stable enough for general use; it is already being
355 used for the Chinese and Polish translations.
356
357 Currently, the create, update, and install actions have all been
358 reimplemented and seem to work.
359
360 =head2 Features
361
362 =over
363
364 =item -
365
366 Translation files in standard Uniforum PO format.
367 All standard tools including all gettext tools,
368 plus PO file editors like kbabel(1) etc.
369 can be used.
370
371 =item -
372
373 Minor changes in whitespace in source templates
374 do not generally require strings to be re-translated.
375
376 =item -
377
378 Able to handle <TMPL_VAR> variables in the templates;
379 <TMPL_VAR> variables are usually extracted in proper context,
380 represented by a short %s placeholder.
381
382 =item -
383
384 Able to handle text input and radio button INPUT elements
385 in the templates; these INPUT elements are also usually
386 extracted in proper context,
387 represented by a short %S or %p placeholder.
388
389 =item -
390
391 Automatic comments in the generated PO files to provide
392 even more context (line numbers, and the names and types
393 of the variables).
394
395 =item -
396
397 The %I<n>$s (or %I<n>$p, etc.) notation can be used
398 for change the ordering of the variables,
399 if such a reordering is required for correct translation.
400
401 =item -
402
403 If a particular <TMPL_VAR> should not appear in the
404 translation, it can be suppressed with the %0.0s notation.
405
406 =item -
407
408 Using the PO format also means translators can add their
409 own comments in the translation files, if necessary.
410
411 =item -
412
413 Create, update, and install actions are all based on the
414 same scanner module. This ensures that update and install
415 have the same idea of what is a translatable string;
416 attribute names in tags, for example, will not be
417 accidentally translated.
418
419 =back
420
421 =head1 NOTES
422
423 Anchors are represented by an <AI<n>> notation.
424 The meaning of this non-standard notation might not be obvious.
425
426 The create action calls xgettext.pl to do the actual work;
427 the update action calls xgettext.pl and msgmerge(1) to do the
428 actual work.
429
430 =head1 BUGS
431
432 xgettext.pl must be present in the current directory; the
433 msgmerge(1) command must also be present in the search path.
434 The script currently does not check carefully whether these
435 dependent commands are present.
436
437 Locale::PO(3) has a lot of bugs. It can neither parse nor
438 generate GNU PO files properly; a couple of workarounds have
439 been written in TmplTokenizer and more is likely to be needed
440 (e.g., to get rid of the "Strange line" warning for #~).
441
442 This script may not work in Windows.
443
444 There are probably some other bugs too, since this has not been
445 tested very much.
446
447 =head1 SEE ALSO
448
449 xgettext.pl,
450 TmplTokenizer.pm,
451 msgmerge(1),
452 Locale::PO(3),
453 translator_doc.txt
454
455 http://www.saas.nsw.edu.au/koha_wiki/index.php?page=DifficultTerms
456
457 =cut