Sortierung im Buchungsjournal verbessert bei mehreren Konten fuer einen Vorgang am...
[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 $| = 1;
14
15 $basedir  = "../..";
16 $bindir   = "$basedir/bin/mozilla";
17 $dbupdir  = "$basedir/sql/Pg-upgrade";
18 $menufile = "menu.ini";
19 $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
20
21 foreach $item (@ARGV) {
22   $item =~ s/-//g;
23   $arg{$item} = 1;
24 }
25
26 opendir DIR, "$bindir" or die "$!";
27 @progfiles = grep { /\.pl/; !/(_|^\.)/ } readdir DIR;
28 seekdir DIR, 0;
29 @customfiles = grep /_/, readdir DIR;
30 closedir DIR;
31
32 # put customized files into @customfiles
33 @customfiles = () if ($arg{n});
34
35 if ($arg{n}) {
36   @menufiles = ($menufile);
37 } else {
38   opendir DIR, "$basedir" or die "$!";
39   @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
40   closedir DIR;
41   unshift @menufiles, $menufile;
42 }
43
44 opendir DIR, $dbupdir or die "$!";
45 @dbplfiles = grep { /\.pl$/ } readdir DIR;
46 closedir DIR;
47
48 # slurp the translations in
49 if (-f 'all') {
50   require "all";
51 }
52
53 # Read HTML templates.
54 #%htmllocale = ();
55 #@htmltemplates = <../../templates/webpages/*/*_master.html>;
56 #foreach $file (@htmltemplates) {
57 #  scanhtmlfile($file);
58 #}
59
60 map({ handle_file($_, $bindir); } @progfiles);
61 map({ handle_file($_, $dbupdir); } @dbplfiles);
62
63 sub handle_file {
64   my ($file, $dir) = @_;
65   print "\n$file" if $arg{v};
66   %locale = ();
67   %submit = ();
68   %subrt  = ();
69
70   &scanfile("$dir/$file");
71
72   # scan custom_{module}.pl or {login}_{module}.pl files
73   foreach $customfile (@customfiles) {
74     if ($customfile =~ /_$file/) {
75       if (-f "$dir/$customfile") {
76         &scanfile("$dir/$customfile");
77       }
78     }
79   }
80
81   # if this is the menu.pl file
82   if ($file eq 'menu.pl') {
83     foreach $item (@menufiles) {
84       &scanmenu("$basedir/$item");
85     }
86   }
87
88   if ($file eq 'menunew.pl') {
89     foreach $item (@menufiles) {
90       &scanmenu("$basedir/$item");
91       print "." if $arg{v};
92     }
93   }
94
95   $file =~ s/\.pl//;
96
97   eval { require 'missing'; };
98   unlink 'missing';
99
100   foreach $text (keys %$missing) {
101     if ($locale{$text} || $htmllocale{$text}) {
102       unless ($self->{texts}{$text}) {
103         $self->{texts}{$text} = $missing->{$text};
104       }
105     }
106   }
107
108   open FH, ">$file" or die "$! : $file";
109
110   print FH q|$self->{texts} = {
111 |;
112
113   foreach $key (sort keys %locale) {
114     if ($self->{texts}{$key}) {
115       $text = $self->{texts}{$key};
116     } else {
117       $text = $key;
118     }
119     $text =~ s/'/\\'/g;
120     $text =~ s/\\$/\\\\/;
121
122     $keytext = $key;
123     $keytext =~ s/'/\\'/g;
124     $keytext =~ s/\\$/\\\\/;
125
126     print FH qq|  '$keytext'|
127       . (' ' x (27 - length($keytext)))
128       . qq| => '$text',\n|;
129   }
130
131   print FH q|};
132
133 $self->{subs} = {
134 |;
135
136   foreach $key (sort keys %subrt) {
137     $text = $key;
138     $text =~ s/'/\\'/g;
139     $text =~ s/\\$/\\\\/;
140     print FH qq|  '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
141   }
142
143   foreach $key (sort keys %submit) {
144     $text = ($self->{texts}{$key}) ? $self->{texts}{$key} : $key;
145     $text =~ s/'/\\'/g;
146     $text =~ s/\\$/\\\\/;
147
148     $english_sub = $key;
149     $english_sub =~ s/'/\\'/g;
150     $english_sub =~ s/\\$/\\\\/;
151     $english_sub = lc $key;
152
153     $translated_sub = lc $text;
154     $english_sub    =~ s/( |-|,)/_/g;
155     $translated_sub =~ s/( |-|,)/_/g;
156     print FH qq|  '$translated_sub'|
157       . (' ' x (27 - length($translated_sub)))
158       . qq| => '$english_sub',\n|;
159   }
160
161   print FH q|};
162
163 1;
164 |;
165
166   close FH;
167
168 }
169
170 #foreach $file (@htmltemplates) {
171 #  converthtmlfile($file);
172 #}
173
174 # now print out all
175
176 open FH, ">all" or die "$! : all";
177
178 print FH q|# These are all the texts to build the translations files.
179 # The file has the form of 'english text'  => 'foreign text',
180 # you can add the translation in this file or in the 'missing' file
181 # run locales.pl from this directory to rebuild the translation files
182
183 $self->{texts} = {
184 |;
185
186 foreach $key (sort keys %alllocales) {
187   $text = $self->{texts}{$key};
188
189   $count++;
190
191   $text =~ s/'/\\'/g;
192   $text =~ s/\\$/\\\\/;
193   $key  =~ s/'/\\'/g;
194   $key  =~ s/\\$/\\\\/;
195
196   unless ($text) {
197     $notext++;
198     push @missing, $key;
199   }
200
201   print FH qq|  '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
202
203 }
204
205 print FH q|};
206
207 1;
208 |;
209
210 close FH;
211
212 if (@missing) {
213   open FH, ">missing" or die "$! : missing";
214
215   print FH q|# add the missing texts and run locales.pl to rebuild
216
217 $missing = {
218 |;
219
220   foreach $text (@missing) {
221     print FH qq|  '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
222   }
223
224   print FH q|};
225
226 1;
227 |;
228
229   close FH;
230
231 }
232
233 open(FH, "LANGUAGE");
234 @language = <FH>;
235 close(FH);
236 $trlanguage = $language[0];
237 chomp $trlanguage;
238
239 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
240 print "\n$trlanguage - ${per}%";
241 print " - $notext missing" if $notext;
242 print "\n";
243
244 exit;
245
246 # eom
247
248 sub extract_text_between_parenthesis {
249   my ($fh, $line) = @_;
250   my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
251
252   while (1) {
253     if (length($line) <= $pos) {
254       $line = <$fh>;
255       return ($text, "") unless ($line);
256       $pos = 0;
257     }
258
259     my $cur_char = substr($line, $pos, 1);
260
261     if (!$inside_string) {
262       if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
263         $inside_string = substr($line, $pos + 2, 1);
264         $pos += 2;
265
266       } elsif ((length($line) >= ($pos + 2)) &&
267                (substr($line, $pos, 1) eq "q")) {
268         $inside_string = substr($line, $pos + 1, 1);
269         $pos++;
270
271       } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
272         $inside_string = $cur_char;
273
274       } elsif ($cur_char eq ")") {
275         return ($text, substr($line, $pos + 1));
276       }
277
278     } else {
279       if ($quote_next) {
280         $text .= $cur_char;
281         $quote_next = 0;
282
283       } elsif ($cur_char eq '\\') {
284         $text .= $cur_char;
285         $quote_next = 1;
286
287       } elsif ($cur_char eq $inside_string) {
288         undef($inside_string);
289
290       } else {
291         $text .= $cur_char;
292
293       }
294     }
295     $pos++;
296   }
297 }
298
299 sub scanfile {
300   my $file = shift;
301   my $dont_include_subs = shift;
302   my $scanned_files = shift;
303
304   $scanned_files = {} unless ($scanned_files);
305   return if ($scanned_files->{$file});
306   $scanned_files->{$file} = 1;
307
308   if (!defined $cached{$file}) {
309
310     return unless (-f "$file");
311
312     my $fh = new FileHandle;
313     open $fh, "$file" or die "$! : $file";
314
315     my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
316
317     while (<$fh>) {
318       $line_no++;
319
320       # is this another file
321       if (/require\s+\W.*\.pl/) {
322         my $newfile = $&;
323         $newfile =~ s/require\s+\W//;
324         $newfile =~ s/\$form->{path}\///;
325 #         &scanfile("$bindir/$newfile", 0, $scanned_files);
326          $cached{$file}{scan}{"$bindir/$newfile"} = 1;
327       } elsif (/use\s+SL::(.*?);/) {
328 #         &scanfile("../../SL/${1}.pm", 1, $scanned_files);
329          $cached{$file}{scannosubs}{"../../SL/${1}.pm"} = 1;
330       }
331
332       # is this a template call?
333       if (/parse_html_template\s*\(\s*[\"\']([\w\/]+)/) {
334         my $newfile = "$basedir/templates/webpages/$1_master.html";
335         if (-f $newfile) {
336 #           &scanhtmlfile($newfile);
337 #           &converthtmlfile($newfile);
338            $cached{$file}{scanh}{$newfile} = 1;
339           print "." if $arg{v};
340         }
341       }
342
343       # is this a sub ?
344       if (/^sub /) {
345         next if ($dont_include_subs);
346         ($null, $subrt) = split / +/;
347 #        $subrt{$subrt} = 1;
348         $cached{$file}{subr}{$subrt} = 1;
349         next;
350       }
351
352       my $rc = 1;
353
354       while ($rc) {
355         if (/Locale/) {
356           unless (/^use /) {
357             my ($null, $country) = split /,/;
358             $country =~ s/^ +[\"\']//;
359             $country =~ s/[\"\'].*//;
360           }
361         }
362
363         my $postmatch = "";
364
365         # is it a submit button before $locale->
366         if (/$submitsearch/) {
367           $postmatch = "$'";
368           if ($` !~ /locale->text/) {
369             $is_submit   = 1;
370             $sub_line_no = $line_no;
371           }
372         }
373
374         my ($found) = /locale->text.*?\(/;
375         my $postmatch = "$'";
376
377         if ($found) {
378           my $string;
379           ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
380           $postmatch = $_;
381
382           # if there is no $ in the string record it
383           unless (($string =~ /\$\D.*/) || ("" eq $string)) {
384
385             # this guarantees one instance of string
386 #            $locale{$string} = 1;
387             $cached{$file}{locale}{$string} = 1;
388
389             # this one is for all the locales
390 #            $alllocales{$string} = 1;
391             $cached{$file}{all}{$string} = 1;
392
393             # is it a submit button before $locale->
394             if ($is_submit) {
395 #              $submit{$string} = 1;
396               $cached{$file}{submit}{$string} = 1;
397             }
398           }
399         } elsif ($postmatch =~ />/) {
400           $is_submit = 0;
401         }
402
403         # exit loop if there are no more locales on this line
404         ($rc) = ($postmatch =~ /locale->text/);
405
406         if (   ($postmatch =~ />/)
407             || (!$found && ($sub_line_no != $line_no) && />/)) {
408           $is_submit = 0;
409         }
410       }
411     }
412
413     close($fh);
414
415   }
416
417   map { $alllocales{$_} = 1 }   keys %{$cached{$file}{all}};
418   map { $locale{$_} = 1 }       keys %{$cached{$file}{locale}};
419   map { $submit{$_} = 1 }       keys %{$cached{$file}{submit}};
420   map { $subrt{$_} = 1 }        keys %{$cached{$file}{subr}};
421   map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
422   map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
423   map { &scanhtmlfile($_)  }    keys %{$cached{$file}{scanh}};
424 }
425
426 sub scanmenu {
427   my $file = shift;
428
429   my $fh = new FileHandle;
430   open $fh, "$file" or die "$! : $file";
431
432   my @a = grep /^\[/, <$fh>;
433   close($fh);
434
435   # strip []
436   grep { s/(\[|\])//g } @a;
437
438   foreach my $item (@a) {
439     @b = split /--/, $item;
440     foreach $string (@b) {
441       chomp $string;
442       $locale{$string}     = 1;
443       $alllocales{$string} = 1;
444     }
445   }
446
447 }
448
449 sub scanhtmlfile {
450   local *IN;
451  
452   if (!defined $cached{$_[0]}) {
453  
454     open(IN, $_[0]) || die $_[0];
455
456     my $copying = 0;
457     my $issubmit = 0;
458     my $text = "";
459     while (my $line = <IN>) {
460       chomp($line);
461
462       while ("" ne $line) {
463         if (!$copying) {
464           if ($line =~ m|<translate>|i) {
465             my $eom = $+[0];
466             if ($` =~ /$submitsearch/) {
467               $issubmit = 1
468             }
469             substr($line, 0, $eom) = "";
470             $copying = 1;
471           } else {
472             $line = "";
473           }
474
475         } else {
476           if ($line =~ m|</translate>|i) {
477             $text .= $`;
478             substr($line, 0, $+[0]) = "";
479             $text =~ s/\s+/ /g;
480
481             $copying = 0; 
482             if ($issubmit) {
483   #            $submit{$text} = 1;
484                $cached{$_[0]}{submit}{$text} = 1;
485               $issubmit = 0;
486             }
487   #          $alllocales{$text} = 1;
488              $cached{$_[0]}{all}{$text} = 1;
489   #          $htmllocale{$text} = 1;
490              $cached{$_[0]}{html}{$text} = 1;
491             $text = "";
492
493           } else {
494             $text .= $line;
495             $line = "";
496           }
497         }
498       }
499     }
500
501     close(IN);
502     &converthtmlfile($_[0]);
503   }
504
505   # copy back into global arrays
506   map { $alllocales{$_} = 1 }  keys %{$cached{$_[0]}{all}};
507   map { $htmllocales{$_} = 1 } keys %{$cached{$_[0]}{html}};
508   map { $submit{$_} = 1 }      keys %{$cached{$_[0]}{submit}};
509 }
510
511 sub converthtmlfile {
512   local *IN;
513   local *OUT;
514
515   my $file = shift;
516
517   open(IN, $file) || die;
518
519   my $langcode = (split("/", getcwd()))[-1];
520   $file =~ s/_master.html$/_${langcode}.html/;
521
522   open(OUT, ">$file") || die;
523
524   my $copying = 0;
525   my $text = "";
526   while (my $line = <IN>) {
527     chomp($line);
528     if ("" eq $line) {
529       print(OUT "\n");
530       next;
531     }
532
533     while ("" ne $line) {
534       if (!$copying) {
535         if ($line =~ m|<translate>|i) {
536           print(OUT $`);
537           substr($line, 0, $+[0]) = "";
538           $copying = 1;
539           print(OUT "\n") if ("" eq $line);
540
541         } else {
542           print(OUT "${line}\n");
543           $line = "";
544         }
545
546       } else {
547         if ($line =~ m|</translate>|i) {
548           $text .= $`;
549           substr($line, 0, $+[0]) = "";
550           $text =~ s/\s+/ /g;
551           $copying = 0;
552           $alllocales{$text} = 1;
553           $htmllocale{$text} = 1;
554           print(OUT $self->{"texts"}{$text} || $text);
555           print(OUT "\n") if ("" eq $line);
556           $text = "";
557
558         } else {
559           $text .= $line;
560           $line = "";
561         }
562       }
563     }
564   }
565
566   close(IN);
567   close(OUT);
568 }