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 $menufile = "menu.ini";
19 $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
21 foreach $item (@ARGV) {
26 opendir DIR, "$bindir" or die "$!";
27 @progfiles = grep { /\.pl/; !/(_|^\.)/ } readdir DIR;
29 @customfiles = grep /_/, readdir DIR;
32 # put customized files into @customfiles
33 @customfiles = () if ($arg{n});
36 @menufiles = ($menufile);
38 opendir DIR, "$basedir" or die "$!";
39 @menufiles = grep { /.*?_$menufile$/ } readdir DIR;
41 unshift @menufiles, $menufile;
44 opendir DIR, $dbupdir or die "$!";
45 @dbplfiles = grep { /\.pl$/ } readdir DIR;
48 # slurp the translations in
53 # Read HTML templates.
55 #@htmltemplates = <../../templates/webpages/*/*_master.html>;
56 #foreach $file (@htmltemplates) {
57 # scanhtmlfile($file);
60 map({ handle_file($_, $bindir); } @progfiles);
61 map({ handle_file($_, $dbupdir); } @dbplfiles);
64 my ($file, $dir) = @_;
65 print "\n$file" if $arg{v};
70 &scanfile("$dir/$file");
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");
81 # if this is the menu.pl file
82 if ($file eq 'menu.pl') {
83 foreach $item (@menufiles) {
84 &scanmenu("$basedir/$item");
88 if ($file eq 'menunew.pl') {
89 foreach $item (@menufiles) {
90 &scanmenu("$basedir/$item");
97 eval { require 'missing'; };
100 foreach $text (keys %$missing) {
101 if ($locale{$text} || $htmllocale{$text}) {
102 unless ($self->{texts}{$text}) {
103 $self->{texts}{$text} = $missing->{$text};
108 open FH, ">$file" or die "$! : $file";
110 print FH q|$self->{texts} = {
113 foreach $key (sort keys %locale) {
114 if ($self->{texts}{$key}) {
115 $text = $self->{texts}{$key};
120 $text =~ s/\\$/\\\\/;
123 $keytext =~ s/'/\\'/g;
124 $keytext =~ s/\\$/\\\\/;
126 print FH qq| '$keytext'|
127 . (' ' x (27 - length($keytext)))
128 . qq| => '$text',\n|;
136 foreach $key (sort keys %subrt) {
139 $text =~ s/\\$/\\\\/;
140 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
143 foreach $key (sort keys %submit) {
144 $text = ($self->{texts}{$key}) ? $self->{texts}{$key} : $key;
146 $text =~ s/\\$/\\\\/;
149 $english_sub =~ s/'/\\'/g;
150 $english_sub =~ s/\\$/\\\\/;
151 $english_sub = lc $key;
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|;
170 #foreach $file (@htmltemplates) {
171 # converthtmlfile($file);
176 open FH, ">all" or die "$! : all";
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
186 foreach $key (sort keys %alllocales) {
187 $text = $self->{texts}{$key};
192 $text =~ s/\\$/\\\\/;
201 print FH qq| '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
213 open FH, ">missing" or die "$! : missing";
215 print FH q|# add the missing texts and run locales.pl to rebuild
220 foreach $text (@missing) {
221 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
233 open(FH, "LANGUAGE");
236 $trlanguage = $language[0];
239 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
240 print "\n$trlanguage - ${per}%";
241 print " - $notext missing" if $notext;
248 sub extract_text_between_parenthesis {
249 my ($fh, $line) = @_;
250 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
253 if (length($line) <= $pos) {
255 return ($text, "") unless ($line);
259 my $cur_char = substr($line, $pos, 1);
261 if (!$inside_string) {
262 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
263 $inside_string = substr($line, $pos + 2, 1);
266 } elsif ((length($line) >= ($pos + 2)) &&
267 (substr($line, $pos, 1) eq "q")) {
268 $inside_string = substr($line, $pos + 1, 1);
271 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
272 $inside_string = $cur_char;
274 } elsif ($cur_char eq ")") {
275 return ($text, substr($line, $pos + 1));
283 } elsif ($cur_char eq '\\') {
287 } elsif ($cur_char eq $inside_string) {
288 undef($inside_string);
301 my $dont_include_subs = shift;
302 my $scanned_files = shift;
304 $scanned_files = {} unless ($scanned_files);
305 return if ($scanned_files->{$file});
306 $scanned_files->{$file} = 1;
308 if (!defined $cached{$file}) {
310 return unless (-f "$file");
312 my $fh = new FileHandle;
313 open $fh, "$file" or die "$! : $file";
315 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
320 # is this another file
321 if (/require\s+\W.*\.pl/) {
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;
332 # is this a template call?
333 if (/parse_html_template\s*\(\s*[\"\']([\w\/]+)/) {
334 my $newfile = "$basedir/templates/webpages/$1_master.html";
336 # &scanhtmlfile($newfile);
337 # &converthtmlfile($newfile);
338 $cached{$file}{scanh}{$newfile} = 1;
339 print "." if $arg{v};
345 next if ($dont_include_subs);
346 ($null, $subrt) = split / +/;
347 # $subrt{$subrt} = 1;
348 $cached{$file}{subr}{$subrt} = 1;
357 my ($null, $country) = split /,/;
358 $country =~ s/^ +[\"\']//;
359 $country =~ s/[\"\'].*//;
365 # is it a submit button before $locale->
366 if (/$submitsearch/) {
368 if ($` !~ /locale->text/) {
370 $sub_line_no = $line_no;
374 my ($found) = /locale->text.*?\(/;
375 my $postmatch = "$'";
379 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
382 # if there is no $ in the string record it
383 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
385 # this guarantees one instance of string
386 # $locale{$string} = 1;
387 $cached{$file}{locale}{$string} = 1;
389 # this one is for all the locales
390 # $alllocales{$string} = 1;
391 $cached{$file}{all}{$string} = 1;
393 # is it a submit button before $locale->
395 # $submit{$string} = 1;
396 $cached{$file}{submit}{$string} = 1;
399 } elsif ($postmatch =~ />/) {
403 # exit loop if there are no more locales on this line
404 ($rc) = ($postmatch =~ /locale->text/);
406 if ( ($postmatch =~ />/)
407 || (!$found && ($sub_line_no != $line_no) && />/)) {
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}};
429 my $fh = new FileHandle;
430 open $fh, "$file" or die "$! : $file";
432 my @a = grep /^\[/, <$fh>;
436 grep { s/(\[|\])//g } @a;
438 foreach my $item (@a) {
439 @b = split /--/, $item;
440 foreach $string (@b) {
442 $locale{$string} = 1;
443 $alllocales{$string} = 1;
452 if (!defined $cached{$_[0]}) {
454 open(IN, $_[0]) || die $_[0];
459 while (my $line = <IN>) {
462 while ("" ne $line) {
464 if ($line =~ m|<translate>|i) {
466 if ($` =~ /$submitsearch/) {
469 substr($line, 0, $eom) = "";
476 if ($line =~ m|</translate>|i) {
478 substr($line, 0, $+[0]) = "";
483 # $submit{$text} = 1;
484 $cached{$_[0]}{submit}{$text} = 1;
487 # $alllocales{$text} = 1;
488 $cached{$_[0]}{all}{$text} = 1;
489 # $htmllocale{$text} = 1;
490 $cached{$_[0]}{html}{$text} = 1;
502 &converthtmlfile($_[0]);
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}};
511 sub converthtmlfile {
517 open(IN, $file) || die;
519 my $langcode = (split("/", getcwd()))[-1];
520 $file =~ s/_master.html$/_${langcode}.html/;
522 open(OUT, ">$file") || die;
526 while (my $line = <IN>) {
533 while ("" ne $line) {
535 if ($line =~ m|<translate>|i) {
537 substr($line, 0, $+[0]) = "";
539 print(OUT "\n") if ("" eq $line);
542 print(OUT "${line}\n");
547 if ($line =~ m|</translate>|i) {
549 substr($line, 0, $+[0]) = "";
552 $alllocales{$text} = 1;
553 $htmllocale{$text} = 1;
554 print(OUT $self->{"texts"}{$text} || $text);
555 print(OUT "\n") if ("" eq $line);