]> wagnertech.de Git - mfinanz.git/blobdiff - scripts/locales.pl
Merge branch 'master' into dev
[mfinanz.git] / scripts / locales.pl
index 0c96006f460cfb04ae76d8ed08fbd10f364ef8c1..00dc7fe316fb8b76d658cf75fa2f72d717259da3 100755 (executable)
@@ -36,12 +36,13 @@ my @progdirs     = ( "$basedir/SL" );
 my $dbupdir      = "$basedir/sql/Pg-upgrade";
 my $dbupdir2     = "$basedir/sql/Pg-upgrade2";
 my $menufile     = "menu.ini";
+my @javascript_dirs = ($basedir .'/js', $basedir .'/templates/webpages');
 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
 our $self        = {};
 our $missing     = {};
 our @lost        = ();
 
-my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit);
+my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit, %jslocale);
 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
 
 init();
@@ -123,6 +124,10 @@ handle_file($_, $dbupdir)  for @dbplfiles;
 handle_file($_, $dbupdir2) for @dbplfiles2;
 scanmenu($_)               for @menufiles;
 
+for my $file_name (map({find_files($_)} @javascript_dirs)) {
+  scan_javascript_file($file_name);
+}
+
 # merge entries to translate with entries from files 'missing' and 'lost'
 merge_texts();
 
@@ -134,6 +139,16 @@ generate_file(
   data_sub  => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
 );
 
+open(my $js_file, '>:encoding(utf8)', $locales_dir .'/js.js') || die;
+print $js_file '{'."\n";
+my $first_entry = 1;
+for my $key (sort(keys(%jslocale))) {
+  print $js_file (!$first_entry ? ',' : '') . _double_quote($key) .':'. _double_quote($self->{texts}{$key}) ."\n";
+  $first_entry = 0;
+}
+print $js_file '}'."\n";
+close($js_file);
+
   foreach my $text (keys %$missing) {
     if ($locale{$text} || $htmllocale{$text}) {
       unless ($self->{texts}{$text}) {
@@ -358,17 +373,32 @@ sub scanfile {
         $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
       }
 
+      # Some calls to render() are split over multiple lines. Deal
+      # with that.
+      while (/(?:parse_html_template2?|render)\s*\( *$/) {
+        $_ .= <$fh>;
+        chomp;
+      }
+
       # is this a template call?
       if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
-        my $newfile = "$basedir/templates/webpages/$1.html";
+        my $new_file_base = "$basedir/templates/webpages/$1.";
         if (/parse_html_template2/) {
-          print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
+          print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base("${new_file_base}html") . ".\n";
         }
-        if (-f $newfile) {
-           $cached{$file}{scanh}{$newfile} = 1;
-          print "." if $opt_v;
-        } elsif ($opt_c) {
-          print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
+
+        my $found_one = 0;
+        foreach my $ext (qw(html js json)) {
+          my $new_file = "${new_file_base}${ext}";
+          if (-f $new_file) {
+            $cached{$file}{scanh}{$new_file} = 1;
+            print "." if $opt_v;
+            $found_one = 1;
+          }
+        }
+
+        if ($opt_c && !$found_one) {
+          print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
         }
       }
 
@@ -377,7 +407,7 @@ sub scanfile {
       while ($rc) {
         if (/Locale/) {
           unless (/^use /) {
-            my ($null, $country) = split /,/;
+            my ($null, $country) = split(/,/);
             $country =~ s/^ +[\"\']//;
             $country =~ s/[\"\'].*//;
           }
@@ -421,7 +451,7 @@ sub scanfile {
         }
 
         # exit loop if there are no more locales on this line
-        ($rc) = ($postmatch =~ /locale->text/);
+        ($rc) = ($postmatch =~ /locale->text | \b t8/x);
 
         if (   ($postmatch =~ />/)
             || (!$found && ($sub_line_no != $line_no) && />/)) {
@@ -496,9 +526,11 @@ 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 L));
+        $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator JSON L P));
       }
 
+      $plugins{needed}->{T8} = 1 if $line =~ m/\[\%.*\|.*\$T8/;
+
       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::
@@ -569,6 +601,31 @@ sub scanhtmlfile {
   $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
 }
 
+sub scan_javascript_file {
+  my ($file) = @_;
+
+  open(my $fh, $file) || die('can not open file: '. $file);
+
+  while( my $line = readline($fh) ) {
+    while( $line =~ m/
+                    kivi.t8
+                    \s*
+                    \(
+                    \s*
+                    ([\'\"])
+                    (.*?)
+                    (?<!\\)\1
+                    /ixg )
+    {
+      my $text = unescape_template_string($2);
+
+      $jslocale{$text} = 1;
+      $alllocales{$text} = 1;
+    }
+  }
+
+  close($fh);
+}
 sub search_unused_htmlfiles {
   my @unscanned_dirs = ('../../templates/webpages');
 
@@ -602,6 +659,12 @@ sub _single_quote {
   return  "'" . $val .  "'";
 }
 
+sub _double_quote {
+  my $val = shift;
+  $val =~ s/(\"|\\$)/\\$1/g;
+  return  '"'. $val .'"';
+}
+
 sub _print_line {
   my $key      = _single_quote(shift);
   my $text     = _single_quote(shift);