Merge branch 'constraints_in_tax_and_taxkeys'
[kivitendo-erp.git] / scripts / locales.pl
index 6f7cabd..5e6a8a5 100755 (executable)
@@ -37,6 +37,9 @@ my $dbupdir      = "$basedir/sql/Pg-upgrade";
 my $dbupdir2     = "$basedir/sql/Pg-upgrade2";
 my $menufile     = "menu.ini";
 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
+our $self        = {};
+our $missing     = {};
+our @lost        = ();
 
 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit);
 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
@@ -64,6 +67,15 @@ sub find_files {
   return @files;
 }
 
+sub merge_texts {
+# overwrite existing entries with the ones from 'missing'
+  $self->{texts}->{$_} = $missing->{$_} for grep { $missing->{$_} } keys %alllocales;
+
+  # try to set missing entries from lost ones
+  my %lost_by_text = map { ($_->{text} => $_->{translation}) } @lost;
+  $self->{texts}->{$_} = $lost_by_text{$_} for grep { !$self->{texts}{$_} } keys %alllocales;
+}
+
 my @bindir_files = find_files($bindir);
 my @progfiles    = map { m:^(.+)/([^/]+)$:; [ $2, $1 ]  } grep { /\.pl$/ && !/_custom/ } @bindir_files;
 my @customfiles  = grep /_custom/, @bindir_files;
@@ -89,11 +101,6 @@ tie %dir_h, 'IO::Dir', $dbupdir2;
 my @dbplfiles2 = grep { /\.pl$/ } keys %dir_h;
 
 # slurp the translations in
-our $self    = {};
-our $missing = {};
-our @missing = ();
-our @lost    = ();
-
 if (-f "$locales_dir/all") {
   require "$locales_dir/all";
 }
@@ -116,6 +123,9 @@ handle_file($_, $dbupdir)  for @dbplfiles;
 handle_file($_, $dbupdir2) for @dbplfiles2;
 scanmenu($_)               for @menufiles;
 
+# merge entries to translate with entries from files 'missing' and 'lost'
+merge_texts();
+
 # generate all
 generate_file(
   file      => "$locales_dir/all",
@@ -124,15 +134,24 @@ generate_file(
   data_sub  => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
 );
 
+  foreach my $text (keys %$missing) {
+    if ($locale{$text} || $htmllocale{$text}) {
+      unless ($self->{texts}{$text}) {
+        $self->{texts}{$text} = $missing->{$text};
+      }
+    }
+  }
+
+
 # calc and generate missing
-push @missing, grep { !$self->{texts}{$_} } sort keys %alllocales;
+my @new_missing = grep { !$self->{texts}{$_} } sort keys %alllocales;
 
-if (@missing) {
+if (@new_missing) {
   generate_file(
     file      => "$locales_dir/missing",
     header    => $MISSING_HEADER,
     data_name => '$missing',
-    data_sub  => sub { _print_line($_, '', @_) for @missing },
+    data_sub  => sub { _print_line($_, '', @_) for @new_missing },
   );
 }
 
@@ -161,7 +180,7 @@ chomp $trlanguage;
 search_unused_htmlfiles() if $opt_c;
 
 my $count  = scalar keys %alllocales;
-my $notext = scalar @missing;
+my $notext = scalar @new_missing;
 my $per    = sprintf("%.1f", ($count - $notext) / $count * 100);
 print "\n$trlanguage - ${per}%";
 print " - $notext/$count missing" if $notext;
@@ -248,14 +267,6 @@ sub handle_file {
   }
 
   $file =~ s/\.pl//;
-
-  foreach my $text (keys %$missing) {
-    if ($locale{$text} || $htmllocale{$text}) {
-      unless ($self->{texts}{$text}) {
-        $self->{texts}{$text} = $missing->{$text};
-      }
-    }
-  }
 }
 
 sub extract_text_between_parenthesis {
@@ -349,15 +360,23 @@ sub scanfile {
 
       # 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";
+        }
+
+        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 (-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";
+
+        if ($opt_c && !$found_one) {
+          print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
         }
       }
 
@@ -410,7 +429,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) && />/)) {
@@ -634,7 +653,7 @@ __END__
 
 =head1 NAME
 
-locales.pl - Collect strings for translation in Lx-Office
+locales.pl - Collect strings for translation in kivitendo
 
 =head1 SYNOPSIS