Merge remote branch 'kc/master'
[koha_fer] / misc / translator / xgettext.pl
index 4e0c05f..031f9ba 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;
@@ -97,16 +98,21 @@ sub text_extract (*) {
        } 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') {
+           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)$/)); # FIXME
+                       || (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
+           }
        }
     }
 }
@@ -198,6 +204,9 @@ EOF
                    . (defined $type? " type=$type->[1]": '')
                    . (defined $name? " name=$name->[1]": '');
            }
+       } elsif ($text{$t}->[0]->has_js_data) {
+           printf OUTPUT "#. For the first occurrence,\n" if @{$text{$t}} > 1;
+           printf OUTPUT "#. SCRIPT\n";
        }
        my $cformat_p;
        for my $token (@{$text{$t}}) {
@@ -251,7 +260,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";
     }
 }
@@ -376,7 +385,6 @@ A gettext-like format provides the following advantages:
 
 =item -
 
-(Future goal)
 Translation to non-English-like languages with different word
 order:  gettext's c-format strings can theoretically be
 emulated if we are able to do some analysis on the .tmpl input
@@ -400,8 +408,9 @@ the gettext format.
 
 =back
 
-Note that this script is experimental and should still be
-considered unstable.
+This script has already been in use for over a year and should
+be reasonable stable. Nevertheless, it is still somewhat
+experimental and there are still some issues.
 
 Please refer to the explanation in tmpl_process3 for further
 details.
@@ -417,9 +426,23 @@ files (passed to -f) can be generated thus:
 This is, however, quite pointless, because the "create" and
 "update" actions have already been implemented in tmpl_process3.pl.
 
+=head2 Strings inside JavaScript
+
+In the SCRIPT elements, the script will attempt to scan for
+_("I<string literal>") patterns, and extract the I<string literal>
+as a translatable string.
+
+Note that the C-like _(...) notation is required.
+
+The JavaScript must actually define a _ function
+so that the code remains correct JavaScript.
+A suitable definition of such a function can be
+
+       function _(s) { return s } // dummy function for gettext
+
 =head1 SEE ALSO
 
-tmpl_process.pl,
+tmpl_process3.pl,
 xgettext(1),
 Locale::PO(3),
 translator_doc.txt
@@ -440,4 +463,9 @@ xgettext(1)'s sort option. This will result in translation
 strings inside the generated PO file spuriously moving about
 when tmpl_process3.pl calls msgmerge(1) to update the PO file.
 
+If a Javascript string has leading spaces, it will
+generate strings with spurious leading spaces,
+leading to failure to match the strings when actually generating
+translated files.
+
 =cut