Skip to content

Commit

Permalink
Merge pull request #84 from melmothx/topic/gh-83
Browse files Browse the repository at this point in the history
Collapse whitespace before i18n
  • Loading branch information
racke committed Mar 3, 2015
2 parents 826240d + 9eeda98 commit afe0a40
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ t/files/i18n.html
t/files/i18n.xml
t/i18n/02-includes.t
t/i18n/03-attributes.t
t/i18n/04-whitespace.t
t/testfiles/bad/admin.html
t/testfiles/bad/admin.xml
t/testfiles/bad/checkout-giftinfo.html
Expand Down
5 changes: 5 additions & 0 deletions lib/Template/Flute/HTML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ sub _translate_string {
}
# return undef if no text is left
return unless length($text);

# collapse the whitespace inside, discarding it for the
# purpose of localization.
$text =~ s/\s+/ /g;

# translate and restore spaces
return $ws_before . $i18n->localize($text) . $ws_after;
}
Expand Down
49 changes: 49 additions & 0 deletions t/i18n/04-whitespace.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!perl

use strict;
use warnings;
use utf8;

use Test::More tests => 1;

use Template::Flute;
use Template::Flute::I18N;

my %lexicon = (
"Enter your username..." => "Inserisci il nome utente...",
"Submit result" => "Invia i risultati",
"Title" => "Titolo",
"Do-not-translate" => "FAIL",
"Please insert your username here and we will send you a reset link." => "Inserisci il tuo nome utente e ti manderemo le istruzioni",
);

sub translate {
my $text = shift;
return $lexicon{$text};
};

my $i18n = Template::Flute::I18N->new(\&translate);

my $spec = '<specification></specification>';
my $template =<<HTML;
<html>
<body>
<div>
<p>
Please insert your username here and we will send you a reset
link.
</p>
</div>
</body>
</html>
HTML

my $flute = Template::Flute->new(specification => $spec,
template => $template,
i18n => $i18n);
my $output = $flute->process();

# diag $output;

like $output, qr{\sInserisci il tuo nome utente e ti manderemo le istruzioni\s},
"White space collapsed and string translated";

0 comments on commit afe0a40

Please sign in to comment.