[Bps-public-commit] r15949 - in Locale-Maketext-Lexicon: lib/Locale/Maketext

jesse at bestpractical.com jesse at bestpractical.com
Fri Sep 12 11:33:53 EDT 2008


Author: jesse
Date: Fri Sep 12 11:33:44 2008
New Revision: 15949

Modified:
   Locale-Maketext-Lexicon/lib/Locale/Maketext/Extract.pm
   Locale-Maketext-Lexicon/t/5-extract.t

Log:
Subject:    concat in loc() works now and TT can now use [% FILTER loc %]
now it is possible to concat strings in loc() and in TT you can use [% FILTER loc %]

http://rt.cpan.org/Public/Bug/Display.html?id=39057 from Sadrak <felix.ostmann [...] thewar.de> 


Modified: Locale-Maketext-Lexicon/lib/Locale/Maketext/Extract.pm
==============================================================================
--- Locale-Maketext-Lexicon/lib/Locale/Maketext/Extract.pm	(original)
+++ Locale-Maketext-Lexicon/lib/Locale/Maketext/Extract.pm	Fri Sep 12 11:33:44 2008
@@ -37,6 +37,13 @@
 Valid localization function names are: C<translate>, C<maketext>,
 C<gettext>, C<loc>, C<x>, C<_> and C<__>.
 
+Concatination of all kind of strings are possible, so you can write this:
+
+_("here my $voice" . "\n" . 'dont fear my [_1]' . <<'__LOC__'
+heredoc
+__LOC__
+, $count);
+
 =item HTML::Mason
 
 Strings inside C<E<lt>&|/lE<gt>I<...>E<lt>/&E<gt>> and
@@ -45,7 +52,7 @@
 =item Template Toolkit
 
 Strings inside C<[%|l%]...[%END%]> or C<[%|loc%]...[%END%]>
-are extracted.
+or C<[%FILTER loc%]> or C<[%FILTER l%]> are extracted.
 
 =item Text::Template
 
@@ -255,7 +262,7 @@
 
     # Template Toolkit
     $line = 1; pos($_) = 0;
-    while (m!\G(.*?\[%-?\s*\|l(?:oc)?(.*?)\s*(-?)%\](.*?)\[%(-?)\s*END\s*-?%\])!sg) { 
+    while (m!\G(.*?\[%-?\s*(?:\|\s*|FILTER\s+)l(?:oc)?(.*?)\s*(-?)%\](.*?)\[%(-?)\s*END\s*-?%\])!sg) { 
         my ($vars, $trim_start, $str, $trim_end) = ($2, $3, $4, $5);
         $line += ( () = ($1 =~ /\n/g) ); # cryptocontext!
         $vars =~ s/^\s*\(//;
@@ -305,7 +312,7 @@
     }
 
     # Perl code:
-    my ($state,$str,$vars,$quo,$heredoc)=(0);
+    my ($state,$line_offset,$str,$str_part,$vars,$quo,$heredoc)=(0,0);
     pos($_) = 0;
     my $orig = 1 + (() = ((my $__ = $_) =~ /\n/g));
 
@@ -322,28 +329,45 @@
         $state == BEG  && m/^([\S\(])\s*/gc
                        && do { $state = ( ($1 eq '(') ? PAR : NUL); redo };
 
+        # concat
+        $state == PAR  && defined($str) && m/^(\s*\.\s*)/gc
+                       && do { $line_offset += (() = ((my $__ = $1) =~ /\n/g)); redo };
+
+        # str_part
+        $state == PAR  && defined($str_part) && do {
+            if (($quo == QUO1) || ($quo == QUO5) ){
+                $str_part =~ s/\\([\\'])/$1/g if ($str_part); # normalize q strings
+            }
+            elsif ($quo != QUO6) {
+                $str_part =~ s/(\\(?:[0x]..|c?.))/"qq($1)"/eeg if ($str_part); # normalize qq / qx strings
+            }
+            $str .= $str_part;
+            undef $str_part; undef $quo;
+            redo;
+        };
+
         # begin or end of string
         $state == PAR  && m/^(\')/gc        && do { $state = $quo = QUO1;   redo };
-        $state == QUO1 && m/^([^'\\]+)/gc   && do { $str  .= $1; redo };
-        $state == QUO1 && m/^((?:\\.)+)/gcs && do { $str  .= $1; redo };
+        $state == QUO1 && m/^([^'\\]+)/gc   && do { $str_part .= $1; redo };
+        $state == QUO1 && m/^((?:\\.)+)/gcs && do { $str_part .= $1; redo };
         $state == QUO1 && m/^\'/gc          && do { $state = PAR;           redo };
 
         $state == PAR  && m/^\"/gc          && do { $state = $quo = QUO2;   redo };
-        $state == QUO2 && m/^([^"\\]+)/gc   && do { $str  .= $1; redo };
-        $state == QUO2 && m/^((?:\\.)+)/gcs && do { $str  .= $1; redo };
+        $state == QUO2 && m/^([^"\\]+)/gc   && do { $str_part .= $1; redo };
+        $state == QUO2 && m/^((?:\\.)+)/gcs && do { $str_part .= $1; redo };
         $state == QUO2 && m/^\"/gc          && do { $state = PAR;           redo };
 
         $state == PAR  && m/^\`/gc          && do { $state = $quo = QUO3;   redo };
-        $state == QUO3 && m/^([^\`]*)/gc    && do { $str  .= $1;            redo };
+        $state == QUO3 && m/^([^\`]*)/gc    && do { $str_part .= $1;            redo };
         $state == QUO3 && m/^\`/gc          && do { $state = PAR;           redo };
 
         $state == PAR  && m/^qq\{/gc        && do { $state = $quo = QUO4;   redo };
-        $state == QUO4 && m/^([^\}]*)/gc    && do { $str  .= $1;            redo };
+        $state == QUO4 && m/^([^\}]*)/gc    && do { $str_part .= $1;            redo };
         $state == QUO4 && m/^\}/gc          && do { $state = PAR;           redo };
         
         
         $state == PAR  && m/^q\{/gc         && do { $state = $quo = QUO5;   redo };
-        $state == QUO5 && m/^([^\}]*)/gc    && do { $str  .= $1;            redo };
+        $state == QUO5 && m/^([^\}]*)/gc    && do { $str_part .= $1;            redo };
         $state == QUO5 && m/^\}/gc          && do { $state = PAR;           redo };
 
         # find heredoc terminator, then get the heredoc and go back to current position
@@ -360,7 +384,7 @@
         $state == PAR  && m/^<<(\w*)/gc     && do { $state = HERE; $quo = QUO7; $heredoc = $1; redo };
 
         # jump ahaid and get the heredoc, then s/// also reset the pos and we are back at the current pos
-        $state == HERE && m/^.*\r?\n/gc     && s/\G(.*?\r?\n)$heredoc(\r?\n)//s && do { $state = PAR; $str .= $1; redo };
+        $state == HERE && m/^.*\r?\n/gc     && s/\G(.*?\r?\n)$heredoc(\r?\n)//s && do { $state = PAR; $str_part .= $1; $line_offset++; redo };
 
         # end ()
         #
@@ -368,15 +392,9 @@
         $state == PAR  && m/^\s*[\)]/gc     && do {
             $state = NUL; 
             $vars =~ s/[\n\r]//g if ($vars);
-            if (($quo == QUO1) || ($quo == QUO5) ){
-                $str =~ s/\\([\\'])/$1/g if ($str); # normalize q strings
-            }
-            elsif ($quo != QUO6) {
-                $str =~ s/(\\(?:[0x]..|c?.))/"qq($1)"/eeg if ($str); # normalize qq / qx strings
-            }
-            # heredoc loosing the terminating line, so decrement one more line for heredoc
-            push @{$entries->{$str}}, [ $file, $line - (() = $str =~ /\n/g) - defined($heredoc), $vars] if ($str);
+            push @{$entries->{$str}}, [ $file, $line - (() = $str =~ /\n/g) - $line_offset, $vars] if ($str);
             undef $str; undef $vars; undef $heredoc;
+            $line_offset = 0;
             redo;
         };
 

Modified: Locale-Maketext-Lexicon/t/5-extract.t
==============================================================================
--- Locale-Maketext-Lexicon/t/5-extract.t	(original)
+++ Locale-Maketext-Lexicon/t/5-extract.t	Fri Sep 12 11:33:44 2008
@@ -1,7 +1,7 @@
 #! /usr/bin/perl -w
 use lib '../lib';
 use strict;
-use Test::More tests => 43;
+use Test::More tests => 47;
 
 use_ok('Locale::Maketext::Extract');
 my $Ext = Locale::Maketext::Extract->new;
@@ -61,6 +61,12 @@
 extract_ok(q(_("","car"))                  => '',            'ignore empty string');
 extract_ok(q(_("0"))                       => '',            'ignore zero');
 
+extract_ok(<<'__EXAMPLE__'                 => 'foo bar baz',   'use FILTER (tt)');
+[% FILTER l -%]
+foo bar baz
+[%- END %]
+__EXAMPLE__
+
 extract_ok(<<'__EXAMPLE__'                 => 'foo bar baz',   'trim the string (tt)');
 [% |loc -%]
 foo bar baz
@@ -173,6 +179,51 @@
 msgstr ""
 __EXPECTED__
 
+write_po_ok(<<'__EXAMPLE__'                => <<'__EXPECTED__', "concat (heredoc)");
+_('exam'.<<"", 10)
+ple1 %1
+
+__EXAMPLE__
+#: :1
+#. (10)
+msgid "example1 %1\n"
+msgstr ""
+__EXPECTED__
+
+write_po_ok(<<'__EXAMPLE__'                => <<'__EXPECTED__', "two _() calls with concat over multiline (heredoc)");
+_('example' . 
+<<"", 10)
+1 %1
+
+_(<<"", 5)
+example2 %1
+
+__EXAMPLE__
+#: :1
+#. (10)
+msgid "example1 %1\n"
+msgstr ""
+
+#: :5
+#. (5)
+msgid "example2 %1\n"
+msgstr ""
+__EXPECTED__
+
+write_po_ok(<<'__EXAMPLE__'                => <<'__EXPECTED__', "i can concat the world!");
+_(
+'\$foo'
+."\$bar"
+.<<''
+\$baz
+
+)
+__EXAMPLE__
+#: :2
+msgid "\\$foo$bar\\$baz\n"
+msgstr ""
+__EXPECTED__
+
 sub extract_ok {
     my ($text, $expected, $info, $verbatim) = @_;
     $Ext->extract('' => $text);



More information about the Bps-public-commit mailing list