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);
23 $OUTPUT_AUTOFLUSH = 1;
33 my $basedir = "../..";
34 my $locales_dir = ".";
35 my $bindir = "$basedir/bin/mozilla";
36 my @progdirs = ( "$basedir/SL" );
37 my @menufiles = <${basedir}/menus/*.ini>;
38 my @javascript_dirs = ($basedir .'/js', $basedir .'/templates/webpages');
39 my $javascript_output_dir = $basedir .'/js';
40 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
45 my %ignore_unused_templates = (
46 map { $_ => 1 } qw(ct/testpage.html generic/autocomplete.html oe/periodic_invoices_email.txt part/testpage.html t/render.html t/render.js)
49 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit, %jslocale);
50 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
55 my ($top_dir_name) = @_;
62 tie my %dir_h, 'IO::Dir', $dir_name;
64 push @files, grep { -f } map { "${dir_name}/${_}" } keys %dir_h;
65 my @sub_dirs = grep { -d } map { "${dir_name}/${_}" } grep { ! m/^\.\.?$/ } keys %dir_h;
67 $finder->($_) for @sub_dirs;
70 $finder->($top_dir_name);
76 # overwrite existing entries with the ones from 'missing'
77 $self->{texts}->{$_} = $missing->{$_} for grep { $missing->{$_} } keys %alllocales;
79 # try to set missing entries from lost ones
80 my %lost_by_text = map { ($_->{text} => $_->{translation}) } @lost;
81 $self->{texts}->{$_} = $lost_by_text{$_} for grep { !$self->{texts}{$_} } keys %alllocales;
84 my @bindir_files = find_files($bindir);
85 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
86 my @customfiles = grep /_custom/, @bindir_files;
88 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
90 # put customized files into @customfiles
96 tie %dir_h, 'IO::Dir', $basedir;
97 push @menufiles, map { "$basedir/$_" } grep { /.*_menu.ini$/ } keys %dir_h;
101 foreach my $sub_dir ("Pg-upgrade2", "Pg-upgrade2-auth") {
102 my $dir = "$basedir/sql/$sub_dir";
103 tie %dir_h, 'IO::Dir', $dir;
104 push @dbplfiles, map { [ $_, $dir ] } 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 %old_texts = %{ $self->{texts} || {} };
122 handle_file(@{ $_ }) for @progfiles;
123 handle_file(@{ $_ }) for @dbplfiles;
124 scanmenu($_) for @menufiles;
126 for my $file_name (map({find_files($_)} @javascript_dirs)) {
127 scan_javascript_file($file_name);
130 # merge entries to translate with entries from files 'missing' and 'lost'
135 file => "$locales_dir/all",
136 header => $ALL_HEADER,
137 data_name => '$self->{texts}',
138 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
141 open(my $js_file, '>:encoding(utf8)', $javascript_output_dir .'/locale/'. $locale .'.js') || die;
142 print $js_file 'namespace("kivi").setupLocale({';
144 for my $key (sort(keys(%jslocale))) {
145 print $js_file ((!$first_entry ? ',' : '') ."\n". _double_quote($key) .':'. _double_quote($self->{texts}{$key}));
148 print $js_file ("\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 my %existing_lc = map { (lc $_ => $_) } grep { $self->{texts}->{$_} } keys %{ $self->{texts} };
167 foreach my $entry (@new_missing) {
168 my $other = $existing_lc{lc $entry};
169 print "W: No entry for '${entry}' exists, but there is one with different case: '${other}'\n" if $other;
174 file => "$locales_dir/missing",
175 header => $MISSING_HEADER,
176 data_name => '$missing',
177 data_sub => sub { _print_line($_, '', @_) for @new_missing },
181 # calc and generate lost
182 while (my ($text, $translation) = each %old_texts) {
183 next if ($alllocales{$text});
184 push @lost, { 'text' => $text, 'translation' => $translation };
188 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
190 file => "$locales_dir/lost",
191 header => $LOST_HEADER,
193 data_name => '@lost',
195 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
200 my $trlanguage = slurp("$locales_dir/LANGUAGE");
203 search_unused_htmlfiles() if $opt_c;
205 my $count = scalar keys %alllocales;
206 my $notext = scalar @new_missing;
207 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
208 print "\n$trlanguage - ${per}%";
209 print " - $notext/$count missing" if $notext;
218 # These are all the texts to build the translations files.
219 # The file has the form of 'english text' => 'foreign text',
220 # you can add the translation in this file or in the 'missing' file
221 # run locales.pl from this directory to rebuild the translation files
223 $MISSING_HEADER = <<EOL;
224 # add the missing texts and run locales.pl to rebuild
226 $LOST_HEADER = <<EOL;
227 # The last 50 text strings, that have been removed.
228 # This file has been auto-generated by locales.pl. Please don't edit!
235 my ($opt_no_c, $ignore_for_compatiblity);
238 'no-custom-files' => \$opt_n,
239 'check-files' => \$ignore_for_compatiblity,
240 'no-check-files' => \$opt_no_c,
241 'verbose' => \$opt_v,
255 pod2usage(-exitstatus => 0, -verbose => 2);
260 my $arg = shift @ARGV;
262 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
263 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
272 print "The locale directory '$arg' could not be found.\n";
276 } elsif (!-f 'all' || !-f 'LANGUAGE') {
277 print "locales.pl was not called from a locale/* subdirectory,\n"
278 . "and no locale directory name was given.\n";
282 $locale ||= (grep { $_ } split m:/:, getcwd())[-1];
287 my ($file, $dir) = @_;
288 print "\n$file" if $opt_v;
292 &scanfile("$dir/$file");
294 # scan custom_{module}.pl or {login}_{module}.pl files
295 foreach my $customfile (@customfiles) {
296 if ($customfile =~ /_$file/) {
297 if (-f "$dir/$customfile") {
298 &scanfile("$dir/$customfile");
306 sub extract_text_between_parenthesis {
307 my ($fh, $line) = @_;
308 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
311 if (length($line) <= $pos) {
313 return ($text, "") unless ($line);
317 my $cur_char = substr($line, $pos, 1);
319 if (!$inside_string) {
320 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
321 $inside_string = substr($line, $pos + 2, 1);
324 } elsif ((length($line) >= ($pos + 2)) &&
325 (substr($line, $pos, 1) eq "q")) {
326 $inside_string = substr($line, $pos + 1, 1);
329 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
330 $inside_string = $cur_char;
332 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
333 return ($text, substr($line, $pos + 1));
338 $text .= '\\' unless $cur_char eq "'";
342 } elsif ($cur_char eq '\\') {
345 } elsif ($cur_char eq $inside_string) {
346 undef($inside_string);
359 my $dont_include_subs = shift;
360 my $scanned_files = shift;
365 $scanned_files = {} unless ($scanned_files);
366 return if ($scanned_files->{$file});
367 $scanned_files->{$file} = 1;
369 if (!defined $cached{$file}) {
371 return unless (-f "$file");
373 my $fh = new FileHandle;
374 open $fh, "$file" or die "$! : $file";
376 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
379 last if /^\s*__END__/;
383 # is this another file
384 if (/require\s+\W.*\.pl/) {
386 $newfile =~ s/require\s+\W//;
387 $newfile =~ s|bin/mozilla||;
388 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
389 } elsif (/use\s+SL::([\w:]*)/) {
392 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
395 # Some calls to render() are split over multiple lines. Deal
397 while (/(?:parse_html_template2?|render)\s*\( *$/) {
402 # is this a template call?
403 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
404 my $new_file_base = "$basedir/templates/webpages/$1.";
405 if (/parse_html_template2/) {
406 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base("${new_file_base}html") . ".\n";
410 foreach my $ext (qw(html js json)) {
411 my $new_file = "${new_file_base}${ext}";
413 $cached{$file}{scanh}{$new_file} = 1;
419 if ($opt_c && !$found_one) {
420 print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
429 my ($null, $country) = split(/,/);
430 $country =~ s/^ +[\"\']//;
431 $country =~ s/[\"\'].*//;
437 # is it a submit button before $locale->
438 if (/$submitsearch/) {
440 if ($` !~ /locale->text/) {
442 $sub_line_no = $line_no;
447 if (/ (?: locale->text | \b t8 ) \b .*? \(/x) {
454 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
457 # if there is no $ in the string record it
458 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
460 # this guarantees one instance of string
461 $cached{$file}{locale}{$string} = 1;
463 # this one is for all the locales
464 $cached{$file}{all}{$string} = 1;
466 # is it a submit button before $locale->
468 $cached{$file}{submit}{$string} = 1;
471 } elsif ($postmatch =~ />/) {
475 # exit loop if there are no more locales on this line
476 ($rc) = ($postmatch =~ /locale->text | \b t8/x);
478 if ( ($postmatch =~ />/)
479 || (!$found && ($sub_line_no != $line_no) && />/)) {
489 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
490 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
491 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
493 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
494 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
495 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
497 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
503 my $fh = new FileHandle;
504 open $fh, "$file" or die "$! : $file";
506 my @a = grep m/^\[/, <$fh>;
510 grep { s/(\[|\])//g } @a;
512 foreach my $item (@a) {
513 my @b = split /--/, $item;
514 foreach my $string (@b) {
516 $locale{$string} = 1;
517 $alllocales{$string} = 1;
523 sub unescape_template_string {
534 return if defined $cached{$file};
536 my %plugins = ( 'loaded' => { }, 'needed' => { } );
538 if (!open(IN, $file)) {
539 print "E: template file '$file' not found\n";
546 while (my $line = <IN>) {
549 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
550 $plugins{loaded}->{$1} = 1;
553 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
555 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript JSON L P));
558 $plugins{needed}->{T8} = 1 if $line =~ m/\[\%.*\|.*\$T8/;
560 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
561 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
562 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
563 (.*?) # Der zu übersetzende String ::Parameter $3::
564 (?<!\\)\2 # Ende des zu übersetzenden Strings
565 | # Start von Variante 2: [% '...' | $T8 %]
566 \[\% # Template-Start-Tag
567 [\-~#]? # Whitespace-Unterdrückung
568 \s* # Optional beliebig viele Whitespace
569 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
570 (.*?) # Der zu übersetzende String ::Parameter $5::
571 (?<!\\)\4 # Ende des zu übersetzenden Strings
572 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
573 (\$T8) # Filteraufruf ::Parameter $6::
574 .*? # Optionale Argumente für den Filter
576 [\-~#]? # Whitespace-Unterdrückung
577 \%\] # Template-Ende-Tag
580 my $module = $1 || $6;
581 my $string = $3 || $5;
582 print "Found filter >>>$string<<<\n" if $debug;
583 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
585 $string = unescape_template_string($string);
586 $cached{$file}{all}{$string} = 1;
587 $cached{$file}{html}{$string} = 1;
588 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
589 $plugins{needed}->{T8} = 1 if $module eq '$T8';
590 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
593 while ($line =~ m/\[\% # Template-Start-Tag
594 [\-~#]? # Whitespace-Unterdrückung
595 \s* # Optional beliebig viele Whitespace
596 (?: # Die erkannten Template-Direktiven
601 \s+ # Mindestens ein Whitespace
602 [\'\"]? # Anfang des Dateinamens
603 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
604 \.html # Endung ".html", ansonsten kann es der Name eines Blocks sein
606 my $new_file_name = "$basedir/templates/webpages/$1.html";
607 $cached{$file}{scanh}{$new_file_name} = 1;
608 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
614 foreach my $plugin (keys %{ $plugins{needed} }) {
615 next if ($plugins{loaded}->{$plugin});
616 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
619 # copy back into global arrays
620 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
621 $locale{$_} = 1 for keys %{$cached{$file}{html}};
622 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
624 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
626 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
629 sub scan_javascript_file {
632 open(my $fh, $file) || die('can not open file: '. $file);
634 while( my $line = readline($fh) ) {
645 my $text = unescape_template_string($2);
647 $jslocale{$text} = 1;
648 $alllocales{$text} = 1;
654 sub search_unused_htmlfiles {
655 my @unscanned_dirs = ('../../templates/webpages');
657 while (scalar @unscanned_dirs) {
658 my $dir = shift @unscanned_dirs;
660 foreach my $entry (<$dir/*>) {
662 push @unscanned_dirs, $entry;
664 } elsif (!$ignore_unused_templates{strip_base($entry)} && -f $entry && !$referenced_html_files{$entry}) {
665 print "W: unused HTML template: " . strip_base($entry) . "\n";
673 my $s = "$_[0]"; # Create a copy of the string.
676 $s =~ s|templates/webpages/||;
683 $val =~ s/(\'|\\$)/\\$1/g;
684 return "'" . $val . "'";
689 $val =~ s/(\"|\\$)/\\$1/g;
690 return '"'. $val .'"';
694 my $key = _single_quote(shift);
695 my $text = _single_quote(shift);
697 my $template = $params{template} || qq| %-29s => %s,\n|;
698 my $fh = $params{fh} || croak 'need filehandle in _print_line';
700 print $fh sprintf $template, $key, $text;
706 my $file = $params{file} || croak 'need filename in generate_file';
707 my $header = $params{header};
708 my $lines = $params{data_sub};
709 my $data_name = $params{data_name};
710 my @delim = split //, ($params{delim} || '{}');
712 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
714 print $fh "#!/usr/bin/perl\n# -*- coding: utf-8; -*-\n# vim: fenc=utf-8\n\nuse utf8;\n\n";
715 print $fh $header, "\n" if $header;
716 print $fh "$data_name = $delim[0]\n" if $data_name;
720 print $fh qq|$delim[1];\n\n1;\n|;
728 locales.pl - Collect strings for translation in kivitendo
732 locales.pl [options] lang_code
735 -n, --no-custom-files Do not process files whose name contains "_"
736 -c, --check-files Run extended checks on HTML files
737 -v, --verbose Be more verbose
738 -h, --help Show this help
744 =item B<-n>, B<--no-custom-files>
746 Do not process files whose name contains "_", e.g. "custom_io.pl".
748 =item B<-c>, B<--check-files>
750 Run extended checks on the usage of templates. This can be used to
751 discover HTML templates that are never used as well as the usage of
752 non-existing HTML templates.
754 =item B<-v>, B<--verbose>
762 This script collects strings from Perl files, the menu.ini file and
763 HTML templates and puts them into the file "all" for translation.