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.
13 unshift(@INC, 'modules/override'); # Use our own versions of various modules (e.g. YAML).
14 push (@INC, 'modules/fallback'); # Only use our own versions of modules if there's no system version.
21 use File::Slurp qw(slurp);
25 use List::Util qw(first);
28 $OUTPUT_AUTOFLUSH = 1;
39 my $basedir = "../..";
40 my $locales_dir = ".";
41 my $bindir = "$basedir/bin/mozilla";
42 my @progdirs = ( "$basedir/SL" );
43 my @menufiles = <${basedir}/menus/*.ini>;
44 my @javascript_dirs = ($basedir .'/js', $basedir .'/templates/webpages');
45 my $javascript_output_dir = $basedir .'/js';
46 my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
51 my %ignore_unused_templates = (
52 map { $_ => 1 } qw(ct/testpage.html generic/autocomplete.html oe/periodic_invoices_email.txt part/testpage.html t/render.html t/render.js task_server/failure_notification_email.txt)
55 my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit, %jslocale);
56 my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
61 my ($top_dir_name) = @_;
68 tie my %dir_h, 'IO::Dir', $dir_name;
70 push @files, grep { -f } map { "${dir_name}/${_}" } keys %dir_h;
71 my @sub_dirs = grep { -d } map { "${dir_name}/${_}" } grep { ! m/^\.\.?$/ } keys %dir_h;
73 $finder->($_) for @sub_dirs;
76 $finder->($top_dir_name);
82 # overwrite existing entries with the ones from 'missing'
83 $self->{texts}->{$_} = $missing->{$_} for grep { $missing->{$_} } keys %alllocales;
85 # try to set missing entries from lost ones
86 my %lost_by_text = map { ($_->{text} => $_->{translation}) } @lost;
87 $self->{texts}->{$_} = $lost_by_text{$_} for grep { !$self->{texts}{$_} } keys %alllocales;
90 my @bindir_files = find_files($bindir);
91 my @progfiles = map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pl$/ && !/_custom/ } @bindir_files;
92 my @customfiles = grep /_custom/, @bindir_files;
94 push @progfiles, map { m:^(.+)/([^/]+)$:; [ $2, $1 ] } grep { /\.pm$/ } map { find_files($_) } @progdirs;
96 # put customized files into @customfiles
102 tie %dir_h, 'IO::Dir', $basedir;
103 push @menufiles, map { "$basedir/$_" } grep { /.*_menu.ini$/ } keys %dir_h;
107 foreach my $sub_dir ("Pg-upgrade2", "Pg-upgrade2-auth") {
108 my $dir = "$basedir/sql/$sub_dir";
109 tie %dir_h, 'IO::Dir', $dir;
110 push @dbplfiles, map { [ $_, $dir ] } grep { /\.pl$/ } keys %dir_h;
113 # slurp the translations in
114 if (-f "$locales_dir/all") {
115 require "$locales_dir/all";
117 if (-f "$locales_dir/missing") {
118 require "$locales_dir/missing" ;
119 unlink "$locales_dir/missing";
121 if (-f "$locales_dir/lost") {
122 require "$locales_dir/lost";
123 unlink "$locales_dir/lost";
126 my %old_texts = %{ $self->{texts} || {} };
128 handle_file(@{ $_ }) for @progfiles;
129 handle_file(@{ $_ }) for @dbplfiles;
130 scanmenu($_) for @menufiles;
132 for my $file_name (grep { /\.(?:js|html)$/i } map({find_files($_)} @javascript_dirs)) {
133 scan_javascript_file($file_name);
136 # merge entries to translate with entries from files 'missing' and 'lost'
141 file => "$locales_dir/all",
142 header => $ALL_HEADER,
143 data_name => '$self->{texts}',
144 data_sub => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
147 open(my $js_file, '>:encoding(utf8)', $javascript_output_dir .'/locale/'. $locale .'.js') || die;
148 print $js_file 'namespace("kivi").setupLocale({';
150 for my $key (sort(keys(%jslocale))) {
151 print $js_file ((!$first_entry ? ',' : '') ."\n". _double_quote($key) .':'. _double_quote($self->{texts}{$key}));
154 print $js_file ("\n");
155 print $js_file ('});'."\n");
158 foreach my $text (keys %$missing) {
159 if ($locale{$text} || $htmllocale{$text}) {
160 unless ($self->{texts}{$text}) {
161 $self->{texts}{$text} = $missing->{$text};
167 # calc and generate missing
168 my @new_missing = grep { !$self->{texts}{$_} } sort keys %alllocales;
172 my %existing_lc = map { (lc $_ => $_) } grep { $self->{texts}->{$_} } keys %{ $self->{texts} };
173 foreach my $entry (@new_missing) {
174 my $other = $existing_lc{lc $entry};
175 print "W: No entry for '${entry}' exists, but there is one with different case: '${other}'\n" if $other;
180 for my $string (@new_missing) {
181 print "new string '$string' in files:\n";
182 for my $file (keys %cached) {
183 print " $file", $/ if $cached{$file}{all}{$string};
189 file => "$locales_dir/missing",
190 header => $MISSING_HEADER,
191 data_name => '$missing',
192 data_sub => sub { _print_line($_, '', @_) for @new_missing },
196 # calc and generate lost
197 while (my ($text, $translation) = each %old_texts) {
198 next if ($alllocales{$text});
199 push @lost, { 'text' => $text, 'translation' => $translation };
203 splice @lost, 0, (scalar @lost - 50) if (scalar @lost > 50);
205 file => "$locales_dir/lost",
206 header => $LOST_HEADER,
208 data_name => '@lost',
210 _print_line($_->{text}, $_->{translation}, @_, template => " { 'text' => %s, 'translation' => %s },\n") for @lost;
215 my $trlanguage = slurp("$locales_dir/LANGUAGE");
218 search_unused_htmlfiles() if $opt_c;
220 my $count = scalar keys %alllocales;
221 my $notext = scalar @new_missing;
222 my $per = sprintf("%.1f", ($count - $notext) / $count * 100);
223 print "\n$trlanguage - ${per}%";
224 print " - $notext/$count missing" if $notext;
233 # These are all the texts to build the translations files.
234 # The file has the form of 'english text' => 'foreign text',
235 # you can add the translation in this file or in the 'missing' file
236 # run locales.pl from this directory to rebuild the translation files
238 $MISSING_HEADER = <<EOL;
239 # add the missing texts and run locales.pl to rebuild
241 $LOST_HEADER = <<EOL;
242 # The last 50 text strings, that have been removed.
243 # This file has been auto-generated by locales.pl. Please don't edit!
250 my ($opt_no_c, $ignore_for_compatiblity);
253 'no-custom-files' => \$opt_n,
254 'check-files' => \$ignore_for_compatiblity,
255 'no-check-files' => \$opt_no_c,
256 'verbose' => \$opt_v,
257 'filenames' => \$opt_f,
271 pod2usage(-exitstatus => 0, -verbose => 2);
276 my $arg = shift @ARGV;
278 foreach my $dir ("../locale/$arg", "locale/$arg", "../$arg", $arg) {
279 next unless -d $dir && -f "$dir/all" && -f "$dir/LANGUAGE";
288 print "The locale directory '$arg' could not be found.\n";
292 } elsif (!-f 'all' || !-f 'LANGUAGE') {
293 print "locales.pl was not called from a locale/* subdirectory,\n"
294 . "and no locale directory name was given.\n";
298 $locale ||= (grep { $_ } split m:/:, getcwd())[-1];
303 my ($file, $dir) = @_;
304 print "\n$file" if $opt_v;
308 &scanfile("$dir/$file");
310 # scan custom_{module}.pl or {login}_{module}.pl files
311 foreach my $customfile (@customfiles) {
312 if ($customfile =~ /_$file/) {
313 if (-f "$dir/$customfile") {
314 &scanfile("$dir/$customfile");
322 sub extract_text_between_parenthesis {
323 my ($fh, $line) = @_;
324 my ($inside_string, $pos, $text, $quote_next) = (undef, 0, "", 0);
327 if (length($line) <= $pos) {
329 return ($text, "") unless ($line);
333 my $cur_char = substr($line, $pos, 1);
335 if (!$inside_string) {
336 if ((length($line) >= ($pos + 3)) && (substr($line, $pos, 2)) eq "qq") {
337 $inside_string = substr($line, $pos + 2, 1);
340 } elsif ((length($line) >= ($pos + 2)) &&
341 (substr($line, $pos, 1) eq "q")) {
342 $inside_string = substr($line, $pos + 1, 1);
345 } elsif (($cur_char eq '"') || ($cur_char eq '\'')) {
346 $inside_string = $cur_char;
348 } elsif (($cur_char eq ")") || ($cur_char eq ',')) {
349 return ($text, substr($line, $pos + 1));
354 $text .= '\\' unless $cur_char eq "'";
358 } elsif ($cur_char eq '\\') {
361 } elsif ($cur_char eq $inside_string) {
362 undef($inside_string);
375 my $dont_include_subs = shift;
376 my $scanned_files = shift;
381 $scanned_files = {} unless ($scanned_files);
382 return if ($scanned_files->{$file});
383 $scanned_files->{$file} = 1;
385 if (!defined $cached{$file}) {
387 return unless (-f "$file");
389 my $fh = new FileHandle;
390 open $fh, '<:encoding(utf8)', $file or die "$! : $file";
392 my ($is_submit, $line_no, $sub_line_no) = (0, 0, 0);
395 last if /^\s*__END__/;
399 # is this another file
400 if (/require\s+\W.*\.pl/) {
402 $newfile =~ s/require\s+\W//;
403 $newfile =~ s|bin/mozilla||;
404 $cached{$file}{scan}{"$bindir/$newfile"} = 1;
405 } elsif (/use\s+SL::([\w:]*)/) {
408 $cached{$file}{scannosubs}{"../../SL/${module}.pm"} = 1;
411 # Some calls to render() are split over multiple lines. Deal
413 while (/(?:parse_html_template2?|render)\s*\( *$/) {
418 # is this a template call?
419 if (/(?:parse_html_template2?|render)\s*\(\s*[\"\']([\w\/]+)\s*[\"\']/) {
420 my $new_file_base = "$basedir/templates/webpages/$1.";
421 if (/parse_html_template2/) {
422 print "E: " . strip_base($file) . " is still using 'parse_html_template2' for " . strip_base("${new_file_base}html") . ".\n";
426 foreach my $ext (qw(html js json)) {
427 my $new_file = "${new_file_base}${ext}";
429 $cached{$file}{scanh}{$new_file} = 1;
435 if ($opt_c && !$found_one) {
436 print "W: missing HTML template: " . strip_base($new_file_base) . "{html,json,js} (referenced from " . strip_base($file) . ")\n";
445 my ($null, $country) = split(/,/);
446 $country =~ s/^ +[\"\']//;
447 $country =~ s/[\"\'].*//;
453 # is it a submit button before $locale->
454 if (/$submitsearch/) {
456 if ($` !~ /locale->text/) {
458 $sub_line_no = $line_no;
463 if (/ (?: locale->text | \b t8 ) \b .*? \(/x) {
470 ($string, $_) = extract_text_between_parenthesis($fh, $postmatch);
473 # if there is no $ in the string record it
474 unless (($string =~ /\$\D.*/) || ("" eq $string)) {
476 # this guarantees one instance of string
477 $cached{$file}{locale}{$string} = 1;
479 # this one is for all the locales
480 $cached{$file}{all}{$string} = 1;
482 # is it a submit button before $locale->
484 $cached{$file}{submit}{$string} = 1;
487 } elsif ($postmatch =~ />/) {
491 # exit loop if there are no more locales on this line
492 ($rc) = ($postmatch =~ /locale->text | \b t8/x);
494 if ( ($postmatch =~ />/)
495 || (!$found && ($sub_line_no != $line_no) && />/)) {
505 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
506 $locale{$_} = 1 for keys %{$cached{$file}{locale}};
507 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
509 scanfile($_, 0, $scanned_files) for keys %{$cached{$file}{scan}};
510 scanfile($_, 1, $scanned_files) for keys %{$cached{$file}{scannosubs}};
511 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
513 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
519 my $fh = new FileHandle;
520 open $fh, '<:encoding(utf8)', $file or die "$! : $file";
522 my @a = grep m/^\[/, <$fh>;
526 grep { s/(\[|\])//g } @a;
528 foreach my $item (@a) {
529 my @b = split /--/, $item;
530 foreach my $string (@b) {
532 $locale{$string} = 1;
533 $alllocales{$string} = 1;
539 sub unescape_template_string {
550 return if defined $cached{$file};
552 my %plugins = ( 'loaded' => { }, 'needed' => { } );
554 if (!open(IN, '<:encoding(utf8)', $file)) {
555 print "E: template file '$file' not found\n";
562 while (my $line = <IN>) {
565 while ($line =~ m/\[\%[^\w]*use[^\w]+(\w+)[^\w]*?\%\]/gi) {
566 $plugins{loaded}->{$1} = 1;
569 while ($line =~ m/\[\%[^\w]*(\w+)\.\w+\(/g) {
571 $plugins{needed}->{$plugin} = 1 if (first { $_ eq $plugin } qw(HTML LxERP JavaScript JSON L P));
574 $plugins{needed}->{T8} = 1 if $line =~ m/\[\%.*\|.*\$T8/;
576 while ($line =~ m/(?: # Start von Variante 1: LxERP.t8('...'); ohne darumliegende [% ... %]-Tags
577 (LxERP\.t8)\( # LxERP.t8( ::Parameter $1::
578 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $2::
579 (.*?) # Der zu übersetzende String ::Parameter $3::
580 (?<!\\)\2 # Ende des zu übersetzenden Strings
581 | # Start von Variante 2: [% '...' | $T8 %]
582 \[\% # Template-Start-Tag
583 [\-~#]? # Whitespace-Unterdrückung
584 \s* # Optional beliebig viele Whitespace
585 ([\'\"]) # Anfang des zu übersetzenden Strings ::Parameter $4::
586 (.*?) # Der zu übersetzende String ::Parameter $5::
587 (?<!\\)\4 # Ende des zu übersetzenden Strings
588 \s*\|\s* # Pipe-Zeichen mit optionalen Whitespace davor und danach
589 (\$T8) # Filteraufruf ::Parameter $6::
590 .*? # Optionale Argumente für den Filter
592 [\-~#]? # Whitespace-Unterdrückung
593 \%\] # Template-Ende-Tag
596 my $module = $1 || $6;
597 my $string = $3 || $5;
598 print "Found filter >>>$string<<<\n" if $debug;
599 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
601 $string = unescape_template_string($string);
602 $cached{$file}{all}{$string} = 1;
603 $cached{$file}{html}{$string} = 1;
604 $cached{$file}{submit}{$string} = 1 if $PREMATCH =~ /$submitsearch/;
605 $plugins{needed}->{T8} = 1 if $module eq '$T8';
606 $plugins{needed}->{LxERP} = 1 if $module eq 'LxERP.t8';
609 while ($line =~ m/\[\% # Template-Start-Tag
610 [\-~#]? # Whitespace-Unterdrückung
611 \s* # Optional beliebig viele Whitespace
612 (?: # Die erkannten Template-Direktiven
617 \s+ # Mindestens ein Whitespace
618 [\'\"]? # Anfang des Dateinamens
619 ([^\s]+) # Beliebig viele Nicht-Whitespaces -- Dateiname
620 \.(html|js) # Endung ".html" oder ".js", ansonsten kann es der Name eines Blocks sein
622 my $new_file_name = "$basedir/templates/webpages/$1.$2";
623 $cached{$file}{scanh}{$new_file_name} = 1;
624 substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[0] - $LAST_MATCH_START[0], '';
630 foreach my $plugin (keys %{ $plugins{needed} }) {
631 next if ($plugins{loaded}->{$plugin});
632 print "E: " . strip_base($file) . " requires the Template plugin '$plugin', but is not loaded with '[\% USE $plugin \%]'.\n";
635 # copy back into global arrays
636 $alllocales{$_} = 1 for keys %{$cached{$file}{all}};
637 $locale{$_} = 1 for keys %{$cached{$file}{html}};
638 $submit{$_} = 1 for keys %{$cached{$file}{submit}};
640 scanhtmlfile($_) for keys %{$cached{$file}{scanh}};
642 $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
645 sub scan_javascript_file {
648 open(my $fh, '<:encoding(utf8)', $file) || die('can not open file: '. $file);
650 while( my $line = readline($fh) ) {
661 my $text = unescape_template_string($2);
663 $jslocale{$text} = 1;
664 $alllocales{$text} = 1;
670 sub search_unused_htmlfiles {
671 my @unscanned_dirs = ('../../templates/webpages');
673 while (scalar @unscanned_dirs) {
674 my $dir = shift @unscanned_dirs;
676 foreach my $entry (<$dir/*>) {
678 push @unscanned_dirs, $entry;
680 } elsif (!$ignore_unused_templates{strip_base($entry)} && -f $entry && !$referenced_html_files{$entry}) {
681 print "W: unused HTML template: " . strip_base($entry) . "\n";
689 my $s = "$_[0]"; # Create a copy of the string.
692 $s =~ s|templates/webpages/||;
699 $val =~ s/(\'|\\$)/\\$1/g;
700 return "'" . $val . "'";
705 $val =~ s/(\"|\\$)/\\$1/g;
706 return '"'. $val .'"';
710 my $key = _single_quote(shift);
711 my $text = _single_quote(shift);
713 my $template = $params{template} || qq| %-29s => %s,\n|;
714 my $fh = $params{fh} || croak 'need filehandle in _print_line';
716 print $fh sprintf $template, $key, $text;
722 my $file = $params{file} || croak 'need filename in generate_file';
723 my $header = $params{header};
724 my $lines = $params{data_sub};
725 my $data_name = $params{data_name};
726 my @delim = split //, ($params{delim} || '{}');
728 open my $fh, '>:encoding(utf8)', $file or die "$! : $file";
730 print $fh "#!/usr/bin/perl\n# -*- coding: utf-8; -*-\n# vim: fenc=utf-8\n\nuse utf8;\n\n";
731 print $fh $header, "\n" if $header;
732 print $fh "$data_name = $delim[0]\n" if $data_name;
736 print $fh qq|$delim[1];\n\n1;\n|;
744 locales.pl - Collect strings for translation in kivitendo
748 locales.pl [options] lang_code
751 -n, --no-custom-files Do not process files whose name contains "_"
752 -c, --check-files Run extended checks on HTML files
753 -f, --filenames Show the filenames where new strings where found
754 -v, --verbose Be more verbose
755 -h, --help Show this help
761 =item B<-n>, B<--no-custom-files>
763 Do not process files whose name contains "_", e.g. "custom_io.pl".
765 =item B<-c>, B<--check-files>
767 Run extended checks on the usage of templates. This can be used to
768 discover HTML templates that are never used as well as the usage of
769 non-existing HTML templates.
771 =item B<-v>, B<--verbose>
779 This script collects strings from Perl files, the menu.ini file and
780 HTML templates and puts them into the file "all" for translation.