a655bdcb5dd88e59fd05fcfaff14077159340a58
[kivitendo-erp.git] / locale / de / locales.pl
1 #!/usr/bin/perl
2
3 # -n do not include custom_ scripts
4 # -v verbose mode, shows progress stuff
5
6 # this version of locles processes not only all required .pl files
7 # but also all parse_html_templated files.
8
9 use POSIX;
10 use FileHandle;
11 use Data::Dumper;
12
13 use List::Util qw(first);
14
15 $| = 1;
16
17 $basedir  = "../..";
18 $bindir   = "$basedir/bin/mozilla";
19 $dbupdir  = "$basedir/sql/Pg-upgrade";
20 $dbupdir2 = "$basedir/sql/Pg-upgrade2";
21 $menufile = "menu.ini";
22 $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
23
24 %referenced_html_files = ();
25
26 # Arguments:
27 #   -v verbose
28 #   -n no custom files
29 #   -h extended checks on HTML templates
30
31 foreach $item (@ARGV) {
32   $item =~ s/-//g;
33   $arg{$item} = 1;
34 }
35
36 opendir DIR, "$bindir" or die "$!";
37 @progfiles = grep { /\.pl$/ && !/(_|^\.)/ } readdir DIR;
38 seekdir DIR, 0;
39 @customfiles = grep /_/, readdir DIR;
40 closedir DIR;
41
42 # put customized files into @customfiles
43 @customfiles = () if ($arg{n});
44
45 if ($arg{n}) {
46   @menufiles = ($menufile);
47 } else {
48   opendir DIR, "$basedir" or die "$!";
49   @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
50   closedir DIR;
51   unshift @menufiles, $menufile;
52 }
53
54 opendir DIR, $dbupdir or die "$!";
55 @dbplfiles = grep { /\.pl$/ } readdir DIR;
56 closedir DIR;
57
58 opendir DIR, $dbupdir2 or die "$!";
59 @dbplfiles2 = grep { /\.pl$/ } readdir DIR;
60 closedir DIR;
61
62 # slurp the translations in
63 if (-f 'all') {
64   require "all";
65 }
66
67 my %old_texts = %{ $self->{texts} };
68
69 # Read HTML templates.
70 #%htmllocale = ();
71 #@htmltemplates = <../../templates/webpages/*/*_master.html>;
72 #foreach $file (@htmltemplates) {
73 #  scanhtmlfile($file);
74 #}
75
76 map({ handle_file($_, $bindir); } @progfiles);
77 map({ handle_file($_, $dbupdir); } @dbplfiles);
78 map({ handle_file($_, $dbupdir2); } @dbplfiles2);
79
80 sub handle_file {
81   my ($file, $dir) = @_;
82   print "\n$file" if $arg{v};
83   %locale = ();
84   %submit = ();
85   %subrt  = ();
86
87   &scanfile("$dir/$file");
88
89   # scan custom_{module}.pl or {login}_{module}.pl files
90   foreach $customfile (@customfiles) {
91     if ($customfile =~ /_$file/) {
92       if (-f "$dir/$customfile") {
93         &scanfile("$dir/$customfile");
94       }
95     }
96   }
97
98   # if this is the menu.pl file
99   if ($file eq 'menu.pl') {
100     foreach $item (@menufiles) {
101       &scanmenu("$basedir/$item");
102     }
103   }
104
105   if ($file eq 'menunew.pl') {
106     foreach $item (@menufiles) {
107       &scanmenu("$basedir/$item");
108       print "." if $arg{v};
109     }
110   }
111
112   $file =~ s/\.pl//;
113
114   eval { require 'missing'; };
115   unlink 'missing';
116
117   foreach $text (keys %$missing) {
118     if ($locale{$text} || $htmllocale{$text}) {
119       unless ($self->{texts}{$text}) {
120         $self->{texts}{$text} = $missing->{$text};
121       }
122     }
123   }
124
125   open FH, ">$file" or die "$! : $file";
126
127   print FH q|#!/usr/bin/perl
128
129 $self->{texts} = {
130 |;
131
132   foreach $key (sort keys %locale) {
133     if ($self->{texts}{$key}) {
134       $text = $self->{texts}{$key};
135     } else {
136       $text = $key;
137     }
138     $text =~ s/'/\\'/g;
139     $text =~ s/\\$/\\\\/;
140
141     $keytext = $key;
142     $keytext =~ s/'/\\'/g;
143     $keytext =~ s/\\$/\\\\/;
144
145     print FH qq|  '$keytext'|
146       . (' ' x (27 - length($keytext)))
147       . qq| => '$text',\n|;
148   }
149
150   print FH q|};
151
152 $self->{subs} = {
153 |;
154
155   foreach $key (sort keys %subrt) {
156     $text = $key;
157     $text =~ s/'/\\'/g;
158     $text =~ s/\\$/\\\\/;
159     print FH qq|  '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
160   }
161
162   foreach $key (sort keys %submit) {
163     $text = ($self->{texts}{$key}) ? $self->{texts}{$key} : $key;
164     $text =~ s/'/\\'/g;
165     $text =~ s/\\$/\\\\/;
166
167     $english_sub = $key;
168     $english_sub =~ s/'/\\'/g;
169     $english_sub =~ s/\\$/\\\\/;
170     $english_sub = lc $key;
171
172     $translated_sub = lc $text;
173     $english_sub    =~ s/( |-|,)/_/g;
174     $translated_sub =~ s/( |-|,)/_/g;
175     print FH qq|  '$translated_sub'|
176       . (' ' x (27 - length($translated_sub)))
177       . qq| => '$english_sub',\n|;
178   }
179
180   print FH q|};
181
182 1;
183 |;
184
185   close FH;
186
187 }
188
189 #foreach $file (@htmltemplates) {
190 #  converthtmlfile($file);
191 #}
192
193 # now print out all
194
195 open FH, ">all" or die "$! : all";
196
197 print FH q|#!/usr/bin/perl
198
199 # These are all the texts to build the translations files.
200 # The file has the form of 'english text'  => 'foreign text',
201 # you can add the translation in this file or in the 'missing' file
202 # run locales.pl from this directory to rebuild the translation files
203
204 $self->{texts} = {
205 |;
206
207 foreach $key (sort keys %alllocales) {
208   $text = $self->{texts}{$key};
209
210   $count++;
211
212   $text =~ s/'/\\'/g;
213   $text =~ s/\\$/\\\\/;
214   $key  =~ s/'/\\'/g;
215   $key  =~ s/\\$/\\\\/;
216
217   unless ($text) {
218     $notext++;
219     push @missing, $key;
220   }
221
222   print FH qq|  '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
223
224 }
225
226 print FH q|};
227
228 1;
229 |;
230
231 close FH;
232
233 if (@missing) {
234   open FH, ">missing" or die "$! : missing";
235
236   print FH q|#!/usr/bin/perl
237
238 # add the missing texts and run locales.pl to rebuild
239
240 $missing = {
241 |;
242
243   foreach $text (@missing) {
244     print FH qq|  '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
245   }
246
247   print FH q|};
248
249 1;
250 |;
251
252   close FH;
253
254 }
255
256 @lost = ();
257
258 if (-f "lost") {
259   require "lost";
260   unlink "lost";
261 }
262
263 while (($text, $translation) = each %old_texts) {
264   next if ($alllocales{$text});
265
266   push @lost, { 'text' => $text, 'translation' => $translation };
267 }
268
269 if (scalar @lost) {
270   splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
271
272   open FH, ">lost";
273   print FH "#!/usr/bin/perl\n\n" .
274     "# The last 50 texts that have been removed.\n" .
275     "# This file will be auto-generated by locales.pl. Do not edit it.\n\n" .
276     "\@lost = (\n";
277
278   foreach $entry (@lost) {
279     $entry->{text}        =~ s/\'/\\\'/;
280     $entry->{translation} =~ s/\'/\\\'/;
281     print FH "  { 'text' => '$entry->{text}', 'translation' => '$entry->{translation}' },\n";
282   }
283
284   print FH ");\n\n1;\n";
285   close FH;
286 }
287
288 open(FH, "LANGUAGE");
289 @language = <FH>;
290 close(FH);
291 $trlanguage = $language[0];
292 chomp $trlanguage;
293
294 if ($arg{h}) {
295   search_unused_htmlfiles();
296   search_translated_htmlfiles_wo_master();
297 }
298
299 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
300 print "\n$trlanguage - ${per}%";
301 print " - $notext missing" if $notext;
302 print "\n";
303
304 exit;
305
306 # eom
307
308 sub extract_text_between_parenthesis {
309   my ($fh, $line) = @_;
310   my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
311
312   while (1) {
313     if (length($line) <= $pos) {
314       $line = <$fh>;
315       return ($text, "") unless ($line);
316       $pos = 0;
317     }
318
319     my $cur_char = substr($line, $pos, 1);
320
321     if (!$inside_string) {
322       if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
323         $inside_string = substr($line, $pos + 2, 1);
324         $pos += 2;
325
326       } elsif ((length($line) >= ($pos + 2)) &&
327                (substr($line, $pos, 1) eq "q")) {
328         $inside_string = substr($line, $pos + 1, 1);
329         $pos++;
330
331       } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
332         $inside_string = $cur_char;
333
334       } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
335         return ($text, substr($line, $pos + 1));
336       }
337
338     } else {
339       if ($quote_next) {
340         $text .= $cur_char;
341         $quote_next = 0;
342
343       } elsif ($cur_char eq '\\') {
344         $text .= $cur_char;
345         $quote_next = 1;
346
347       } elsif ($cur_char eq $inside_string) {
348         undef($inside_string);
349
350       } else {
351         $text .= $cur_char;
352
353       }
354     }
355     $pos++;
356   }
357 }
358
359 sub scanfile {
360   my $file = shift;
361   my $dont_include_subs = shift;
362   my $scanned_files = shift;
363
364   $scanned_files = {} unless ($scanned_files);
365   return if ($scanned_files->{$file});
366   $scanned_files->{$file} = 1;
367
368   if (!defined $cached{$file}) {
369
370     return unless (-f "$file");
371
372     my $fh = new FileHandle;
373     open $fh, "$file" or die "$! : $file";
374
375     my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
376
377     while (<$fh>) {
378       $line_no++;
379
380       # is this another file
381       if (/require\s+\W.*\.pl/) {
382         my $newfile = $&;
383         $newfile =~ s/require\s+\W//;
384         $newfile =~ s|bin/mozilla||;
385 #         &scanfile("$bindir/$newfile", 0, $scanned_files);
386          $cached{$file}{scan}{"$bindir/$newfile"} = 1;
387       } elsif (/use\s+SL::(.*?);/) {
388         my $module =  $1;
389         $module    =~ s|::|/|g;
390 #         &scanfile("../../SL/${1}.pm", 1, $scanned_files);
391         $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
392       }
393
394       # is this a template call?
395       if (/parse_html_template2?\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
396         my $newfile = "$basedir/templates/webpages/$1_master.html";
397         if (/parse_html_template2/) {
398           print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base($newfile) . ".\n";
399         }
400         if (-f $newfile) {
401 #           &scanhtmlfile($newfile);
402 #           &converthtmlfile($newfile);
403            $cached{$file}{scanh}{$newfile} = 1;
404           print "." if $arg{v};
405         } elsif ($arg{h}) {
406           print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
407         }
408       }
409
410       # is this a sub ?
411       if (/^sub /) {
412         next if ($dont_include_subs);
413         ($null, $subrt) = split / +/;
414 #        $subrt{$subrt} = 1;
415         $cached{$file}{subr}{$subrt} = 1;
416         next;
417       }
418
419       my $rc = 1;
420
421       while ($rc) {
422         if (/Locale/) {
423           unless (/^use /) {
424             my ($null, $country) = split /,/;
425             $country =~ s/^ +[\"\']//;
426             $country =~ s/[\"\'].*//;
427           }
428         }
429
430         my $postmatch = "";
431
432         # is it a submit button before $locale->
433         if (/$submitsearch/) {
434           $postmatch = "$'";
435           if ($` !~ /locale->text/) {
436             $is_submit   = 1;
437             $sub_line_no = $line_no;
438           }
439         }
440
441         my ($found) = /locale->text.*?\(/;
442         my $postmatch = "$'";
443
444         if ($found) {
445           my $string;
446           ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
447           $postmatch = $_;
448
449           # if there is no $ in the string record it
450           unless (($string =~ /\$\D.*/) || ("" eq $string)) {
451
452             # this guarantees one instance of string
453 #            $locale{$string} = 1;
454             $cached{$file}{locale}{$string} = 1;
455
456             # this one is for all the locales
457 #            $alllocales{$string} = 1;
458             $cached{$file}{all}{$string} = 1;
459
460             # is it a submit button before $locale->
461             if ($is_submit) {
462 #              $submit{$string} = 1;
463               $cached{$file}{submit}{$string} = 1;
464             }
465           }
466         } elsif ($postmatch =~ />/) {
467           $is_submit = 0;
468         }
469
470         # exit loop if there are no more locales on this line
471         ($rc) = ($postmatch =~ /locale->text/);
472
473         if (   ($postmatch =~ />/)
474             || (!$found && ($sub_line_no != $line_no) && />/)) {
475           $is_submit = 0;
476         }
477       }
478     }
479
480     close($fh);
481
482   }
483
484   map { $alllocales{$_} = 1 }   keys %{$cached{$file}{all}};
485   map { $locale{$_} = 1 }       keys %{$cached{$file}{locale}};
486   map { $submit{$_} = 1 }       keys %{$cached{$file}{submit}};
487   map { $subrt{$_} = 1 }        keys %{$cached{$file}{subr}};
488   map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
489   map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
490   map { &scanhtmlfile($_)  }    keys %{$cached{$file}{scanh}};
491
492   @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
493 }
494
495 sub scanmenu {
496   my $file = shift;
497
498   my $fh = new FileHandle;
499   open $fh, "$file" or die "$! : $file";
500
501   my @a = grep m/^\[/, <$fh>;
502   close($fh);
503
504   # strip []
505   grep { s/(\[|\])//g } @a;
506
507   foreach my $item (@a) {
508     @b = split /--/, $item;
509     foreach $string (@b) {
510       chomp $string;
511       $locale{$string}     = 1;
512       $alllocales{$string} = 1;
513     }
514   }
515
516 }
517
518 sub scanhtmlfile {
519   local *IN;
520
521   if (!defined $cached{$_[0]}) {
522     my %plugins = ( 'loaded' => { }, 'needed' => { } );
523
524     open(IN, $_[0]) || die $_[0];
525
526     my $copying = 0;
527     my $issubmit = 0;
528     my $text = "";
529     while (my $line = <IN>) {
530       chomp($line);
531
532       while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
533         $plugins{loaded}->{$1} = 1;
534       }
535
536       while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
537         my $plugin = $1;
538         $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator));
539       }
540
541       while ("" ne $line) {
542         if (!$copying) {
543           if ($line =~ m|<translate>|i) {
544             my $eom = $+[0];
545             if ($` =~ /$submitsearch/) {
546               $issubmit = 1
547             }
548             substr($line, 0, $eom) = "";
549             $copying = 1;
550           } else {
551             $line = "";
552           }
553
554         } else {
555           if ($line =~ m|</translate>|i) {
556             $text .= $`;
557             substr($line, 0, $+[0]) = "";
558             $text =~ s/\s+/ /g;
559
560             $copying = 0; 
561             if ($issubmit) {
562   #            $submit{$text} = 1;
563                $cached{$_[0]}{submit}{$text} = 1;
564               $issubmit = 0;
565             }
566   #          $alllocales{$text} = 1;
567              $cached{$_[0]}{all}{$text} = 1;
568   #          $htmllocale{$text} = 1;
569              $cached{$_[0]}{html}{$text} = 1;
570             $text = "";
571
572           } else {
573             $text .= $line;
574             $line = "";
575           }
576         }
577       }
578     }
579
580     close(IN);
581
582     foreach my $plugin (keys %{ $plugins{needed} }) {
583       next if ($plugins{loaded}->{$plugin});
584       print "E: " . strip_base($_[0]) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
585     }
586
587     &converthtmlfile($_[0]);
588   }
589
590   # copy back into global arrays
591   map { $alllocales{$_} = 1 }  keys %{$cached{$_[0]}{all}};
592   map { $htmllocales{$_} = 1 } keys %{$cached{$_[0]}{html}};
593   map { $submit{$_} = 1 }      keys %{$cached{$_[0]}{submit}};
594 }
595
596 sub converthtmlfile {
597   local *IN;
598   local *OUT;
599
600   my $file = shift;
601
602   open(IN, $file) || die;
603
604   my $langcode = (split("/", getcwd()))[-1];
605   $file =~ s/_master.html$/_${langcode}.html/;
606
607   open(OUT, ">$file") || die;
608
609   my $copying = 0;
610   my $text = "";
611   while (my $line = <IN>) {
612     chomp($line);
613     if ("" eq $line) {
614       print(OUT "\n");
615       next;
616     }
617
618     while ("" ne $line) {
619       if (!$copying) {
620         if ($line =~ m|<translate>|i) {
621           print(OUT $`);
622           substr($line, 0, $+[0]) = "";
623           $copying = 1;
624           print(OUT "\n") if ("" eq $line);
625
626         } else {
627           print(OUT "${line}\n");
628           $line = "";
629         }
630
631       } else {
632         if ($line =~ m|</translate>|i) {
633           $text .= $`;
634           substr($line, 0, $+[0]) = "";
635           $text =~ s/\s+/ /g;
636           $copying = 0;
637           $alllocales{$text} = 1;
638           $htmllocale{$text} = 1;
639           print(OUT $self->{"texts"}{$text} || $text);
640           print(OUT "\n") if ("" eq $line);
641           $text = "";
642
643         } else {
644           $text .= $line;
645           $line = "";
646         }
647       }
648     }
649   }
650
651   close(IN);
652   close(OUT);
653 }
654
655 sub search_unused_htmlfiles {
656   my @unscanned_dirs = ('../../templates/webpages');
657
658   while (scalar @unscanned_dirs) {
659     my $dir = shift @unscanned_dirs;
660
661     foreach my $entry (<$dir/*>) {
662       if (-d $entry) {
663         push @unscanned_dirs, $entry;
664
665       } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
666         print "W: unused HTML template: " . strip_base($entry) . "\n";
667
668       }
669     }
670   }
671 }
672
673 sub search_translated_htmlfiles_wo_master {
674   my @unscanned_dirs = ('../../templates/webpages');
675
676   while (scalar @unscanned_dirs) {
677     my $dir = shift @unscanned_dirs;
678
679     foreach my $entry (<$dir/*>) {
680       if (-d $entry) {
681         push @unscanned_dirs, $entry;
682
683       } elsif (($entry =~ /_[a-z]+\.html$/) && ($entry !~ /_master.html$/) && -f $entry) {
684         my $master =  $entry;
685         $master    =~ s/[a-z]+\.html$/master.html/;
686         if (! -f $master) {
687           print "W: translated HTML template without master: " . strip_base($entry) . "\n";
688         }
689       }
690     }
691   }
692 }
693
694 sub strip_base {
695   my $s =  "$_[0]";             # Create a copy of the string.
696
697   $s    =~ s|^../../||;
698   $s    =~ s|templates/webpages/||;
699
700   return $s;
701 }