Bug 8150 [SIGNED-OFF] Remove obsoleted navigation bar in circ history
[koha.git] / misc / translator / tmpl_process3.pl
index 46ceceb..89c993f 100755 (executable)
@@ -35,14 +35,21 @@ 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 ($$) {
@@ -95,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}) {
@@ -265,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;
@@ -274,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
@@ -328,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;
@@ -354,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;