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 $dbupdir = "$basedir/sql/Pg-upgrade";
18 $dbupdir2 = "$basedir/sql/Pg-upgrade2";
19 $menufile = "menu.ini";
20 $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
22 %referenced_html_files = ();
27 # -h extended checks on HTML templates
29 foreach $item (@ARGV) {
34 opendir DIR, "$bindir" or die "$!";
35 @progfiles = grep { /\.pl$/ && !/(_|^\.)/ } readdir DIR;
37 @customfiles = grep /_/, readdir DIR;
40 # put customized files into @customfiles
41 @customfiles = () if ($arg{n});
44 @menufiles = ($menufile);
46 opendir DIR, "$basedir" or die "$!";
47 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
49 unshift @menufiles, $menufile;
52 opendir DIR, $dbupdir or die "$!";
53 @dbplfiles = grep { /\.pl$/ } readdir DIR;
56 opendir DIR, $dbupdir2 or die "$!";
57 @dbplfiles2 = grep { /\.pl$/ } readdir DIR;
60 # slurp the translations in
65 # Read HTML templates.
67 #@htmltemplates = <../../templates/webpages/*/*_master.html>;
68 #foreach $file (@htmltemplates) {
69 # scanhtmlfile($file);
72 map({ handle_file($_, $bindir); } @progfiles);
73 map({ handle_file($_, $dbupdir); } @dbplfiles);
74 map({ handle_file($_, $dbupdir2); } @dbplfiles2);
77 my ($file, $dir) = @_;
78 print "\n$file" if $arg{v};
83 &scanfile("$dir/$file");
85 # scan custom_{module}.pl or {login}_{module}.pl files
86 foreach $customfile (@customfiles) {
87 if ($customfile =~ /_$file/) {
88 if (-f "$dir/$customfile") {
89 &scanfile("$dir/$customfile");
94 # if this is the menu.pl file
95 if ($file eq 'menu.pl') {
96 foreach $item (@menufiles) {
97 &scanmenu("$basedir/$item");
101 if ($file eq 'menunew.pl') {
102 foreach $item (@menufiles) {
103 &scanmenu("$basedir/$item");
104 print "." if $arg{v};
110 eval { require 'missing'; };
113 foreach $text (keys %$missing) {
114 if ($locale{$text} || $htmllocale{$text}) {
115 unless ($self->{texts}{$text}) {
116 $self->{texts}{$text} = $missing->{$text};
121 open FH, ">$file" or die "$! : $file";
123 print FH q|$self->{texts} = {
126 foreach $key (sort keys %locale) {
127 if ($self->{texts}{$key}) {
128 $text = $self->{texts}{$key};
133 $text =~ s/\\$/\\\\/;
136 $keytext =~ s/'/\\'/g;
137 $keytext =~ s/\\$/\\\\/;
139 print FH qq| '$keytext'|
140 . (' ' x (27 - length($keytext)))
141 . qq| => '$text',\n|;
149 foreach $key (sort keys %subrt) {
152 $text =~ s/\\$/\\\\/;
153 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
156 foreach $key (sort keys %submit) {
157 $text = ($self->{texts}{$key}) ? $self->{texts}{$key} : $key;
159 $text =~ s/\\$/\\\\/;
162 $english_sub =~ s/'/\\'/g;
163 $english_sub =~ s/\\$/\\\\/;
164 $english_sub = lc $key;
166 $translated_sub = lc $text;
167 $english_sub =~ s/( |-|,)/_/g;
168 $translated_sub =~ s/( |-|,)/_/g;
169 print FH qq| '$translated_sub'|
170 . (' ' x (27 - length($translated_sub)))
171 . qq| => '$english_sub',\n|;
183 #foreach $file (@htmltemplates) {
184 # converthtmlfile($file);
189 open FH, ">all" or die "$! : all";
191 print FH q|# These are all the texts to build the translations files.
192 # The file has the form of 'english text' => 'foreign text',
193 # you can add the translation in this file or in the 'missing' file
194 # run locales.pl from this directory to rebuild the translation files
199 foreach $key (sort keys %alllocales) {
200 $text = $self->{texts}{$key};
205 $text =~ s/\\$/\\\\/;
214 print FH qq| '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
226 open FH, ">missing" or die "$! : missing";
228 print FH q|# add the missing texts and run locales.pl to rebuild
233 foreach $text (@missing) {
234 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
246 open(FH, "LANGUAGE");
249 $trlanguage = $language[0];
253 search_unused_htmlfiles();
254 search_translated_htmlfiles_wo_master();
257 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
258 print "\n$trlanguage - ${per}%";
259 print " - $notext missing" if $notext;
266 sub extract_text_between_parenthesis {
267 my ($fh, $line) = @_;
268 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
271 if (length($line) <= $pos) {
273 return ($text, "") unless ($line);
277 my $cur_char = substr($line, $pos, 1);
279 if (!$inside_string) {
280 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
281 $inside_string = substr($line, $pos + 2, 1);
284 } elsif ((length($line) >= ($pos + 2)) &&
285 (substr($line, $pos, 1) eq "q")) {
286 $inside_string = substr($line, $pos + 1, 1);
289 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
290 $inside_string = $cur_char;
292 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
293 return ($text, substr($line, $pos + 1));
301 } elsif ($cur_char eq '\\') {
305 } elsif ($cur_char eq $inside_string) {
306 undef($inside_string);
319 my $dont_include_subs = shift;
320 my $scanned_files = shift;
322 $scanned_files = {} unless ($scanned_files);
323 return if ($scanned_files->{$file});
324 $scanned_files->{$file} = 1;
326 if (!defined $cached{$file}) {
328 return unless (-f "$file");
330 my $fh = new FileHandle;
331 open $fh, "$file" or die "$! : $file";
333 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
338 # is this another file
339 if (/require\s+\W.*\.pl/) {
341 $newfile =~ s/require\s+\W//;
342 $newfile =~ s|bin/mozilla||;
343 # &scanfile("$bindir/$newfile", 0, $scanned_files);
344 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
345 } elsif (/use\s+SL::(.*?);/) {
348 # &scanfile("../../SL/${1}.pm", 1, $scanned_files);
349 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
352 # is this a template call?
353 if (/parse_html_template\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
354 my $newfile = "$basedir/templates/webpages/$1_master.html";
356 # &scanhtmlfile($newfile);
357 # &converthtmlfile($newfile);
358 $cached{$file}{scanh}{$newfile} = 1;
359 print "." if $arg{v};
361 print "W: missing HTML template: " . strip_base($newfile) . " (referenced from " . strip_base($file) . ")\n";
367 next if ($dont_include_subs);
368 ($null, $subrt) = split / +/;
369 # $subrt{$subrt} = 1;
370 $cached{$file}{subr}{$subrt} = 1;
379 my ($null, $country) = split /,/;
380 $country =~ s/^ +[\"\']//;
381 $country =~ s/[\"\'].*//;
387 # is it a submit button before $locale->
388 if (/$submitsearch/) {
390 if ($` !~ /locale->text/) {
392 $sub_line_no = $line_no;
396 my ($found) = /locale->text.*?\(/;
397 my $postmatch = "$'";
401 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
404 # if there is no $ in the string record it
405 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
407 # this guarantees one instance of string
408 # $locale{$string} = 1;
409 $cached{$file}{locale}{$string} = 1;
411 # this one is for all the locales
412 # $alllocales{$string} = 1;
413 $cached{$file}{all}{$string} = 1;
415 # is it a submit button before $locale->
417 # $submit{$string} = 1;
418 $cached{$file}{submit}{$string} = 1;
421 } elsif ($postmatch =~ />/) {
425 # exit loop if there are no more locales on this line
426 ($rc) = ($postmatch =~ /locale->text/);
428 if ( ($postmatch =~ />/)
429 || (!$found && ($sub_line_no != $line_no) && />/)) {
439 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
440 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
441 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
442 map { $subrt{$_} = 1 } keys %{$cached{$file}{subr}};
443 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
444 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
445 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
447 @referenced_html_files{keys %{$cached{$file}{scanh}}} = (1) x scalar keys %{$cached{$file}{scanh}};
453 my $fh = new FileHandle;
454 open $fh, "$file" or die "$! : $file";
456 my @a = grep m/^\[/, <$fh>;
460 grep { s/(\[|\])//g } @a;
462 foreach my $item (@a) {
463 @b = split /--/, $item;
464 foreach $string (@b) {
466 $locale{$string} = 1;
467 $alllocales{$string} = 1;
476 if (!defined $cached{$_[0]}) {
478 open(IN, $_[0]) || die $_[0];
483 while (my $line = <IN>) {
486 while ("" ne $line) {
488 if ($line =~ m|<translate>|i) {
490 if ($` =~ /$submitsearch/) {
493 substr($line, 0, $eom) = "";
500 if ($line =~ m|</translate>|i) {
502 substr($line, 0, $+[0]) = "";
507 # $submit{$text} = 1;
508 $cached{$_[0]}{submit}{$text} = 1;
511 # $alllocales{$text} = 1;
512 $cached{$_[0]}{all}{$text} = 1;
513 # $htmllocale{$text} = 1;
514 $cached{$_[0]}{html}{$text} = 1;
526 &converthtmlfile($_[0]);
529 # copy back into global arrays
530 map { $alllocales{$_} = 1 } keys %{$cached{$_[0]}{all}};
531 map { $htmllocales{$_} = 1 } keys %{$cached{$_[0]}{html}};
532 map { $submit{$_} = 1 } keys %{$cached{$_[0]}{submit}};
535 sub converthtmlfile {
541 open(IN, $file) || die;
543 my $langcode = (split("/", getcwd()))[-1];
544 $file =~ s/_master.html$/_${langcode}.html/;
546 open(OUT, ">$file") || die;
550 while (my $line = <IN>) {
557 while ("" ne $line) {
559 if ($line =~ m|<translate>|i) {
561 substr($line, 0, $+[0]) = "";
563 print(OUT "\n") if ("" eq $line);
566 print(OUT "${line}\n");
571 if ($line =~ m|</translate>|i) {
573 substr($line, 0, $+[0]) = "";
576 $alllocales{$text} = 1;
577 $htmllocale{$text} = 1;
578 print(OUT $self->{"texts"}{$text} || $text);
579 print(OUT "\n") if ("" eq $line);
594 sub search_unused_htmlfiles {
595 my @unscanned_dirs = ('../../templates/webpages');
597 while (scalar @unscanned_dirs) {
598 my $dir = shift @unscanned_dirs;
600 foreach my $entry (<$dir/*>) {
602 push @unscanned_dirs, $entry;
604 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
605 print "W: unused HTML template: " . strip_base($entry) . "\n";
612 sub search_translated_htmlfiles_wo_master {
613 my @unscanned_dirs = ('../../templates/webpages');
615 while (scalar @unscanned_dirs) {
616 my $dir = shift @unscanned_dirs;
618 foreach my $entry (<$dir/*>) {
620 push @unscanned_dirs, $entry;
622 } elsif (($entry =~ /_[a-z]+\.html$/) && ($entry !~ /_master.html$/) && -f $entry) {
624 $master =~ s/[a-z]+\.html$/master.html/;
626 print "W: translated HTML template without master: " . strip_base($entry) . "\n";
634 $_[0] =~ s|^../../||;
635 $_[0] =~ s|templates/webpages/||;