Merge remote branch 'kc/master' into new/enh/bug_5917
[koha_gimpoz] / misc / translator / xgettext.pl
index b793867..ef1fb3d 100755 (executable)
@@ -7,6 +7,7 @@ xgettext.pl - xgettext(1)-like interface for .tmpl strings extraction
 =cut
 
 use strict;
+use warnings;
 use Getopt::Long;
 use POSIX;
 use Locale::PO;
@@ -59,9 +60,9 @@ sub remember ($$) {
     my($token, $string) = @_;
     # If we determine that the string is negligible, don't bother to remember
     unless (string_negligible_p( $string ) || token_negligible_p( $token )) {
-       my $key = TmplTokenizer::string_canon( $string );
-       $text{$key} = [] unless defined $text{$key};
-       push @{$text{$key}}, $token;
+        my $key = TmplTokenizer::string_canon( $string );
+        $text{$key} = [] unless defined $text{$key};
+        push @{$text{$key}}, $token;
     }
 }
 
@@ -82,36 +83,37 @@ sub string_list () {
     return @t;
 }
 
-###############################################################################
+  ###############################################################################
 
 sub text_extract (*) {
     my($h) = @_;
     for (;;) {
-       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) {
-           remember( $s, $t ) if $t =~ /\S/s;
-       } elsif ($kind eq TmplTokenType::TEXT_PARAMETRIZED) {
-           remember( $s, $s->form ) if $s->form =~ /\S/s;
-       } elsif ($kind eq TmplTokenType::TAG && %$attr) {
-           # value [tag=input], meta
-           my $tag = lc($1) if $t =~ /^<(\S+)/s;
-           for my $a ('alt', 'content', 'title', 'value') {
-               if ($attr->{$a}) {
-                   next if $a eq 'content' && $tag ne 'meta';
-                   next if $a eq 'value' && ($tag ne 'input'
-                       || (ref $attr->{'type'} && $attr->{'type'}->[1] =~ /^(?:hidden|radio)$/)); # FIXME
-                   my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
-                   $val = TmplTokenizer::trim $val;
-                   remember( $s, $val ) if $val =~ /\S/s;
-               }
-           }
-       } elsif ($s->has_js_data) {
-           for my $t (@{$s->js_data}) {
-               remember( $s, $t->[3] ) if $t->[0]; # FIXME
-           }
-       }
+        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) {
+            remember( $s, $t ) if $t =~ /\S/s;
+        } elsif ($kind eq TmplTokenType::TEXT_PARAMETRIZED) {
+            remember( $s, $s->form ) if $s->form =~ /\S/s;
+        } elsif ($kind eq TmplTokenType::TAG && %$attr) {
+            # value [tag=input], meta
+            my $tag = lc($1) if $t =~ /^<(\S+)/s;
+            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] =~ /^(?:hidden|radio|checkbox)$/)); # FIXME
+                    my($key, $val, $val_orig, $order) = @{$attr->{$a}}; #FIXME
+                    $val = TmplTokenizer::trim $val;
+                    remember( $s, $val ) if $val =~ /\S/s;
+                }
+            }
+        } elsif ($s->has_js_data) {
+            for my $t (@{$s->js_data}) {
+              remember( $s, $t->[3] ) if $t->[0]; # FIXME
+            }
+        }
     }
 }
 
@@ -210,6 +212,7 @@ EOF
        for my $token (@{$text{$t}}) {
            my $pathname = $token->pathname;
            $pathname =~ s/^$directory_re//os;
+        $pathname =~ s/^.*\/koha-tmpl\/(.*)$/$1/;
            printf OUTPUT "#: %s:%d\n", $pathname, $token->line_number
                    if defined $pathname && defined $token->line_number;
            $cformat_p = 1 if $token->type == TmplTokenType::TEXT_PARAMETRIZED;
@@ -258,7 +261,7 @@ sub convert_translation_file () {
     }
     # The following assumption is correct; that's what HTML::Template assumes
     if (!defined $charset_in) {
-       $charset_in = $charset_out = TmplTokenizer::charset_canon 'iso8859-1';
+       $charset_in = $charset_out = TmplTokenizer::charset_canon 'utf-8';
        warn "Warning: Can't determine original templates' charset, defaulting to $charset_in\n";
     }
 }