X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;ds=sidebyside;f=misc%2Ftranslator%2Ftmpl_process3.pl;h=89c993f672ad230de0aebbdb692e82b62fd772fb;hb=91fa5f9bbd89a4cb63a69bf0c87b3272fcb6ebf5;hp=d876450717d0ff47f485c87a85f2a1c5f74fb06e;hpb=01b7b518f88fb8ea5cf78497388a73046d919baf;p=koha.git diff --git a/misc/translator/tmpl_process3.pl b/misc/translator/tmpl_process3.pl index d876450717..89c993f672 100755 --- a/misc/translator/tmpl_process3.pl +++ b/misc/translator/tmpl_process3.pl @@ -12,6 +12,7 @@ using gettext-compatible translation files =cut use strict; +#use warnings; FIXME - Bug 2505 use Getopt::Long; use Locale::PO; use File::Temp qw( :POSIX ); @@ -34,27 +35,36 @@ sub find_translation ($) { my($s) = @_; my $key = $s; if ($s =~ /\S/s) { - $key = TmplTokenizer::string_canon($key); - $key = TmplTokenizer::charset_convert($key, $charset_in, $charset_out); - $key = TmplTokenizer::quote_po($key); + $key = TmplTokenizer::string_canon($key); + $key = TmplTokenizer::charset_convert($key, $charset_in, $charset_out); + $key = TmplTokenizer::quote_po($key); + } + if (defined $href->{$key} && !$href->{$key}->fuzzy && length Locale::PO->dequote($href->{$key}->msgstr)){ + if ($s =~ /^(\s+)/){ + return $1 . Locale::PO->dequote($href->{$key}->msgstr); + } + else { + return Locale::PO->dequote($href->{$key}->msgstr); + } + } + else { + return $s; } - return defined $href->{$key} - && !$href->{$key}->fuzzy - && length Locale::PO->dequote($href->{$key}->msgstr)? - Locale::PO->dequote($href->{$key}->msgstr): $s; } sub text_replace_tag ($$) { my($t, $attr) = @_; my $it; + # value [tag=input], meta my $tag = lc($1) if $t =~ /^<(\S+)/s; my $translated_p = 0; - for my $a ('alt', 'content', 'title', 'value') { + for my $a ('alt', 'content', 'title', 'value','label') { 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|submit)$/)); # FIXME + next if $a eq 'value' && ($tag ne 'input' || (ref $attr->{'type'} && $attr->{'type'}->[1] =~ /^(?:checkbox|hidden|radio|text)$/)); # FIXME + my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME if ($val =~ /\S/s) { my $s = find_translation($val); @@ -67,15 +77,21 @@ sub text_replace_tag ($$) { } } if ($translated_p) { - $it = "<$tag" - . join('', map { - sprintf(' %s=%s', $_, $attr->{$_}->[2]) #FIXME - } sort { - $attr->{$a}->[3] <=> $attr->{$b}->[3] #FIXME - } keys %$attr) - . '>'; - } else { - $it = $t; + $it = "<$tag" + . join('', map { if ($_ ne '/'){ + sprintf(' %s="%s"', $_, $attr->{$_}->[1]); + } + else { + sprintf(' %s',$_); + } + + } sort { + $attr->{$a}->[3] <=> $attr->{$b}->[3] #FIXME + } keys %$attr); + $it .= '>'; + } + else { + $it = $t; } return $it; } @@ -86,16 +102,16 @@ sub text_replace (**) { my $s = TmplTokenizer::next_token $h; last unless defined $s; my($kind, $t, $attr) = ($s->type, $s->string, $s->attributes); - if ($kind eq TmplTokenType::TEXT) { + if ($kind eq C4::TmplTokenType::TEXT) { print $output find_translation($t); - } elsif ($kind eq TmplTokenType::TEXT_PARAMETRIZED) { + } elsif ($kind eq C4::TmplTokenType::TEXT_PARAMETRIZED) { my $fmt = find_translation($s->form); print $output TmplTokenizer::parametrize($fmt, 1, $s, sub { $_ = $_[0]; my($kind, $t, $attr) = ($_->type, $_->string, $_->attributes); - $kind == TmplTokenType::TAG && %$attr? + $kind == C4::TmplTokenType::TAG && %$attr? text_replace_tag($t, $attr): $t }); - } elsif ($kind eq TmplTokenType::TAG && %$attr) { + } elsif ($kind eq C4::TmplTokenType::TAG && %$attr) { print $output text_replace_tag($t, $attr); } elsif ($s->has_js_data) { for my $t (@{$s->js_data}) { @@ -108,6 +124,8 @@ sub text_replace (**) { } } } elsif (defined $t) { + # Quick fix to bug 4472 + $t = "{'""'}; $charset_out = TmplTokenizer::charset_canon $2 if $href->{'""'}->msgstr =~ /\bcharset=(["']?)([^;\s"'\\]+)\1/; $charset_in = $charset_out; - warn "Charset in/out: ".$charset_out; # for my $msgid (keys %$href) { # if ($msgid =~ /\bcharset=(["']?)([^;\s"'\\]+)\1/) { # my $candidate = TmplTokenizer::charset_canon $2; @@ -263,6 +280,21 @@ if (defined $href) { # $charset_in = $candidate; # } # } + + # BUG6464: check consistency of PO messages + # - count number of '%s' in msgid and msgstr + for my $msg ( values %$href ) { + my $id_count = split(/%s/, $msg->{msgid}) - 1; + my $str_count = split(/%s/, $msg->{msgstr}) - 1; + next if $id_count == $str_count || + $msg->{msgstr} eq '""' || + grep { /fuzzy/ } @{$msg->{_flags}}; + 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; + } } # set our charset in to UTF-8 @@ -317,7 +349,7 @@ if ($action eq 'create') { close INPUT; close OUTPUT; } - $st = system('msgmerge', '-U', '-s', $str_file, $tmpfile2); + $st = system("msgmerge -U ".($quiet?'-q':'')." -s $str_file $tmpfile2"); } else { error_normal "Text extraction failed: $xgettext: $!\n", undef; error_additional "Will not run msgmerge\n", undef; @@ -343,7 +375,7 @@ if ($action eq 'create') { # Merge the temporary "pot file" with the specified po file ($str_file) # FIXME: msgmerge(1) is a Unix dependency # FIXME: need to check the return value - $st = system('msgmerge', '-U', '-s', $str_file, $tmpfile2); + $st = system("msgmerge -U ".($quiet?'-q':'')." -s $str_file $tmpfile2"); } else { error_normal "Text extraction failed: $xgettext: $!\n", undef; error_additional "Will not run msgmerge\n", undef; @@ -383,7 +415,7 @@ if ($action eq 'create') { my $targetdir = $` if $target =~ /[^\/]+$/s; mkdir_recursive($targetdir) unless -d $targetdir; print STDERR "Creating $target...\n" unless $quiet; - open( OUTPUT, ">$target" ) || die "$target: $!\n"; + open( OUTPUT, ">$target" ) || die "$target: $!\n"; text_replace( $h, *OUTPUT ); close OUTPUT; } else {