bcf8f0a1855592c8932f34842d6c4c89831ba173
[kivitendo-erp.git] / locale / de / locales.pl
1 #!/usr/bin/perl
2
3 # -n do not include custom_ scripts
4
5 use FileHandle;
6
7 $basedir  = "../..";
8 $bindir   = "$basedir/bin/mozilla";
9 $menufile = "menu.ini";
10
11 foreach $item (@ARGV) {
12   $item =~ s/-//g;
13   $arg{$item} = 1;
14 }
15
16 opendir DIR, "$bindir" or die "$!";
17 @progfiles = grep { /\.pl/; !/(_|^\.)/ } readdir DIR;
18 seekdir DIR, 0;
19 @customfiles = grep /_/, readdir DIR;
20 closedir DIR;
21
22 # put customized files into @customfiles
23 @customfiles = () if ($arg{n});
24
25 if ($arg{n}) {
26   @menufiles = ($menufile);
27 } else {
28   opendir DIR, "$basedir" or die "$!";
29   @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
30   closedir DIR;
31   unshift @menufiles, $menufile;
32 }
33
34 # slurp the translations in
35 if (-f 'all') {
36   require "all";
37 }
38
39 foreach $file (@progfiles) {
40
41   %locale = ();
42   %submit = ();
43   %subrt  = ();
44
45   &scanfile("$bindir/$file");
46
47   # scan custom_{module}.pl or {login}_{module}.pl files
48   foreach $customfile (@customfiles) {
49     if ($customfile =~ /_$file/) {
50       if (-f "$bindir/$customfile") {
51         &scanfile("$bindir/$customfile");
52       }
53     }
54   }
55
56   # if this is the menu.pl file
57   if ($file eq 'menu.pl') {
58     foreach $item (@menufiles) {
59       &scanmenu("$basedir/$item");
60     }
61   }
62
63   if ($file eq 'menunew.pl') {
64     foreach $item (@menufiles) {
65       &scanmenu("$basedir/$item");
66     }
67   }
68
69   $file =~ s/\.pl//;
70
71   eval { require 'missing'; };
72   unlink 'missing';
73
74   foreach $text (keys %$missing) {
75     if ($locale{$text}) {
76       unless ($self{texts}{$text}) {
77         $self{texts}{$text} = $missing->{$text};
78       }
79     }
80   }
81
82   open FH, ">$file" or die "$! : $file";
83
84   print FH q|$self{texts} = {
85 |;
86
87   foreach $key (sort keys %locale) {
88     if ($self{texts}{$key}) {
89       $text = $self{texts}{$key};
90     } else {
91       $text = $key;
92     }
93     $text =~ s/'/\\'/g;
94     $text =~ s/\\$/\\\\/;
95
96     $keytext = $key;
97     $keytext =~ s/'/\\'/g;
98     $keytext =~ s/\\$/\\\\/;
99
100     print FH qq|  '$keytext'|
101       . (' ' x (27 - length($keytext)))
102       . qq| => '$text',\n|;
103   }
104
105   print FH q|};
106
107 $self{subs} = {
108 |;
109
110   foreach $key (sort keys %subrt) {
111     $text = $key;
112     $text =~ s/'/\\'/g;
113     $text =~ s/\\$/\\\\/;
114     print FH qq|  '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
115   }
116
117   foreach $key (sort keys %submit) {
118     $text = ($self{texts}{$key}) ? $self{texts}{$key} : $key;
119     $text =~ s/'/\\'/g;
120     $text =~ s/\\$/\\\\/;
121
122     $english_sub = $key;
123     $english_sub =~ s/'/\\'/g;
124     $english_sub =~ s/\\$/\\\\/;
125     $english_sub = lc $key;
126
127     $translated_sub = lc $text;
128     $english_sub    =~ s/( |-|,)/_/g;
129     $translated_sub =~ s/( |-|,)/_/g;
130     print FH qq|  '$translated_sub'|
131       . (' ' x (27 - length($translated_sub)))
132       . qq| => '$english_sub',\n|;
133   }
134
135   print FH q|};
136
137 1;
138 |;
139
140   close FH;
141 }
142
143 # now print out all
144
145 open FH, ">all" or die "$! : all";
146
147 print FH q|# These are all the texts to build the translations files.
148 # The file has the form of 'english text'  => 'foreign text',
149 # you can add the translation in this file or in the 'missing' file
150 # run locales.pl from this directory to rebuild the translation files
151
152 $self{texts} = {
153 |;
154
155 foreach $key (sort keys %alllocales) {
156   $text = $self{texts}{$key};
157
158   $count++;
159
160   $text =~ s/'/\\'/g;
161   $text =~ s/\\$/\\\\/;
162   $key  =~ s/'/\\'/g;
163   $key  =~ s/\\$/\\\\/;
164
165   unless ($text) {
166     $notext++;
167     push @missing, $key;
168   }
169
170   print FH qq|  '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
171
172 }
173
174 print FH q|};
175
176 1;
177 |;
178
179 close FH;
180
181 if (@missing) {
182   open FH, ">missing" or die "$! : missing";
183
184   print FH q|# add the missing texts and run locales.pl to rebuild
185
186 $missing = {
187 |;
188
189   foreach $text (@missing) {
190     print FH qq|  '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
191   }
192
193   print FH q|};
194
195 1;
196 |;
197
198   close FH;
199
200 }
201
202 open(FH, "LANGUAGE");
203 @language = <FH>;
204 close(FH);
205 $trlanguage = $language[0];
206 chomp $trlanguage;
207
208 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
209 print "\n$trlanguage - ${per}%\n";
210
211 exit;
212
213 # eom
214
215 sub scanfile {
216   my $file = shift;
217
218   return unless (-f "$file");
219
220   my $fh = new FileHandle;
221   open $fh, "$file" or die "$! : $file";
222
223   my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
224
225   while (<$fh>) {
226     $line_no++;
227
228     # is this another file
229     if (/require\s+\W.*\.pl/) {
230       my $newfile = $&;
231       $newfile =~ s/require\s+\W//;
232       $newfile =~ s/\$form->{path}\///;
233       &scanfile("$bindir/$newfile");
234     }
235
236     # is this a sub ?
237     if (/^sub /) {
238       ($null, $subrt) = split / +/;
239       $subrt{$subrt} = 1;
240       next;
241     }
242
243     my $rc = 1;
244
245     while ($rc) {
246       if (/Locale/) {
247         unless (/^use /) {
248           my ($null, $country) = split /,/;
249           $country =~ s/^ +[\"\']//;
250           $country =~ s/[\"\'].*//;
251         }
252       }
253
254       my $postmatch = "";
255
256       # is it a submit button before $locale->
257       if (/type\s*=\s*submit/i) {
258         $postmatch = $';
259         if ($` !~ /\$locale->text/) {
260           $is_submit   = 1;
261           $sub_line_no = $line_no;
262         }
263       }
264
265       my ($found) = /\$locale->text.*?\W\)/;
266       my $postmatch = $';
267
268       if ($found) {
269         my $string = $&;
270         $string =~ s/\$locale->text\(\s*[\'\"(q|qq)][\'\/\\\|~]*//;
271         $string =~ s/\W\)+.*$//;
272
273         # if there is no $ in the string record it
274         unless ($string =~ /\$\D.*/) {
275
276           # this guarantees one instance of string
277           $locale{$string} = 1;
278
279           # this one is for all the locales
280           $alllocales{$string} = 1;
281
282           # is it a submit button before $locale->
283           if ($is_submit) {
284             $submit{$string} = 1;
285           }
286         }
287       } elsif ($postmatch =~ />/) {
288         $is_submit = 0;
289       }
290
291       # exit loop if there are no more locales on this line
292       ($rc) = ($postmatch =~ /\$locale->text/);
293
294       # strip text
295       s/^.*?\$locale->text.*?\)//;
296
297       if (   ($postmatch =~ />/)
298           || (!$found && ($sub_line_no != $line_no) && />/)) {
299         $is_submit = 0;
300       }
301     }
302   }
303
304   close($fh);
305
306 }
307
308 sub scanmenu {
309   my $file = shift;
310
311   my $fh = new FileHandle;
312   open $fh, "$file" or die "$! : $file";
313
314   my @a = grep /^\[/, <$fh>;
315   close($fh);
316
317   # strip []
318   grep { s/(\[|\])//g } @a;
319
320   foreach my $item (@a) {
321     @b = split /--/, $item;
322     foreach $string (@b) {
323       chomp $string;
324       $locale{$string}     = 1;
325       $alllocales{$string} = 1;
326     }
327   }
328
329 }
330