X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=misc%2Ftranslator%2Ftmpl_process3.pl;h=0f7b3a2a76240f7591a736a4d9cc1e1c6cc90a64;hb=b168f4a2e9a03ed56ce857353d4dde940980caba;hp=1690306274260dc76b9023a98ddd32d92c6160a5;hpb=16636c34d25054fd8d8f9dfbfcdfc6fe6fe7020b;p=srvgit diff --git a/misc/translator/tmpl_process3.pl b/misc/translator/tmpl_process3.pl index 1690306274..0f7b3a2a76 100755 --- a/misc/translator/tmpl_process3.pl +++ b/misc/translator/tmpl_process3.pl @@ -4,6 +4,9 @@ # Parts copyright 2003-2004 Jerome Vizcaino # Parts copyright 2004 Ambrose Li +use FindBin; +use lib $FindBin::Bin; + =head1 NAME tmpl_process3.pl - Alternative version of tmpl_process.pl @@ -22,7 +25,7 @@ use VerboseWarnings qw( :warn :die ); ############################################################################### -use vars qw( $in_dir @filenames $str_file $out_dir $quiet ); +use vars qw( @in_dirs @filenames @match @nomatch $str_file $out_dir $quiet ); use vars qw( @excludes $exclude_regex ); use vars qw( $recursive_p ); use vars qw( $pedantic_p ); @@ -32,7 +35,7 @@ use vars qw( $charset_in $charset_out ); ############################################################################### -sub find_translation ($) { +sub find_translation { my($s) = @_; my $key = $s; if ($s =~ /\S/s) { @@ -53,27 +56,43 @@ sub find_translation ($) { } } -sub text_replace_tag ($$) { +sub text_replace_tag { my($t, $attr) = @_; my $it; + my @ttvar; # value [tag=input], meta - my $tag = lc($1) if $t =~ /^<(\S+)/s; + my $tag = ($t =~ /^<(\S+)/s) ? lc($1) : undef; my $translated_p = 0; for my $a ('alt', 'content', 'title', 'value', 'label', 'placeholder') { if ($attr->{$a}) { next if $a eq 'label' && $tag ne 'optgroup'; next if $a eq 'content' && $tag ne 'meta'; - next if $a eq 'value' && ($tag ne 'input' || (ref $attr->{'type'} && $attr->{'type'}->[1] =~ /^(?:checkbox|hidden|radio|text)$/)); # FIXME + next if $a eq 'value' && ($tag ne 'input' || (ref $attr->{'type'} && $attr->{'type'}->[1] =~ /^(?:checkbox|hidden|radio)$/)); # FIXME my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME if ($val =~ /\S/s) { - my $s = find_translation($val); - if ($attr->{$a}->[1] ne $s) { #FIXME - $attr->{$a}->[1] = $s; # FIXME - $attr->{$a}->[2] = ($s =~ /"/s)? "'$s'": "\"$s\""; #FIXME - $translated_p = 1; - } + # for selected attributes replace '[%..%]' with '%s' and remember matches + if ( $a =~ /title|value|alt|content|placeholder/ ) { + while ( $val =~ s/(\[\%.*?\%\])/\%s/ ) { + my $var = $1; + push @ttvar, $1; + } + } + # find translation for transformed attributes + my $s = find_translation($val); + # replace '%s' with original content (in order) on translated string, this is fragile! + if ( $a =~ /title|value|alt|content|placeholder/ and @ttvar ) { + while ( @ttvar ) { + my $var = shift @ttvar; + $s =~ s/\%s/$var/; + } + } + if ($attr->{$a}->[1] ne $s) { #FIXME + $attr->{$a}->[1] = $s; # FIXME + $attr->{$a}->[2] = ($s =~ /"/s)? "'$s'": "\"$s\""; #FIXME + $translated_p = 1; + } } } } @@ -88,6 +107,7 @@ sub text_replace_tag ($$) { } sort { $attr->{$a}->[3] <=> $attr->{$b}->[3] #FIXME + || $a cmp $b # Sort attributes BZ 22236 } keys %$attr); $it .= '>'; } @@ -97,10 +117,10 @@ sub text_replace_tag ($$) { return $it; } -sub text_replace (**) { +sub text_replace { my($h, $output) = @_; for (;;) { - my $s = TmplTokenizer::next_token $h; + my $s = TmplTokenizer::next_token($h); last unless defined $s; my($kind, $t, $attr) = ($s->type, $s->string, $s->attributes); if ($kind eq C4::TmplTokenType::TEXT) { @@ -118,7 +138,7 @@ sub text_replace (**) { for my $t (@{$s->js_data}) { # FIXME for this whole block if ($t->[0]) { - printf $output "%s%s%s", $t->[2], find_translation $t->[3], + printf $output "%s%s%s", $t->[2], find_translation($t->[3]), $t->[2]; } else { print $output $t->[1]; @@ -133,7 +153,10 @@ sub text_replace (**) { } sub listfiles { - my($dir, $type, $action, $filenames) = @_; + my($dir, $type, $action) = @_; + my $filenames = join ('|', @filenames); # used to update strings from this file + my $match = join ('|', @match); # use only this files + my $nomatch = join ('|', @nomatch); # do no use this files my @it = (); if (opendir(DIR, $dir)) { my @dirent = readdir DIR; # because DIR is shared when recursing @@ -144,38 +167,40 @@ sub listfiles { || (defined $exclude_regex && $dirent =~ /^(?:$exclude_regex)$/)) { ; } elsif (-f $path) { - my $basename = basename $path; + my $basename = fileparse( $path ); push @it, $path - if ( not @$filenames or ( grep { $path =~ /$_/ } @$filenames ) ) - and (!defined $type || $dirent =~ /\.(?:$type)$/) || $action eq 'install'; + if ( not @filenames or $basename =~ /($filenames)/i ) + and ( not @match or $basename =~ /($match)/i ) # files to include + and ( not @nomatch or $basename !~ /($nomatch)/i ) # files not to include + and (!defined $type || $dirent =~ /\.(?:$type)$/) || $action eq 'install'; } elsif (-d $path && $recursive_p) { - push @it, listfiles($path, $type, $action, $filenames); + push @it, listfiles($path, $type, $action); } } } else { - warn_normal "$dir: $!", undef; + warn_normal("$dir: $!", undef); } return @it; } ############################################################################### -sub mkdir_recursive ($) { +sub mkdir_recursive { my($dir) = @_; local($`, $&, $', $1); $dir = $` if $dir ne /^\/+$/ && $dir =~ /\/+$/; my ($prefix, $basename) = ($dir =~ /\/([^\/]+)$/s)? ($`, $1): ('.', $dir); mkdir_recursive($prefix) if $prefix ne '.' && !-d $prefix; if (!-d $dir) { - print STDERR "Making directory $dir..." unless $quiet; + print STDERR "Making directory $dir...\n" unless $quiet; # creates with rwxrwxr-x permissions - mkdir($dir, 0775) || warn_normal "$dir: $!", undef; + mkdir($dir, 0775) || warn_normal("$dir: $!", undef); } } ############################################################################### -sub usage ($) { +sub usage { my($exitcode) = @_; my $h = $exitcode? *STDERR: *STDOUT; print $h < \$in_dir, + 'input|i=s' => \@in_dirs, 'filename|f=s' => \@filenames, + 'match|m=s' => \@match, + 'nomatch|n=s' => \@nomatch, 'outputdir|o=s' => \$out_dir, 'recursive|r' => \$recursive_p, 'str-file|s=s' => \$str_file, @@ -226,10 +260,10 @@ GetOptions( 'quiet|q' => \$quiet, 'pedantic-warnings|pedantic' => sub { $pedantic_p = 1 }, 'help' => \&usage, -) || usage_error; +) || usage_error(); -VerboseWarnings::set_application_name $0; -VerboseWarnings::set_pedantic_mode $pedantic_p; +VerboseWarnings::set_application_name($0); +VerboseWarnings::set_pedantic_mode($pedantic_p); # keep the buggy Locale::PO quiet if it says stupid things $SIG{__WARN__} = sub { @@ -239,15 +273,17 @@ $SIG{__WARN__} = sub { my $action = shift or usage_error('You must specify an ACTION.'); usage_error('You must at least specify input and string list filenames.') - if !$in_dir || !defined $str_file; + if !@in_dirs || !defined $str_file; # Type match defaults to *.tt plus *.inc if not specified $type = "tt|inc|xsl|xml|def" if !defined($type); # Check the inputs for being directories -usage_error("$in_dir: Input must be a directory.\n" - . "(Symbolic links are not supported at the moment)") - unless -d $in_dir; +for my $in_dir ( @in_dirs ) { + usage_error("$in_dir: Input must be a directory.\n" + . "(Symbolic links are not supported at the moment)") + unless -d $in_dir; +} # Generates the global exclude regular expression $exclude_regex = '(?:'.join('|', @excludes).')' if @excludes; @@ -255,21 +291,23 @@ $exclude_regex = '(?:'.join('|', @excludes).')' if @excludes; my @in_files; # Generate the list of input files if a directory is specified # input is a directory, generates list of files to process -$in_dir =~ s/\/$//; # strips the trailing / if any for my $fn ( @filenames ) { die "You cannot specify input files and directories at the same time.\n" if -d $fn; } -@in_files = listfiles($in_dir, $type, $action, \@filenames); +for my $in_dir ( @in_dirs ) { + $in_dir =~ s/\/$//; # strips the trailing / if any + @in_files = ( @in_files, listfiles($in_dir, $type, $action)); +} # restores the string list from file -$href = Locale::PO->load_file_ashash($str_file); +$href = Locale::PO->load_file_ashash($str_file, 'utf-8'); # guess the charsets. HTML::Templates defaults to iso-8859-1 if (defined $href) { die "$str_file: PO file is corrupted, or not a PO file\n" unless defined $href->{'""'}; - $charset_out = TmplTokenizer::charset_canon $2 if $href->{'""'}->msgstr =~ /\bcharset=(["']?)([^;\s"'\\]+)\1/; + $charset_out = TmplTokenizer::charset_canon($2) if $href->{'""'}->msgstr =~ /\bcharset=(["']?)([^;\s"'\\]+)\1/; $charset_in = $charset_out; # for my $msgid (keys %$href) { # if ($msgid =~ /\bcharset=(["']?)([^;\s"'\\]+)\1/) { @@ -288,23 +326,23 @@ if (defined $href) { next if $id_count == $str_count || $msg->{msgstr} eq '""' || grep { /fuzzy/ } @{$msg->{_flags}}; - warn_normal + warn_normal( "unconsistent %s count: ($id_count/$str_count):\n" . " line: " . $msg->{loaded_line_number} . "\n" . " msgid: " . $msg->{msgid} . "\n" . - " msgstr: " . $msg->{msgstr} . "\n", undef; + " msgstr: " . $msg->{msgstr} . "\n", undef); } } # set our charset in to UTF-8 if (!defined $charset_in) { - $charset_in = TmplTokenizer::charset_canon 'UTF-8'; - warn "Warning: Can't determine original templates' charset, defaulting to $charset_in\n"; + $charset_in = TmplTokenizer::charset_canon('UTF-8'); + warn "Warning: Can't determine original templates' charset, defaulting to $charset_in\n" unless ( $quiet ); } # set our charset out to UTF-8 if (!defined $charset_out) { - $charset_out = TmplTokenizer::charset_canon 'UTF-8'; - warn "Warning: Charset Out defaulting to $charset_out\n"; + $charset_out = TmplTokenizer::charset_canon('UTF-8'); + warn "Warning: Charset Out defaulting to $charset_out\n" unless ( $quiet ); } my $xgettext = './xgettext.pl'; # actual text extractor script my $st; @@ -312,7 +350,7 @@ my $st; if ($action eq 'create') { # updates the list. As the list is empty, every entry will be added if (!-s $str_file) { - warn "Removing empty file $str_file\n"; + warn "Removing empty file $str_file\n" unless ( $quiet ); unlink $str_file || die "$str_file: $!\n"; } die "$str_file: Output file already exists\n" if -f $str_file; @@ -324,7 +362,7 @@ if ($action eq 'create') { print $tmph1 "$input\n"; } close $tmph1; - warn "I $charset_in O $charset_out"; + warn "I $charset_in O $charset_out" unless ( $quiet ); # Generate the specified po file ($str_file) $st = system ($xgettext, '-s', '-f', $tmpfile1, '-o', $tmpfile2, (defined $charset_in? ('-I', $charset_in): ()), @@ -338,23 +376,22 @@ if ($action eq 'create') { # FIXME: msgmerge(1) is a Unix dependency # FIXME: need to check the return value unless (-f $str_file) { - local(*INPUT, *OUTPUT); - open(INPUT, "<$tmpfile2"); - open(OUTPUT, ">$str_file"); - while () { - print OUTPUT; + open(my $infh, '<', $tmpfile2); + open(my $outfh, '>', $str_file); + while (<$infh>) { + print $outfh; last if /^\n/s; } - close INPUT; - close OUTPUT; + close $infh; + close $outfh; } $st = system("msgmerge ".($quiet?'-q':'')." -s $str_file $tmpfile2 -o - | msgattrib --no-obsolete -o $str_file"); } else { - error_normal "Text extraction failed: $xgettext: $!\n", undef; - error_additional "Will not run msgmerge\n", undef; + error_normal("Text extraction failed: $xgettext: $!\n", undef); + error_additional("Will not run msgmerge\n", undef); } - unlink $tmpfile1 || warn_normal "$tmpfile1: unlink failed: $!\n", undef; - unlink $tmpfile2 || warn_normal "$tmpfile2: unlink failed: $!\n", undef; + unlink $tmpfile1 || warn_normal("$tmpfile1: unlink failed: $!\n", undef); + unlink $tmpfile2 || warn_normal("$tmpfile2: unlink failed: $!\n", undef); } elsif ($action eq 'update') { my($tmph1, $tmpfile1) = tmpnam(); @@ -383,17 +420,23 @@ if ($action eq 'create') { $st = system("msgmerge ".($quiet?'-q':'')." -s $str_file $tmpfile2 -o - | msgattrib --no-obsolete -o $str_file"); } } else { - error_normal "Text extraction failed: $xgettext: $!\n", undef; - error_additional "Will not run msgmerge\n", undef; + error_normal("Text extraction failed: $xgettext: $!\n", undef); + error_additional("Will not run msgmerge\n", undef); } - unlink $tmpfile1 || warn_normal "$tmpfile1: unlink failed: $!\n", undef; - unlink $tmpfile2 || warn_normal "$tmpfile2: unlink failed: $!\n", undef; + unlink $tmpfile1 || warn_normal("$tmpfile1: unlink failed: $!\n", undef); + unlink $tmpfile2 || warn_normal("$tmpfile2: unlink failed: $!\n", undef); } elsif ($action eq 'install') { if(!defined($out_dir)) { usage_error("You must specify an output directory when using the install method."); } + if ( scalar @in_dirs > 1 ) { + usage_error("You must specify only one input directory when using the install method."); + } + + my $in_dir = shift @in_dirs; + if ($in_dir eq $out_dir) { warn "You must specify a different input and output directory.\n"; exit -1; @@ -404,8 +447,8 @@ if ($action eq 'create') { -d $out_dir || die "$out_dir: The directory does not exist\n"; # Try to open the file, because Locale::PO doesn't check :-/ - open(INPUT, "<$str_file") || die "$str_file: $!\n"; - close INPUT; + open(my $fh, '<', $str_file) || die "$str_file: $!\n"; + close $fh; # creates the new tmpl file using the new translation for my $input (@in_files) { @@ -413,17 +456,17 @@ if ($action eq 'create') { unless substr($input, 0, length($in_dir) + 1) eq "$in_dir/"; my $target = $out_dir . substr($input, length($in_dir)); - my $targetdir = $` if $target =~ /[^\/]+$/s; + my $targetdir = ($target =~ /[^\/]+$/s) ? $` : undef; if (!defined $type || $input =~ /\.(?:$type)$/) { my $h = TmplTokenizer->new( $input ); $h->set_allow_cformat( 1 ); - VerboseWarnings::set_input_file_name $input; + VerboseWarnings::set_input_file_name($input); mkdir_recursive($targetdir) unless -d $targetdir; print STDERR "Creating $target...\n" unless $quiet; - open( OUTPUT, ">$target" ) || die "$target: $!\n"; - text_replace( $h, *OUTPUT ); - close OUTPUT; + open( my $fh, ">:encoding(UTF-8)", "$target" ) || die "$target: $!\n"; + text_replace( $h, $fh ); + close $fh; } else { # just copying the file mkdir_recursive($targetdir) unless -d $targetdir;