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.
15 use File::Slurp qw(slurp);
19 use List::Util qw(first);
23 $OUTPUT_AUTOFLUSH = 1;
32 my $basedir = "../..";
33 my $locales_dir = ".";
34 my $bindir = "$basedir/bin/mozilla";
35 my @progdirs = ( "$basedir/SL" );
36 my $dbupdir = "$basedir/sql/Pg-upgrade";
37 my $dbupdir2 = "$basedir/sql/Pg-upgrade2";
38 my $menufile = "menu.ini";
39 my @javascript_dirs = ($basedir .'/js', $basedir .'/templates/webpages');
40 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
45 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit, %jslocale);
46 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
51 my ($top_dir_name) = @_;
58 tie my %dir_h, 'IO::Dir', $dir_name;
60 push @files, grep { -f } map { "${dir_name}/${_}" } keys %dir_h;
61 my @sub_dirs = grep { -d } map { "${dir_name}/${_}" } grep { ! m/^\.\.?$/ } keys %dir_h;
63 $finder->($_) for @sub_dirs;
66 $finder->($top_dir_name);
72 # overwrite existing entries with the ones from 'missing'
73 $self->{texts}->{$_} = $missing->{$_} for grep { $missing->{$_} } keys %alllocales;
75 # try to set missing entries from lost ones
76 my %lost_by_text = map { ($_->{text} => $_->{translation}) } @lost;
77 $self->{texts}->{$_} = $lost_by_text{$_} for grep { !$self->{texts}{$_} } keys %alllocales;
80 my @bindir_files = find_files($bindir);
81 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
82 my @customfiles = grep /_custom/, @bindir_files;
84 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
86 # put customized files into @customfiles
87 my (@menufiles, %dir_h);
91 @menufiles = ($menufile);
93 tie %dir_h, 'IO::Dir', $basedir;
94 @menufiles = map { "$basedir/$_" } grep { /.*?_$menufile$/ } keys %dir_h;
95 unshift @menufiles, "$basedir/$menufile";
98 tie %dir_h, 'IO::Dir', $dbupdir;
99 my @dbplfiles = grep { /\.pl$/ } keys %dir_h;
101 tie %dir_h, 'IO::Dir', $dbupdir2;
102 my @dbplfiles2 = grep { /\.pl$/ } keys %dir_h;
104 # slurp the translations in
105 if (-f "$locales_dir/all") {
106 require "$locales_dir/all";
108 if (-f "$locales_dir/missing") {
109 require "$locales_dir/missing" ;
110 unlink "$locales_dir/missing";
112 if (-f "$locales_dir/lost") {
113 require "$locales_dir/lost";
114 unlink "$locales_dir/lost";
117 my $charset = slurp("$locales_dir/charset") || 'utf-8';
120 my %old_texts = %{ $self->{texts} || {} };
122 handle_file(@{ $_ }) for @progfiles;
123 handle_file($_, $dbupdir) for @dbplfiles;
124 handle_file($_, $dbupdir2) for @dbplfiles2;
125 scanmenu($_) for @menufiles;
127 for my $file_name (map({find_files($_)} @javascript_dirs)) {
128 scan_javascript_file($file_name);
131 # merge entries to translate with entries from files 'missing' and 'lost'
136 file => "$locales_dir/all",
137 header => $ALL_HEADER,
138 data_name => '$self->{texts}',
139 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
142 open(my $js_file, '>:encoding(utf8)', $locales_dir .'/js.js') || die;
143 print $js_file '{'."\n";
145 for my $key (sort(keys(%jslocale))) {
146 print $js_file (!$first_entry ? ',' : '') . _double_quote($key) .':'. _double_quote($self->{texts}{$key}) ."\n";
149 print $js_file '}'."\n";
152 foreach my $text (keys %$missing) {
153 if ($locale{$text} || $htmllocale{$text}) {
154 unless ($self->{texts}{$text}) {
155 $self->{texts}{$text} = $missing->{$text};
161 # calc and generate missing
162 my @new_missing = grep { !$self->{texts}{$_} } sort keys %alllocales;
166 file => "$locales_dir/missing",
167 header => $MISSING_HEADER,
168 data_name => '$missing',
169 data_sub => sub { _print_line($_, '', @_) for @new_missing },
173 # calc and generate lost
174 while (my ($text, $translation) = each %old_texts) {
175 next if ($alllocales{$text});
176 push @lost, { 'text' => $text, 'translation' => $translation };
180 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
182 file => "$locales_dir/lost",
183 header => $LOST_HEADER,
185 data_name => '@lost',
187 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
192 my $trlanguage = slurp("$locales_dir/LANGUAGE");
195 search_unused_htmlfiles() if $opt_c;
197 my $count = scalar keys %alllocales;
198 my $notext = scalar @new_missing;
199 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
200 print "\n$trlanguage - ${per}%";
201 print " - $notext/$count missing" if $notext;
210 # These are all the texts to build the translations files.
211 # The file has the form of 'english text' => 'foreign text',
212 # you can add the translation in this file or in the 'missing' file
213 # run locales.pl from this directory to rebuild the translation files
215 $MISSING_HEADER = <<EOL;
216 # add the missing texts and run locales.pl to rebuild
218 $LOST_HEADER = <<EOL;
219 # The last 50 text strings, that have been removed.
220 # This file has been auto-generated by locales.pl. Please don't edit!
228 'no-custom-files' => \$opt_n,
229 'check-files' => \$opt_c,
230 'verbose' => \$opt_v,
242 pod2usage(-exitstatus => 0, -verbose => 2);
247 my $arg = shift @ARGV;
249 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
250 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
256 print "The locale directory '$arg' could not be found.\n";
260 } elsif (!-f 'all' || !-f 'LANGUAGE') {
261 print "locales.pl was not called from a locale/* subdirectory,\n"
262 . "and no locale directory name was given.\n";
268 my ($file, $dir) = @_;
269 print "\n$file" if $opt_v;
273 &scanfile("$dir/$file");
275 # scan custom_{module}.pl or {login}_{module}.pl files
276 foreach my $customfile (@customfiles) {
277 if ($customfile =~ /_$file/) {
278 if (-f "$dir/$customfile") {
279 &scanfile("$dir/$customfile");
287 sub extract_text_between_parenthesis {
288 my ($fh, $line) = @_;
289 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
292 if (length($line) <= $pos) {
294 return ($text, "") unless ($line);
298 my $cur_char = substr($line, $pos, 1);
300 if (!$inside_string) {
301 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
302 $inside_string = substr($line, $pos + 2, 1);
305 } elsif ((length($line) >= ($pos + 2)) &&
306 (substr($line, $pos, 1) eq "q")) {
307 $inside_string = substr($line, $pos + 1, 1);
310 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
311 $inside_string = $cur_char;
313 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
314 return ($text, substr($line, $pos + 1));
319 $text .= '\\' unless $cur_char eq "'";
323 } elsif ($cur_char eq '\\') {
326 } elsif ($cur_char eq $inside_string) {
327 undef($inside_string);
340 my $dont_include_subs = shift;
341 my $scanned_files = shift;
346 $scanned_files = {} unless ($scanned_files);
347 return if ($scanned_files->{$file});
348 $scanned_files->{$file} = 1;
350 if (!defined $cached{$file}) {
352 return unless (-f "$file");
354 my $fh = new FileHandle;
355 open $fh, "$file" or die "$! : $file";
357 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
360 last if /^\s*__END__/;
364 # is this another file
365 if (/require\s+\W.*\.pl/) {
367 $newfile =~ s/require\s+\W//;
368 $newfile =~ s|bin/mozilla||;
369 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
370 } elsif (/use\s+SL::([\w:]*)/) {
373 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
376 # is this a template call?
377 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
378 my $new_file_base = "$basedir/templates/webpages/$1.";
379 if (/parse_html_template2/) {
380 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base("${new_file_base}html") . ".\n";
384 foreach my $ext (qw(html js json)) {
385 my $new_file = "${new_file_base}${ext}";
387 $cached{$file}{scanh}{$new_file} = 1;
393 if ($opt_c && !$found_one) {
394 print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
403 my ($null, $country) = split(/,/);
404 $country =~ s/^ +[\"\']//;
405 $country =~ s/[\"\'].*//;
411 # is it a submit button before $locale->
412 if (/$submitsearch/) {
414 if ($` !~ /locale->text/) {
416 $sub_line_no = $line_no;
420 my ($found) = / (?: locale->text | \b t8 ) \b .*? \(/x;
425 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
428 # if there is no $ in the string record it
429 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
431 # this guarantees one instance of string
432 $cached{$file}{locale}{$string} = 1;
434 # this one is for all the locales
435 $cached{$file}{all}{$string} = 1;
437 # is it a submit button before $locale->
439 $cached{$file}{submit}{$string} = 1;
442 } elsif ($postmatch =~ />/) {
446 # exit loop if there are no more locales on this line
447 ($rc) = ($postmatch =~ /locale->text | \b t8/x);
449 if ( ($postmatch =~ />/)
450 || (!$found && ($sub_line_no != $line_no) && />/)) {
460 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
461 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
462 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
464 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
465 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
466 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
468 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
474 my $fh = new FileHandle;
475 open $fh, "$file" or die "$! : $file";
477 my @a = grep m/^\[/, <$fh>;
481 grep { s/(\[|\])//g } @a;
483 foreach my $item (@a) {
484 my @b = split /--/, $item;
485 foreach my $string (@b) {
487 $locale{$string} = 1;
488 $alllocales{$string} = 1;
494 sub unescape_template_string {
505 if (!defined $cached{$file}) {
506 my %plugins = ( 'loaded' => { }, 'needed' => { } );
508 open(IN, $file) || die $file;
513 while (my $line = <IN>) {
516 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
517 $plugins{loaded}->{$1} = 1;
520 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
522 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator L));
525 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
526 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
527 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
528 (.*?) # Der zu übersetzende String ::Parameter $3::
529 (?<!\\)\2 # Ende des zu übersetzenden Strings
530 | # Start von Variante 2: [% '...' | $T8 %]
531 \[\% # Template-Start-Tag
532 [\-~#]? # Whitespace-Unterdrückung
533 \s* # Optional beliebig viele Whitespace
534 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
535 (.*?) # Der zu übersetzende String ::Parameter $5::
536 (?<!\\)\4 # Ende des zu übersetzenden Strings
537 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
538 (\$T8) # Filteraufruf ::Parameter $6::
539 .*? # Optionale Argumente für den Filter
541 [\-~#]? # Whitespace-Unterdrückung
542 \%\] # Template-Ende-Tag
545 my $module = $1 || $6;
546 my $string = $3 || $5;
547 print "Found filter >>>$string<<<\n" if $debug;
548 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
550 $string = unescape_template_string($string);
551 $cached{$file}{all}{$string} = 1;
552 $cached{$file}{html}{$string} = 1;
553 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
554 $plugins{needed}->{T8} = 1 if $module eq '$T8';
555 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
558 while ($line =~ m/\[\% # Template-Start-Tag
559 [\-~#]? # Whitespace-Unterdrückung
560 \s* # Optional beliebig viele Whitespace
561 (?: # Die erkannten Template-Direktiven
566 \s+ # Mindestens ein Whitespace
567 [\'\"]? # Anfang des Dateinamens
568 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
569 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
571 my $new_file_name = "$basedir/templates/webpages/$1.html";
572 $cached{$file}{scanh}{$new_file_name} = 1;
573 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
579 foreach my $plugin (keys %{ $plugins{needed} }) {
580 next if ($plugins{loaded}->{$plugin});
581 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
585 # copy back into global arrays
586 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
587 $locale{$_} = 1 for keys %{$cached{$file}{html}};
588 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
590 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
592 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
595 sub scan_javascript_file {
598 open(my $fh, $file) || die('can not open file: '. $file);
600 while( my $line = readline($fh) ) {
611 my $text = unescape_template_string($2);
613 $jslocale{$text} = 1;
614 $alllocales{$text} = 1;
620 sub search_unused_htmlfiles {
621 my @unscanned_dirs = ('../../templates/webpages');
623 while (scalar @unscanned_dirs) {
624 my $dir = shift @unscanned_dirs;
626 foreach my $entry (<$dir/*>) {
628 push @unscanned_dirs, $entry;
630 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
631 print "W: unused HTML template: " . strip_base($entry) . "\n";
639 my $s = "$_[0]"; # Create a copy of the string.
642 $s =~ s|templates/webpages/||;
649 $val =~ s/(\'|\\$)/\\$1/g;
650 return "'" . $val . "'";
655 $val =~ s/(\"|\\$)/\\$1/g;
656 return '"'. $val .'"';
660 my $key = _single_quote(shift);
661 my $text = _single_quote(shift);
663 my $template = $params{template} || qq| %-29s => %s,\n|;
664 my $fh = $params{fh} || croak 'need filehandle in _print_line';
666 print $fh sprintf $template, $key, $text;
672 my $file = $params{file} || croak 'need filename in generate_file';
673 my $header = $params{header};
674 my $lines = $params{data_sub};
675 my $data_name = $params{data_name};
676 my @delim = split //, ($params{delim} || '{}');
678 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
680 $charset =~ s/\r?\n//g;
681 my $emacs_charset = lc $charset;
683 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
684 print $fh $header, "\n" if $header;
685 print $fh "$data_name = $delim[0]\n" if $data_name;
689 print $fh qq|$delim[1];\n\n1;\n|;
695 do { local ( @ARGV, $/ ) = $file; <> }
702 locales.pl - Collect strings for translation in kivitendo
706 locales.pl [options] lang_code
709 -n, --no-custom-files Do not process files whose name contains "_"
710 -c, --check-files Run extended checks on HTML files
711 -v, --verbose Be more verbose
712 -h, --help Show this help
718 =item B<-n>, B<--no-custom-files>
720 Do not process files whose name contains "_", e.g. "custom_io.pl".
722 =item B<-c>, B<--check-files>
724 Run extended checks on the usage of templates. This can be used to
725 discover HTML templates that are never used as well as the usage of
726 non-existing HTML templates.
728 =item B<-v>, B<--verbose>
736 This script collects strings from Perl files, the menu.ini file and
737 HTML templates and puts them into the file "all" for translation.