From dc6929f69dee95949e9b3fb46419c6ecf13fa6bf Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Mon, 28 Apr 2014 14:25:24 +0300 Subject: [PATCH] Bug 12221: Remove TT statement placeholders from translatable strings The translation message catalogs contain a lot of Template Toolkit statements in the messages. They bloat the message lengths, and add unnecessary complications for translators, making errors far more likely. For example, there's no need to have "INCLUDE 'opac-bottom.inc'" show up as a placeholder in the messages. Before this patch, the top 5 placeholders in the catalogs are: 1750 END 1429 IF 647 ELSE 263 ELSIF 252 INCLUDE After this patch, the placeholders are only variables. The top 5 is: 32 total 22 searchfield 20 bibliotitle 19 surname 19 OPACBaseURL Use the following incantation to get the stats: grep -E '^#\. %[0-9]+\$s: ' po/xx-YY-*.po | cut -d':' -f 3- | sed -e 's/^ *\(IF\|ELSIF\|CASE\|SWITCH\|INCLUDE\|USE\|SET\|PROCESS\|BLOCK\|FOREACH\|UNLESS\) *.*$/ \1 /g' | sort | uniq -c | sort -rn The number of separate msgid's does not change very much: 19430 before the patch, 19995 afterwards. --- misc/translator/TmplTokenizer.pm | 58 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/misc/translator/TmplTokenizer.pm b/misc/translator/TmplTokenizer.pm index 6129f8d..3ad2970 100644 --- a/misc/translator/TmplTokenizer.pm +++ b/misc/translator/TmplTokenizer.pm @@ -303,6 +303,56 @@ sub _parametrize_internal{ return $t; } +sub trimmable_string { + my $s = shift; + + # Remove TT start and end tag + $s =~ s/^\[%[-+]?\s+//; + $s =~ s/\s+[-+]?%\]$//; + + return 1 if ($s =~ /^#/); # comment + + # semicolon or newline is a TT directive separator. + if ($s =~ /[;\n]/) { + my @parts = split(/[;\n]/, $s); + foreach my $tmps (@parts) { + return 0 if (!trimmable_string($tmps)); + } + return 1; + } + + # trim away spaces + $s =~ s/^\s+//; + $s =~ s/\s+$//; + + if ($s =~ /^IF / || + $s =~ /^ELSIF / || + $s =~ /^ELSE\b/ || + $s =~ /^END\b/ || + $s =~ /^INCLUDE / || + $s =~ /^PROCESS / || + $s =~ /^BLOCK / || + $s =~ /^SWITCH / || + $s =~ /^CASE\b/ || + $s =~ /^SET / || + $s =~ /^FILTER / || + $s =~ /^FOREACH / || + $s =~ /^FOR / || + $s =~ /^USE / || + $s =~ /^UNLESS / || + $s =~ /^\S+ *= *\S/ # variable = value + ) { + return 1; + } + return 0; +} + +sub trimmable { + my $t = shift; + return 0 if (!$t || $t->type != C4::TmplTokenType::DIRECTIVE); + return trimmable_string($t->string); +} + sub next_token { my $self = shift; my $next; @@ -326,7 +376,13 @@ sub next_token { } # elsif( $next->type == C4::TmplTokenType::DIRECTIVE && $next->string =~ m/\[%\s*\w+\s*%\]/ ){ elsif( $next->type == C4::TmplTokenType::DIRECTIVE ){ - push @parts, $next; + if (trimmable($next)) { + return $next unless @parts; + $self->{_parser}->unshift_token($next); + return $self->_parametrize_internal(@parts); + } else { + push @parts, $next; + } } elsif ( $next->type == C4::TmplTokenType::CDATA){ $self->_set_js_mode(1); -- 1.8.3.2