Bug 8150 [SIGNED-OFF] Remove obsoleted navigation bar in circ history
[koha.git] / misc / translator / tmpl_process3.pl
index d876450..89c993f 100755 (executable)
@@ -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 = "<!DOCTYPE stylesheet ["  if $t =~ /DOCTYPE stylesheet/ ;
         print $output $t;
     }
     }
@@ -217,8 +235,8 @@ 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_files || !defined $str_file;
 
-# Type match defaults to *.tmpl plus *.inc if not specified
-$type = "tmpl|inc|xsl" if !defined($type);
+# Type match defaults to *.tt plus *.inc if not specified
+$type = "tt|inc|xsl" if !defined($type);
 
 # Check the inputs for being files or directories
 for my $input (@in_files) {
@@ -254,7 +272,6 @@ 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_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 {