Perl-Scripte mit UTF-8 encodierten Umlaugen brauchen "use utf8"
[kivitendo-erp.git] / scripts / locales.pl
index dd7c4ca..ff2638b 100755 (executable)
@@ -6,6 +6,7 @@
 # this version of locles processes not only all required .pl files
 # but also all parse_html_templated files.
 
+use utf8;
 use strict;
 
 use Data::Dumper;
@@ -23,13 +24,12 @@ $OUTPUT_AUTOFLUSH = 1;
 my $opt_v  = 0;
 my $opt_n  = 0;
 my $opt_c  = 0;
-my $lang;
 my $debug  = 0;
 
 parse_args();
 
-my $basedir      = ".";
-my $locales_dir  = "$basedir/locale/$lang";
+my $basedir      = "../..";
+my $locales_dir  = ".";
 my $bindir       = "$basedir/bin/mozilla";
 my $dbupdir      = "$basedir/sql/Pg-upgrade";
 my $dbupdir2     = "$basedir/sql/Pg-upgrade2";
@@ -86,6 +86,9 @@ if (-f "$locales_dir/lost") {
   unlink "$locales_dir/lost";
 }
 
+my $charset = slurp("$locales_dir/charset") || 'utf-8';
+chomp $charset;
+
 my %old_texts = %{ $self->{texts} || {} };
 
 map({ handle_file($_, $bindir); } @progfiles);
@@ -126,7 +129,7 @@ if (scalar @lost) {
     delim     => '()',
     data_name => '@lost',
     data_sub  => sub {
-      _print_line($_->{text}, $_->{translation}, @_, template => "  { 'text' => %s, 'translation' => %s },") for @lost;
+      _print_line($_->{text}, $_->{translation}, @_, template => "  { 'text' => %s, 'translation' => %s },\n") for @lost;
     },
   );
 }
@@ -158,8 +161,8 @@ EOL
 # add the missing texts and run locales.pl to rebuild
 EOL
   $LOST_HEADER  = <<EOL;
-# The last 50 texts that have been removed.
-# This file will be auto-generated by locales.pl. Do not edit it.
+# The last 50 text strings, that have been removed.
+# This file has been auto-generated by locales.pl. Please don't edit!
 EOL
 }
 
@@ -185,7 +188,25 @@ sub parse_args {
     exit 0;
   }
 
-  $lang = shift @ARGV   || croak 'need language code as argument';
+  if (@ARGV) {
+    my $arg = shift @ARGV;
+    my $ok  = 0;
+    foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
+      next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
+      $ok = chdir $dir;
+      last;
+    }
+
+    if (!$ok) {
+      print "The locale directory '$arg' could not be found.\n";
+      exit 1;
+    }
+
+  } elsif (!-f 'all' || !-f 'LANGUAGE') {
+    print "locales.pl was not called from a locale/* subdirectory,\n"
+      .   "and no locale directory name was given.\n";
+    exit 1;
+  }
 }
 
 sub handle_file {
@@ -454,26 +475,26 @@ sub scanhtmlfile {
 
       while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
         my $plugin = $1;
-        $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator));
+        $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator L));
       }
 
       while ($line =~ m/(?:             # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
                           (LxERP\.t8)\( #   LxERP.t8(                             ::Parameter $1::
-                          ([\'\"])      #   Anfang des zu übersetzenden Strings   ::Parameter $2::
-                          (.*?)         #   Der zu übersetzende String            ::Parameter $3::
-                          (?<!\\)\2     #   Ende des zu übersetzenden Strings
+                          ([\'\"])      #   Anfang des zu übersetzenden Strings   ::Parameter $2::
+                          (.*?)         #   Der zu übersetzende String            ::Parameter $3::
+                          (?<!\\)\2     #   Ende des zu übersetzenden Strings
                         |               # Start von Variante 2: [% '...' | $T8 %]
                           \[\%          #   Template-Start-Tag
-                          [\-~#]?       #   Whitespace-Unterdrückung
+                          [\-~#]?       #   Whitespace-Unterdrückung
                           \s*           #   Optional beliebig viele Whitespace
-                          ([\'\"])      #   Anfang des zu übersetzenden Strings   ::Parameter $4::
-                          (.*?)         #   Der zu übersetzende String            ::Parameter $5::
-                          (?<!\\)\4     #   Ende des zu übersetzenden Strings
+                          ([\'\"])      #   Anfang des zu übersetzenden Strings   ::Parameter $4::
+                          (.*?)         #   Der zu übersetzende String            ::Parameter $5::
+                          (?<!\\)\4     #   Ende des zu übersetzenden Strings
                           \s*\|\s*      #   Pipe-Zeichen mit optionalen Whitespace davor und danach
                           (\$T8)        #   Filteraufruf                          ::Parameter $6::
-                          .*?           #   Optionale Argumente für den Filter
+                          .*?           #   Optionale Argumente für den Filter
                           \s*           #   Whitespaces
-                          [\-~#]?       #   Whitespace-Unterdrückung
+                          [\-~#]?       #   Whitespace-Unterdrückung
                           \%\]          #   Template-Ende-Tag
                         )
                        /ix) {
@@ -491,7 +512,7 @@ sub scanhtmlfile {
       }
 
       while ($line =~ m/\[\%          # Template-Start-Tag
-                        [\-~#]?       # Whitespace-Unterdrückung
+                        [\-~#]?       # Whitespace-Unterdrückung
                         \s*           # Optional beliebig viele Whitespace
                         (?:           # Die erkannten Template-Direktiven
                           PROCESS
@@ -556,7 +577,7 @@ sub strip_base {
 
 sub _single_quote {
   my $val = shift;
-  $val =~ s/('|\\$)/\\$1/g;
+  $val =~ s/(\'|\\$)/\\$1/g;
   return  "'" . $val .  "'";
 }
 
@@ -579,9 +600,12 @@ sub generate_file {
   my $data_name = $params{data_name};
   my @delim     = split //, ($params{delim} || '{}');
 
-  open my $fh, '>', $file or die "$! : $file";
+  open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
+
+  $charset =~ s/\r?\n//g;
+  my $emacs_charset = lc $charset;
 
-  print $fh "#!/usr/bin/perl\n\n";
+  print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
   print $fh $header, "\n" if $header;
   print $fh "$data_name = $delim[0]\n" if $data_name;
 
@@ -591,6 +615,11 @@ sub generate_file {
   close $fh;
 }
 
+sub slurp {
+  my $file = shift;
+  do { local ( @ARGV, $/ ) = $file; <> }
+}
+
 __END__
 
 =head1 NAME
@@ -599,7 +628,7 @@ locales.pl - Collect strings for translation in Lx-Office
 
 =head1 SYNOPSIS
 
-locales.pl [options]
+locales.pl [options] lang_code
 
  Options:
   -n, --no-custom-files  Do not process files whose name contains "_"
@@ -630,7 +659,6 @@ Be more verbose.
 =head1 DESCRIPTION
 
 This script collects strings from Perl files, the menu.ini file and
-HTML templates and puts them into the file "all" for translation.  It
-also distributes those translations back to the individual files.
+HTML templates and puts them into the file "all" for translation.
 
 =cut