3 # -n do not include custom_ scripts
4 # -v verbose mode, shows progress stuff
6 # this version of locles processes not only all required .pl files
7 # but also all parse_html_templated files.
16 $bindir = "$basedir/bin/mozilla";
17 $menufile = "menu.ini";
18 $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
20 foreach $item (@ARGV) {
25 opendir DIR, "$bindir" or die "$!";
26 @progfiles = grep { /\.pl/; !/(_|^\.)/ } readdir DIR;
28 @customfiles = grep /_/, readdir DIR;
31 # put customized files into @customfiles
32 @customfiles = () if ($arg{n});
35 @menufiles = ($menufile);
37 opendir DIR, "$basedir" or die "$!";
38 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
40 unshift @menufiles, $menufile;
43 # slurp the translations in
48 # Read HTML templates.
50 #@htmltemplates = <../../templates/webpages/*/*_master.html>;
51 #foreach $file (@htmltemplates) {
52 # scanhtmlfile($file);
55 foreach $file (@progfiles) {
57 print "\n$file" if $arg{v};
62 &scanfile("$bindir/$file");
64 # scan custom_{module}.pl or {login}_{module}.pl files
65 foreach $customfile (@customfiles) {
66 if ($customfile =~ /_$file/) {
67 if (-f "$bindir/$customfile") {
68 &scanfile("$bindir/$customfile");
73 # if this is the menu.pl file
74 if ($file eq 'menu.pl') {
75 foreach $item (@menufiles) {
76 &scanmenu("$basedir/$item");
80 if ($file eq 'menunew.pl') {
81 foreach $item (@menufiles) {
82 &scanmenu("$basedir/$item");
89 eval { require 'missing'; };
92 foreach $text (keys %$missing) {
93 if ($locale{$text} || $htmllocale{$text}) {
94 unless ($self{texts}{$text}) {
95 $self{texts}{$text} = $missing->{$text};
100 open FH, ">$file" or die "$! : $file";
102 print FH q|$self{texts} = {
105 foreach $key (sort keys %locale) {
106 if ($self{texts}{$key}) {
107 $text = $self{texts}{$key};
112 $text =~ s/\\$/\\\\/;
115 $keytext =~ s/'/\\'/g;
116 $keytext =~ s/\\$/\\\\/;
118 print FH qq| '$keytext'|
119 . (' ' x (27 - length($keytext)))
120 . qq| => '$text',\n|;
128 foreach $key (sort keys %subrt) {
131 $text =~ s/\\$/\\\\/;
132 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
135 foreach $key (sort keys %submit) {
136 $text = ($self{texts}{$key}) ? $self{texts}{$key} : $key;
138 $text =~ s/\\$/\\\\/;
141 $english_sub =~ s/'/\\'/g;
142 $english_sub =~ s/\\$/\\\\/;
143 $english_sub = lc $key;
145 $translated_sub = lc $text;
146 $english_sub =~ s/( |-|,)/_/g;
147 $translated_sub =~ s/( |-|,)/_/g;
148 print FH qq| '$translated_sub'|
149 . (' ' x (27 - length($translated_sub)))
150 . qq| => '$english_sub',\n|;
162 #foreach $file (@htmltemplates) {
163 # converthtmlfile($file);
168 open FH, ">all" or die "$! : all";
170 print FH q|# These are all the texts to build the translations files.
171 # The file has the form of 'english text' => 'foreign text',
172 # you can add the translation in this file or in the 'missing' file
173 # run locales.pl from this directory to rebuild the translation files
178 foreach $key (sort keys %alllocales) {
179 $text = $self{texts}{$key};
184 $text =~ s/\\$/\\\\/;
193 print FH qq| '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
205 open FH, ">missing" or die "$! : missing";
207 print FH q|# add the missing texts and run locales.pl to rebuild
212 foreach $text (@missing) {
213 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
225 open(FH, "LANGUAGE");
228 $trlanguage = $language[0];
231 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
232 print "\n$trlanguage - ${per}%";
233 print " - $notext missing" if $notext;
240 sub extract_text_between_parenthesis {
241 my ($fh, $line) = @_;
242 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
245 if (length($line) <= $pos) {
247 return ($text, "") unless ($line);
251 my $cur_char = substr($line, $pos, 1);
253 if (!$inside_string) {
254 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
255 $inside_string = substr($line, $pos + 2, 1);
258 } elsif ((length($line) >= ($pos + 2)) &&
259 (substr($line, $pos, 1) eq "q")) {
260 $inside_string = substr($line, $pos + 1, 1);
263 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
264 $inside_string = $cur_char;
266 } elsif ($cur_char eq ")") {
267 return ($text, substr($line, $pos + 1));
275 } elsif ($cur_char eq '\\') {
279 } elsif ($cur_char eq $inside_string) {
280 undef($inside_string);
293 my $dont_include_subs = shift;
294 my $scanned_files = shift;
296 $scanned_files = {} unless ($scanned_files);
297 return if ($scanned_files->{$file});
298 $scanned_files->{$file} = 1;
300 if (!defined $cached{$file}) {
302 return unless (-f "$file");
304 my $fh = new FileHandle;
305 open $fh, "$file" or die "$! : $file";
307 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
312 # is this another file
313 if (/require\s+\W.*\.pl/) {
315 $newfile =~ s/require\s+\W//;
316 $newfile =~ s/\$form->{path}\///;
317 # &scanfile("$bindir/$newfile", 0, $scanned_files);
318 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
319 } elsif (/use\s+SL::(.*?);/) {
320 # &scanfile("../../SL/${1}.pm", 1, $scanned_files);
321 $cached{$file}{scannosubs}{"../../SL/${1}.pm"} = 1;
324 # is this a template call?
325 if (/parse_html_template\s*\(\s*[\"\']([\w\/]+)/) {
326 my $newfile = "$basedir/templates/webpages/$1_master.html";
328 # &scanhtmlfile($newfile);
329 # &converthtmlfile($newfile);
330 $cached{$file}{scanh}{$newfile} = 1;
331 print "." if $arg{v};
337 next if ($dont_include_subs);
338 ($null, $subrt) = split / +/;
339 # $subrt{$subrt} = 1;
340 $cached{$file}{subr}{$subrt} = 1;
349 my ($null, $country) = split /,/;
350 $country =~ s/^ +[\"\']//;
351 $country =~ s/[\"\'].*//;
357 # is it a submit button before $locale->
358 if (/$submitsearch/) {
360 if ($` !~ /locale->text/) {
362 $sub_line_no = $line_no;
366 my ($found) = /locale->text.*?\(/;
367 my $postmatch = "$'";
371 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
374 # if there is no $ in the string record it
375 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
377 # this guarantees one instance of string
378 # $locale{$string} = 1;
379 $cached{$file}{locale}{$string} = 1;
381 # this one is for all the locales
382 # $alllocales{$string} = 1;
383 $cached{$file}{all}{$string} = 1;
385 # is it a submit button before $locale->
387 # $submit{$string} = 1;
388 $cached{$file}{submit}{$string} = 1;
391 } elsif ($postmatch =~ />/) {
395 # exit loop if there are no more locales on this line
396 ($rc) = ($postmatch =~ /locale->text/);
398 if ( ($postmatch =~ />/)
399 || (!$found && ($sub_line_no != $line_no) && />/)) {
409 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
410 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
411 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
412 map { $subrt{$_} = 1 } keys %{$cached{$file}{subr}};
413 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
414 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
415 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
421 my $fh = new FileHandle;
422 open $fh, "$file" or die "$! : $file";
424 my @a = grep /^\[/, <$fh>;
428 grep { s/(\[|\])//g } @a;
430 foreach my $item (@a) {
431 @b = split /--/, $item;
432 foreach $string (@b) {
434 $locale{$string} = 1;
435 $alllocales{$string} = 1;
444 if (!defined $cached{$_[0]}) {
446 open(IN, $_[0]) || die $_[0];
451 while (my $line = <IN>) {
454 while ("" ne $line) {
456 if ($line =~ m|<translate>|i) {
458 if ($` =~ /$submitsearch/) {
461 substr($line, 0, $eom) = "";
468 if ($line =~ m|</translate>|i) {
470 substr($line, 0, $+[0]) = "";
474 # $submit{$text} = 1;
475 $cached{$_[0]}{submit}{$text} = 1;
478 # $alllocales{$text} = 1;
479 $cached{$_[0]}{all}{$text} = 1;
480 # $htmllocale{$text} = 1;
481 $cached{$_[0]}{html}{$text} = 1;
493 &converthtmlfile($_[0]);
496 # copy back into global arrays
497 map { $alllocales{$_} = 1 } keys %{$cached{$_[0]}{all}};
498 map { $htmllocales{$_} = 1 } keys %{$cached{$_[0]}{html}};
499 map { $submit{$_} = 1 } keys %{$cached{$_[0]}{submit}};
502 sub converthtmlfile {
508 open(IN, $file) || die;
510 my $langcode = (split("/", getcwd()))[-1];
511 $file =~ s/_master.html$/_${langcode}.html/;
513 open(OUT, ">$file") || die;
517 while (my $line = <IN>) {
524 while ("" ne $line) {
526 if ($line =~ m|<translate>|i) {
528 substr($line, 0, $+[0]) = "";
530 print(OUT "\n") if ("" eq $line);
533 print(OUT "${line}\n");
538 if ($line =~ m|</translate>|i) {
540 substr($line, 0, $+[0]) = "";
542 $alllocales{$text} = 1;
543 $htmllocale{$text} = 1;
544 print(OUT $self{"texts"}{$text} || $text);
545 print(OUT "\n") if ("" eq $line);