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 my $localefile = $dir eq $bindir ? $file : "dbupgrade";
109 open FH, ">$localefile" or die "$! : $localefile";
111 print FH q|$self{texts} = {
114 foreach $key (sort keys %locale) {
115 if ($self{texts}{$key}) {
116 $text = $self{texts}{$key};
121 $text =~ s/\\$/\\\\/;
124 $keytext =~ s/'/\\'/g;
125 $keytext =~ s/\\$/\\\\/;
127 print FH qq| '$keytext'|
128 . (' ' x (27 - length($keytext)))
129 . qq| => '$text',\n|;
137 foreach $key (sort keys %subrt) {
140 $text =~ s/\\$/\\\\/;
141 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '$text',\n|;
144 foreach $key (sort keys %submit) {
145 $text = ($self{texts}{$key}) ? $self{texts}{$key} : $key;
147 $text =~ s/\\$/\\\\/;
150 $english_sub =~ s/'/\\'/g;
151 $english_sub =~ s/\\$/\\\\/;
152 $english_sub = lc $key;
154 $translated_sub = lc $text;
155 $english_sub =~ s/( |-|,)/_/g;
156 $translated_sub =~ s/( |-|,)/_/g;
157 print FH qq| '$translated_sub'|
158 . (' ' x (27 - length($translated_sub)))
159 . qq| => '$english_sub',\n|;
171 #foreach $file (@htmltemplates) {
172 # converthtmlfile($file);
177 open FH, ">all" or die "$! : all";
179 print FH q|# These are all the texts to build the translations files.
180 # The file has the form of 'english text' => 'foreign text',
181 # you can add the translation in this file or in the 'missing' file
182 # run locales.pl from this directory to rebuild the translation files
187 foreach $key (sort keys %alllocales) {
188 $text = $self{texts}{$key};
193 $text =~ s/\\$/\\\\/;
202 print FH qq| '$key'| . (' ' x (27 - length($key))) . qq| => '$text',\n|;
214 open FH, ">missing" or die "$! : missing";
216 print FH q|# add the missing texts and run locales.pl to rebuild
221 foreach $text (@missing) {
222 print FH qq| '$text'| . (' ' x (27 - length($text))) . qq| => '',\n|;
234 open(FH, "LANGUAGE");
237 $trlanguage = $language[0];
240 $per = sprintf("%.1f", ($count - $notext) / $count * 100);
241 print "\n$trlanguage - ${per}%";
242 print " - $notext missing" if $notext;
249 sub extract_text_between_parenthesis {
250 my ($fh, $line) = @_;
251 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
254 if (length($line) <= $pos) {
256 return ($text, "") unless ($line);
260 my $cur_char = substr($line, $pos, 1);
262 if (!$inside_string) {
263 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
264 $inside_string = substr($line, $pos + 2, 1);
267 } elsif ((length($line) >= ($pos + 2)) &&
268 (substr($line, $pos, 1) eq "q")) {
269 $inside_string = substr($line, $pos + 1, 1);
272 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
273 $inside_string = $cur_char;
275 } elsif ($cur_char eq ")") {
276 return ($text, substr($line, $pos + 1));
284 } elsif ($cur_char eq '\\') {
288 } elsif ($cur_char eq $inside_string) {
289 undef($inside_string);
302 my $dont_include_subs = shift;
303 my $scanned_files = shift;
305 $scanned_files = {} unless ($scanned_files);
306 return if ($scanned_files->{$file});
307 $scanned_files->{$file} = 1;
309 if (!defined $cached{$file}) {
311 return unless (-f "$file");
313 my $fh = new FileHandle;
314 open $fh, "$file" or die "$! : $file";
316 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
321 # is this another file
322 if (/require\s+\W.*\.pl/) {
324 $newfile =~ s/require\s+\W//;
325 $newfile =~ s/\$form->{path}\///;
326 # &scanfile("$bindir/$newfile", 0, $scanned_files);
327 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
328 } elsif (/use\s+SL::(.*?);/) {
329 # &scanfile("../../SL/${1}.pm", 1, $scanned_files);
330 $cached{$file}{scannosubs}{"../../SL/${1}.pm"} = 1;
333 # is this a template call?
334 if (/parse_html_template\s*\(\s*[\"\']([\w\/]+)/) {
335 my $newfile = "$basedir/templates/webpages/$1_master.html";
337 # &scanhtmlfile($newfile);
338 # &converthtmlfile($newfile);
339 $cached{$file}{scanh}{$newfile} = 1;
340 print "." if $arg{v};
346 next if ($dont_include_subs);
347 ($null, $subrt) = split / +/;
348 # $subrt{$subrt} = 1;
349 $cached{$file}{subr}{$subrt} = 1;
358 my ($null, $country) = split /,/;
359 $country =~ s/^ +[\"\']//;
360 $country =~ s/[\"\'].*//;
366 # is it a submit button before $locale->
367 if (/$submitsearch/) {
369 if ($` !~ /locale->text/) {
371 $sub_line_no = $line_no;
375 my ($found) = /locale->text.*?\(/;
376 my $postmatch = "$'";
380 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
383 # if there is no $ in the string record it
384 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
386 # this guarantees one instance of string
387 # $locale{$string} = 1;
388 $cached{$file}{locale}{$string} = 1;
390 # this one is for all the locales
391 # $alllocales{$string} = 1;
392 $cached{$file}{all}{$string} = 1;
394 # is it a submit button before $locale->
396 # $submit{$string} = 1;
397 $cached{$file}{submit}{$string} = 1;
400 } elsif ($postmatch =~ />/) {
404 # exit loop if there are no more locales on this line
405 ($rc) = ($postmatch =~ /locale->text/);
407 if ( ($postmatch =~ />/)
408 || (!$found && ($sub_line_no != $line_no) && />/)) {
418 map { $alllocales{$_} = 1 } keys %{$cached{$file}{all}};
419 map { $locale{$_} = 1 } keys %{$cached{$file}{locale}};
420 map { $submit{$_} = 1 } keys %{$cached{$file}{submit}};
421 map { $subrt{$_} = 1 } keys %{$cached{$file}{subr}};
422 map { &scanfile($_, 0, $scanned_files) } keys %{$cached{$file}{scan}};
423 map { &scanfile($_, 1, $scanned_files) } keys %{$cached{$file}{scannosubs}};
424 map { &scanhtmlfile($_) } keys %{$cached{$file}{scanh}};
430 my $fh = new FileHandle;
431 open $fh, "$file" or die "$! : $file";
433 my @a = grep /^\[/, <$fh>;
437 grep { s/(\[|\])//g } @a;
439 foreach my $item (@a) {
440 @b = split /--/, $item;
441 foreach $string (@b) {
443 $locale{$string} = 1;
444 $alllocales{$string} = 1;
453 if (!defined $cached{$_[0]}) {
455 open(IN, $_[0]) || die $_[0];
460 while (my $line = <IN>) {
463 while ("" ne $line) {
465 if ($line =~ m|<translate>|i) {
467 if ($` =~ /$submitsearch/) {
470 substr($line, 0, $eom) = "";
477 if ($line =~ m|</translate>|i) {
479 substr($line, 0, $+[0]) = "";
484 # $submit{$text} = 1;
485 $cached{$_[0]}{submit}{$text} = 1;
488 # $alllocales{$text} = 1;
489 $cached{$_[0]}{all}{$text} = 1;
490 # $htmllocale{$text} = 1;
491 $cached{$_[0]}{html}{$text} = 1;
503 &converthtmlfile($_[0]);
506 # copy back into global arrays
507 map { $alllocales{$_} = 1 } keys %{$cached{$_[0]}{all}};
508 map { $htmllocales{$_} = 1 } keys %{$cached{$_[0]}{html}};
509 map { $submit{$_} = 1 } keys %{$cached{$_[0]}{submit}};
512 sub converthtmlfile {
518 open(IN, $file) || die;
520 my $langcode = (split("/", getcwd()))[-1];
521 $file =~ s/_master.html$/_${langcode}.html/;
523 open(OUT, ">$file") || die;
527 while (my $line = <IN>) {
534 while ("" ne $line) {
536 if ($line =~ m|<translate>|i) {
538 substr($line, 0, $+[0]) = "";
540 print(OUT "\n") if ("" eq $line);
543 print(OUT "${line}\n");
548 if ($line =~ m|</translate>|i) {
550 substr($line, 0, $+[0]) = "";
553 $alllocales{$text} = 1;
554 $htmllocale{$text} = 1;
555 print(OUT $self{"texts"}{$text} || $text);
556 print(OUT "\n") if ("" eq $line);