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 use File::Slurp qw(slurp);
20 use List::Util qw(first);
24 $OUTPUT_AUTOFLUSH = 1;
34 my $basedir = "../..";
35 my $locales_dir = ".";
36 my $bindir = "$basedir/bin/mozilla";
37 my @progdirs = ( "$basedir/SL" );
38 my $dbupdir = "$basedir/sql/Pg-upgrade";
39 my $dbupdir2 = "$basedir/sql/Pg-upgrade2";
40 my $menufile = "menu.ini";
41 my @javascript_dirs = ($basedir .'/js', $basedir .'/templates/webpages');
42 my $javascript_output_dir = $basedir .'/js';
43 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
48 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit, %jslocale);
49 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
54 my ($top_dir_name) = @_;
61 tie my %dir_h, 'IO::Dir', $dir_name;
63 push @files, grep { -f } map { "${dir_name}/${_}" } keys %dir_h;
64 my @sub_dirs = grep { -d } map { "${dir_name}/${_}" } grep { ! m/^\.\.?$/ } keys %dir_h;
66 $finder->($_) for @sub_dirs;
69 $finder->($top_dir_name);
75 # overwrite existing entries with the ones from 'missing'
76 $self->{texts}->{$_} = $missing->{$_} for grep { $missing->{$_} } keys %alllocales;
78 # try to set missing entries from lost ones
79 my %lost_by_text = map { ($_->{text} => $_->{translation}) } @lost;
80 $self->{texts}->{$_} = $lost_by_text{$_} for grep { !$self->{texts}{$_} } keys %alllocales;
83 my @bindir_files = find_files($bindir);
84 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
85 my @customfiles = grep /_custom/, @bindir_files;
87 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
89 # put customized files into @customfiles
90 my (@menufiles, %dir_h);
94 @menufiles = ($menufile);
96 tie %dir_h, 'IO::Dir', $basedir;
97 @menufiles = map { "$basedir/$_" } grep { /.*?_$menufile$/ } keys %dir_h;
98 unshift @menufiles, "$basedir/$menufile";
101 tie %dir_h, 'IO::Dir', $dbupdir;
102 my @dbplfiles = grep { /\.pl$/ } keys %dir_h;
104 tie %dir_h, 'IO::Dir', $dbupdir2;
105 my @dbplfiles2 = grep { /\.pl$/ } keys %dir_h;
107 # slurp the translations in
108 if (-f "$locales_dir/all") {
109 require "$locales_dir/all";
111 if (-f "$locales_dir/missing") {
112 require "$locales_dir/missing" ;
113 unlink "$locales_dir/missing";
115 if (-f "$locales_dir/lost") {
116 require "$locales_dir/lost";
117 unlink "$locales_dir/lost";
120 my $charset = slurp("$locales_dir/charset") || 'utf-8';
123 my %old_texts = %{ $self->{texts} || {} };
125 handle_file(@{ $_ }) for @progfiles;
126 handle_file($_, $dbupdir) for @dbplfiles;
127 handle_file($_, $dbupdir2) for @dbplfiles2;
128 scanmenu($_) for @menufiles;
130 for my $file_name (map({find_files($_)} @javascript_dirs)) {
131 scan_javascript_file($file_name);
134 # merge entries to translate with entries from files 'missing' and 'lost'
139 file => "$locales_dir/all",
140 header => $ALL_HEADER,
141 data_name => '$self->{texts}',
142 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
145 open(my $js_file, '>:encoding(utf8)', $javascript_output_dir .'/locale/'. $locale .'.js') || die;
148 for my $key (sort(keys(%jslocale))) {
149 print $js_file ((!$first_entry ? ',' : '') ."\n". _double_quote($key) .':'. _double_quote($self->{texts}{$key}));
152 print $js_file ("\n".'}'."\n");
155 foreach my $text (keys %$missing) {
156 if ($locale{$text} || $htmllocale{$text}) {
157 unless ($self->{texts}{$text}) {
158 $self->{texts}{$text} = $missing->{$text};
164 # calc and generate missing
165 my @new_missing = grep { !$self->{texts}{$_} } sort keys %alllocales;
169 file => "$locales_dir/missing",
170 header => $MISSING_HEADER,
171 data_name => '$missing',
172 data_sub => sub { _print_line($_, '', @_) for @new_missing },
176 # calc and generate lost
177 while (my ($text, $translation) = each %old_texts) {
178 next if ($alllocales{$text});
179 push @lost, { 'text' => $text, 'translation' => $translation };
183 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
185 file => "$locales_dir/lost",
186 header => $LOST_HEADER,
188 data_name => '@lost',
190 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
195 my $trlanguage = slurp("$locales_dir/LANGUAGE");
198 search_unused_htmlfiles() if $opt_c;
200 my $count = scalar keys %alllocales;
201 my $notext = scalar @new_missing;
202 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
203 print "\n$trlanguage - ${per}%";
204 print " - $notext/$count missing" if $notext;
213 # These are all the texts to build the translations files.
214 # The file has the form of 'english text' => 'foreign text',
215 # you can add the translation in this file or in the 'missing' file
216 # run locales.pl from this directory to rebuild the translation files
218 $MISSING_HEADER = <<EOL;
219 # add the missing texts and run locales.pl to rebuild
221 $LOST_HEADER = <<EOL;
222 # The last 50 text strings, that have been removed.
223 # This file has been auto-generated by locales.pl. Please don't edit!
231 'no-custom-files' => \$opt_n,
232 'check-files' => \$opt_c,
233 'verbose' => \$opt_v,
245 pod2usage(-exitstatus => 0, -verbose => 2);
250 my $arg = shift @ARGV;
252 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
253 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
262 print "The locale directory '$arg' could not be found.\n";
266 } elsif (!-f 'all' || !-f 'LANGUAGE') {
267 print "locales.pl was not called from a locale/* subdirectory,\n"
268 . "and no locale directory name was given.\n";
272 $locale ||= (grep { $_ } split m:/:, getcwd())[-1];
277 my ($file, $dir) = @_;
278 print "\n$file" if $opt_v;
282 &scanfile("$dir/$file");
284 # scan custom_{module}.pl or {login}_{module}.pl files
285 foreach my $customfile (@customfiles) {
286 if ($customfile =~ /_$file/) {
287 if (-f "$dir/$customfile") {
288 &scanfile("$dir/$customfile");
296 sub extract_text_between_parenthesis {
297 my ($fh, $line) = @_;
298 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
301 if (length($line) <= $pos) {
303 return ($text, "") unless ($line);
307 my $cur_char = substr($line, $pos, 1);
309 if (!$inside_string) {
310 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
311 $inside_string = substr($line, $pos + 2, 1);
314 } elsif ((length($line) >= ($pos + 2)) &&
315 (substr($line, $pos, 1) eq "q")) {
316 $inside_string = substr($line, $pos + 1, 1);
319 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
320 $inside_string = $cur_char;
322 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
323 return ($text, substr($line, $pos + 1));
328 $text .= '\\' unless $cur_char eq "'";
332 } elsif ($cur_char eq '\\') {
335 } elsif ($cur_char eq $inside_string) {
336 undef($inside_string);
349 my $dont_include_subs = shift;
350 my $scanned_files = shift;
355 $scanned_files = {} unless ($scanned_files);
356 return if ($scanned_files->{$file});
357 $scanned_files->{$file} = 1;
359 if (!defined $cached{$file}) {
361 return unless (-f "$file");
363 my $fh = new FileHandle;
364 open $fh, "$file" or die "$! : $file";
366 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
369 last if /^\s*__END__/;
373 # is this another file
374 if (/require\s+\W.*\.pl/) {
376 $newfile =~ s/require\s+\W//;
377 $newfile =~ s|bin/mozilla||;
378 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
379 } elsif (/use\s+SL::([\w:]*)/) {
382 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
385 # Some calls to render() are split over multiple lines. Deal
387 while (/(?:parse_html_template2?|render)\s*\( *$/) {
392 # is this a template call?
393 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
394 my $new_file_base = "$basedir/templates/webpages/$1.";
395 if (/parse_html_template2/) {
396 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base("${new_file_base}html") . ".\n";
400 foreach my $ext (qw(html js json)) {
401 my $new_file = "${new_file_base}${ext}";
403 $cached{$file}{scanh}{$new_file} = 1;
409 if ($opt_c && !$found_one) {
410 print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
419 my ($null, $country) = split(/,/);
420 $country =~ s/^ +[\"\']//;
421 $country =~ s/[\"\'].*//;
427 # is it a submit button before $locale->
428 if (/$submitsearch/) {
430 if ($` !~ /locale->text/) {
432 $sub_line_no = $line_no;
436 my ($found) = / (?: locale->text | \b t8 ) \b .*? \(/x;
441 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
444 # if there is no $ in the string record it
445 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
447 # this guarantees one instance of string
448 $cached{$file}{locale}{$string} = 1;
450 # this one is for all the locales
451 $cached{$file}{all}{$string} = 1;
453 # is it a submit button before $locale->
455 $cached{$file}{submit}{$string} = 1;
458 } elsif ($postmatch =~ />/) {
462 # exit loop if there are no more locales on this line
463 ($rc) = ($postmatch =~ /locale->text | \b t8/x);
465 if ( ($postmatch =~ />/)
466 || (!$found && ($sub_line_no != $line_no) && />/)) {
476 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
477 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
478 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
480 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
481 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
482 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
484 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
490 my $fh = new FileHandle;
491 open $fh, "$file" or die "$! : $file";
493 my @a = grep m/^\[/, <$fh>;
497 grep { s/(\[|\])//g } @a;
499 foreach my $item (@a) {
500 my @b = split /--/, $item;
501 foreach my $string (@b) {
503 $locale{$string} = 1;
504 $alllocales{$string} = 1;
510 sub unescape_template_string {
521 if (!defined $cached{$file}) {
522 my %plugins = ( 'loaded' => { }, 'needed' => { } );
524 open(IN, $file) || die $file;
529 while (my $line = <IN>) {
532 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
533 $plugins{loaded}->{$1} = 1;
536 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
538 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript JSON L P));
541 $plugins{needed}->{T8} = 1 if $line =~ m/\[\%.*\|.*\$T8/;
543 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
544 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
545 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
546 (.*?) # Der zu übersetzende String ::Parameter $3::
547 (?<!\\)\2 # Ende des zu übersetzenden Strings
548 | # Start von Variante 2: [% '...' | $T8 %]
549 \[\% # Template-Start-Tag
550 [\-~#]? # Whitespace-Unterdrückung
551 \s* # Optional beliebig viele Whitespace
552 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
553 (.*?) # Der zu übersetzende String ::Parameter $5::
554 (?<!\\)\4 # Ende des zu übersetzenden Strings
555 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
556 (\$T8) # Filteraufruf ::Parameter $6::
557 .*? # Optionale Argumente für den Filter
559 [\-~#]? # Whitespace-Unterdrückung
560 \%\] # Template-Ende-Tag
563 my $module = $1 || $6;
564 my $string = $3 || $5;
565 print "Found filter >>>$string<<<\n" if $debug;
566 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
568 $string = unescape_template_string($string);
569 $cached{$file}{all}{$string} = 1;
570 $cached{$file}{html}{$string} = 1;
571 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
572 $plugins{needed}->{T8} = 1 if $module eq '$T8';
573 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
576 while ($line =~ m/\[\% # Template-Start-Tag
577 [\-~#]? # Whitespace-Unterdrückung
578 \s* # Optional beliebig viele Whitespace
579 (?: # Die erkannten Template-Direktiven
584 \s+ # Mindestens ein Whitespace
585 [\'\"]? # Anfang des Dateinamens
586 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
587 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
589 my $new_file_name = "$basedir/templates/webpages/$1.html";
590 $cached{$file}{scanh}{$new_file_name} = 1;
591 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
597 foreach my $plugin (keys %{ $plugins{needed} }) {
598 next if ($plugins{loaded}->{$plugin});
599 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
603 # copy back into global arrays
604 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
605 $locale{$_} = 1 for keys %{$cached{$file}{html}};
606 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
608 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
610 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
613 sub scan_javascript_file {
616 open(my $fh, $file) || die('can not open file: '. $file);
618 while( my $line = readline($fh) ) {
629 my $text = unescape_template_string($2);
631 $jslocale{$text} = 1;
632 $alllocales{$text} = 1;
638 sub search_unused_htmlfiles {
639 my @unscanned_dirs = ('../../templates/webpages');
641 while (scalar @unscanned_dirs) {
642 my $dir = shift @unscanned_dirs;
644 foreach my $entry (<$dir/*>) {
646 push @unscanned_dirs, $entry;
648 } elsif (($entry =~ /_master.html$/) && -f $entry && !$referenced_html_files{$entry}) {
649 print "W: unused HTML template: " . strip_base($entry) . "\n";
657 my $s = "$_[0]"; # Create a copy of the string.
660 $s =~ s|templates/webpages/||;
667 $val =~ s/(\'|\\$)/\\$1/g;
668 return "'" . $val . "'";
673 $val =~ s/(\"|\\$)/\\$1/g;
674 return '"'. $val .'"';
678 my $key = _single_quote(shift);
679 my $text = _single_quote(shift);
681 my $template = $params{template} || qq| %-29s => %s,\n|;
682 my $fh = $params{fh} || croak 'need filehandle in _print_line';
684 print $fh sprintf $template, $key, $text;
690 my $file = $params{file} || croak 'need filename in generate_file';
691 my $header = $params{header};
692 my $lines = $params{data_sub};
693 my $data_name = $params{data_name};
694 my @delim = split //, ($params{delim} || '{}');
696 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
698 $charset =~ s/\r?\n//g;
699 my $emacs_charset = lc $charset;
701 print $fh "#!/usr/bin/perl\n# -*- coding: $emacs_charset; -*-\n# vim: fenc=$charset\n\nuse utf8;\n\n";
702 print $fh $header, "\n" if $header;
703 print $fh "$data_name = $delim[0]\n" if $data_name;
707 print $fh qq|$delim[1];\n\n1;\n|;
713 do { local ( @ARGV, $/ ) = $file; <> }
720 locales.pl - Collect strings for translation in kivitendo
724 locales.pl [options] lang_code
727 -n, --no-custom-files Do not process files whose name contains "_"
728 -c, --check-files Run extended checks on HTML files
729 -v, --verbose Be more verbose
730 -h, --help Show this help
736 =item B<-n>, B<--no-custom-files>
738 Do not process files whose name contains "_", e.g. "custom_io.pl".
740 =item B<-c>, B<--check-files>
742 Run extended checks on the usage of templates. This can be used to
743 discover HTML templates that are never used as well as the usage of
744 non-existing HTML templates.
746 =item B<-v>, B<--verbose>
754 This script collects strings from Perl files, the menu.ini file and
755 HTML templates and puts them into the file "all" for translation.