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 $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
44 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit);
45 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
50 my ($top_dir_name) = @_;
57 tie my %dir_h, 'IO::Dir', $dir_name;
59 push @files, grep { -f } map { "${dir_name}/${_}" } keys %dir_h;
60 my @sub_dirs = grep { -d } map { "${dir_name}/${_}" } grep { ! m/^\.\.?$/ } keys %dir_h;
62 $finder->($_) for @sub_dirs;
65 $finder->($top_dir_name);
71 # overwrite existing entries with the ones from 'missing'
72 $self->{texts}->{$_} = $missing->{$_} for grep { $missing->{$_} } keys %alllocales;
74 # try to set missing entries from lost ones
75 my %lost_by_text = map { ($_->{text} => $_->{translation}) } @lost;
76 $self->{texts}->{$_} = $lost_by_text{$_} for grep { !$self->{texts}{$_} } keys %alllocales;
79 my @bindir_files = find_files($bindir);
80 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
81 my @customfiles = grep /_custom/, @bindir_files;
83 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
85 # put customized files into @customfiles
86 my (@menufiles, %dir_h);
90 @menufiles = ($menufile);
92 tie %dir_h, 'IO::Dir', $basedir;
93 @menufiles = map { "$basedir/$_" } grep { /.*?_$menufile$/ } keys %dir_h;
94 unshift @menufiles, "$basedir/$menufile";
97 tie %dir_h, 'IO::Dir', $dbupdir;
98 my @dbplfiles = grep { /\.pl$/ } keys %dir_h;
100 tie %dir_h, 'IO::Dir', $dbupdir2;
101 my @dbplfiles2 = grep { /\.pl$/ } keys %dir_h;
103 # slurp the translations in
104 if (-f "$locales_dir/all") {
105 require "$locales_dir/all";
107 if (-f "$locales_dir/missing") {
108 require "$locales_dir/missing" ;
109 unlink "$locales_dir/missing";
111 if (-f "$locales_dir/lost") {
112 require "$locales_dir/lost";
113 unlink "$locales_dir/lost";
116 my $charset = slurp("$locales_dir/charset") || 'utf-8';
119 my %old_texts = %{ $self->{texts} || {} };
121 handle_file(@{ $_ }) for @progfiles;
122 handle_file($_, $dbupdir) for @dbplfiles;
123 handle_file($_, $dbupdir2) for @dbplfiles2;
124 scanmenu($_) for @menufiles;
126 # merge entries to translate with entries from files 'missing' and 'lost'
131 file => "$locales_dir/all",
132 header => $ALL_HEADER,
133 data_name => '$self->{texts}',
134 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
137 foreach my $text (keys %$missing) {
138 if ($locale{$text} || $htmllocale{$text}) {
139 unless ($self->{texts}{$text}) {
140 $self->{texts}{$text} = $missing->{$text};
146 # calc and generate missing
147 my @new_missing = grep { !$self->{texts}{$_} } sort keys %alllocales;
151 file => "$locales_dir/missing",
152 header => $MISSING_HEADER,
153 data_name => '$missing',
154 data_sub => sub { _print_line($_, '', @_) for @new_missing },
158 # calc and generate lost
159 while (my ($text, $translation) = each %old_texts) {
160 next if ($alllocales{$text});
161 push @lost, { 'text' => $text, 'translation' => $translation };
165 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
167 file => "$locales_dir/lost",
168 header => $LOST_HEADER,
170 data_name => '@lost',
172 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
177 my $trlanguage = slurp("$locales_dir/LANGUAGE");
180 search_unused_htmlfiles() if $opt_c;
182 my $count = scalar keys %alllocales;
183 my $notext = scalar @new_missing;
184 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
185 print "\n$trlanguage - ${per}%";
186 print " - $notext/$count missing" if $notext;
195 # These are all the texts to build the translations files.
196 # The file has the form of 'english text' => 'foreign text',
197 # you can add the translation in this file or in the 'missing' file
198 # run locales.pl from this directory to rebuild the translation files
200 $MISSING_HEADER = <<EOL;
201 # add the missing texts and run locales.pl to rebuild
203 $LOST_HEADER = <<EOL;
204 # The last 50 text strings, that have been removed.
205 # This file has been auto-generated by locales.pl. Please don't edit!
213 'no-custom-files' => \$opt_n,
214 'check-files' => \$opt_c,
215 'verbose' => \$opt_v,
227 pod2usage(-exitstatus => 0, -verbose => 2);
232 my $arg = shift @ARGV;
234 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
235 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
241 print "The locale directory '$arg' could not be found.\n";
245 } elsif (!-f 'all' || !-f 'LANGUAGE') {
246 print "locales.pl was not called from a locale/* subdirectory,\n"
247 . "and no locale directory name was given.\n";
253 my ($file, $dir) = @_;
254 print "\n$file" if $opt_v;
258 &scanfile("$dir/$file");
260 # scan custom_{module}.pl or {login}_{module}.pl files
261 foreach my $customfile (@customfiles) {
262 if ($customfile =~ /_$file/) {
263 if (-f "$dir/$customfile") {
264 &scanfile("$dir/$customfile");
272 sub extract_text_between_parenthesis {
273 my ($fh, $line) = @_;
274 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
277 if (length($line) <= $pos) {
279 return ($text, "") unless ($line);
283 my $cur_char = substr($line, $pos, 1);
285 if (!$inside_string) {
286 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
287 $inside_string = substr($line, $pos + 2, 1);
290 } elsif ((length($line) >= ($pos + 2)) &&
291 (substr($line, $pos, 1) eq "q")) {
292 $inside_string = substr($line, $pos + 1, 1);
295 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
296 $inside_string = $cur_char;
298 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
299 return ($text, substr($line, $pos + 1));
304 $text .= '\\' unless $cur_char eq "'";
308 } elsif ($cur_char eq '\\') {
311 } elsif ($cur_char eq $inside_string) {
312 undef($inside_string);
325 my $dont_include_subs = shift;
326 my $scanned_files = shift;
331 $scanned_files = {} unless ($scanned_files);
332 return if ($scanned_files->{$file});
333 $scanned_files->{$file} = 1;
335 if (!defined $cached{$file}) {
337 return unless (-f "$file");
339 my $fh = new FileHandle;
340 open $fh, "$file" or die "$! : $file";
342 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
345 last if /^\s*__END__/;
349 # is this another file
350 if (/require\s+\W.*\.pl/) {
352 $newfile =~ s/require\s+\W//;
353 $newfile =~ s|bin/mozilla||;
354 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
355 } elsif (/use\s+SL::([\w:]*)/) {
358 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
361 # is this a template call?
362 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
363 my $new_file_base = "$basedir/templates/webpages/$1.";
364 if (/parse_html_template2/) {
365 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base("${new_file_base}html") . ".\n";
369 foreach my $ext (qw(html js json)) {
370 my $new_file = "${new_file_base}${ext}";
372 $cached{$file}{scanh}{$new_file} = 1;
378 if ($opt_c && !$found_one) {
379 print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
388 my ($null, $country) = split /,/;
389 $country =~ s/^ +[\"\']//;
390 $country =~ s/[\"\'].*//;
396 # is it a submit button before $locale->
397 if (/$submitsearch/) {
399 if ($` !~ /locale->text/) {
401 $sub_line_no = $line_no;
405 my ($found) = / (?: locale->text | \b t8 ) \b .*? \(/x;
410 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
413 # if there is no $ in the string record it
414 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
416 # this guarantees one instance of string
417 $cached{$file}{locale}{$string} = 1;
419 # this one is for all the locales
420 $cached{$file}{all}{$string} = 1;
422 # is it a submit button before $locale->
424 $cached{$file}{submit}{$string} = 1;
427 } elsif ($postmatch =~ />/) {
431 # exit loop if there are no more locales on this line
432 ($rc) = ($postmatch =~ /locale->text | \b t8/x);
434 if ( ($postmatch =~ />/)
435 || (!$found && ($sub_line_no != $line_no) && />/)) {
445 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
446 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
447 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
449 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
450 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
451 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
453 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
459 my $fh = new FileHandle;
460 open $fh, "$file" or die "$! : $file";
462 my @a = grep m/^\[/, <$fh>;
466 grep { s/(\[|\])//g } @a;
468 foreach my $item (@a) {
469 my @b = split /--/, $item;
470 foreach my $string (@b) {
472 $locale{$string} = 1;
473 $alllocales{$string} = 1;
479 sub unescape_template_string {
490 if (!defined $cached{$file}) {
491 my %plugins = ( 'loaded' => { }, 'needed' => { } );
493 open(IN, $file) || die $file;
498 while (my $line = <IN>) {
501 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
502 $plugins{loaded}->{$1} = 1;
505 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
507 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript MultiColumnIterator L));
510 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
511 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
512 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
513 (.*?) # Der zu übersetzende String ::Parameter $3::
514 (?<!\\)\2 # Ende des zu übersetzenden Strings
515 | # Start von Variante 2: [% '...' | $T8 %]
516 \[\% # Template-Start-Tag
517 [\-~#]? # Whitespace-Unterdrückung
518 \s* # Optional beliebig viele Whitespace
519 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
520 (.*?) # Der zu übersetzende String ::Parameter $5::
521 (?<!\\)\4 # Ende des zu übersetzenden Strings
522 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
523 (\$T8) # Filteraufruf ::Parameter $6::
524 .*? # Optionale Argumente für den Filter
526 [\-~#]? # Whitespace-Unterdrückung
527 \%\] # Template-Ende-Tag
530 my $module = $1 || $6;
531 my $string = $3 || $5;
532 print "Found filter >>>$string<<<\n" if $debug;
533 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
535 $string = unescape_template_string($string);
536 $cached{$file}{all}{$string} = 1;
537 $cached{$file}{html}{$string} = 1;
538 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
539 $plugins{needed}->{T8} = 1 if $module eq '$T8';
540 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
543 while ($line =~ m/\[\% # Template-Start-Tag
544 [\-~#]? # Whitespace-Unterdrückung
545 \s* # Optional beliebig viele Whitespace
546 (?: # Die erkannten Template-Direktiven
551 \s+ # Mindestens ein Whitespace
552 [\'\"]? # Anfang des Dateinamens
553 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
554 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
556 my $new_file_name = "$basedir/templates/webpages/$1.html";
557 $cached{$file}{scanh}{$new_file_name} = 1;
558 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
564 foreach my $plugin (keys %{ $plugins{needed} }) {
565 next if ($plugins{loaded}->{$plugin});
566 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
570 # copy back into global arrays
571 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
572 $locale{$_} = 1 for keys %{$cached{$file}{html}};
573 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
575 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
577 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
580 sub search_unused_htmlfiles {
581 my @unscanned_dirs = ('../../templates/webpages');
583 while (scalar @unscanned_dirs) {
584 my $dir = shift @unscanned_dirs;
586 foreach my $entry (<$dir/*>) {
588 push @unscanned_dirs, $entry;
590 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
591 print "W: unused HTML template: " . strip_base($entry) . "\n";
599 my $s = "$_[0]"; # Create a copy of the string.
602 $s =~ s|templates/webpages/||;
609 $val =~ s/(\'|\\$)/\\$1/g;
610 return "'" . $val . "'";
614 my $key = _single_quote(shift);
615 my $text = _single_quote(shift);
617 my $template = $params{template} || qq| %-29s => %s,\n|;
618 my $fh = $params{fh} || croak 'need filehandle in _print_line';
620 print $fh sprintf $template, $key, $text;
626 my $file = $params{file} || croak 'need filename in generate_file';
627 my $header = $params{header};
628 my $lines = $params{data_sub};
629 my $data_name = $params{data_name};
630 my @delim = split //, ($params{delim} || '{}');
632 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
634 $charset =~ s/\r?\n//g;
635 my $emacs_charset = lc $charset;
637 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
638 print $fh $header, "\n" if $header;
639 print $fh "$data_name = $delim[0]\n" if $data_name;
643 print $fh qq|$delim[1];\n\n1;\n|;
649 do { local ( @ARGV, $/ ) = $file; <> }
656 locales.pl - Collect strings for translation in kivitendo
660 locales.pl [options] lang_code
663 -n, --no-custom-files Do not process files whose name contains "_"
664 -c, --check-files Run extended checks on HTML files
665 -v, --verbose Be more verbose
666 -h, --help Show this help
672 =item B<-n>, B<--no-custom-files>
674 Do not process files whose name contains "_", e.g. "custom_io.pl".
676 =item B<-c>, B<--check-files>
678 Run extended checks on the usage of templates. This can be used to
679 discover HTML templates that are never used as well as the usage of
680 non-existing HTML templates.
682 =item B<-v>, B<--verbose>
690 This script collects strings from Perl files, the menu.ini file and
691 HTML templates and puts them into the file "all" for translation.