Skip to content

Commit

Permalink
[Translation] Concatenated translation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Stadly authored and nicolas-grekas committed Jan 25, 2019
1 parent aca3d2c commit df73ebf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Symfony/Component/Translation/Extractor/PhpExtractor.php
Expand Up @@ -156,9 +156,14 @@ private function getValue(\Iterator $tokenIterator)
{
$message = '';
$docToken = '';
$docPart = '';

for (; $tokenIterator->valid(); $tokenIterator->next()) {
$t = $tokenIterator->current();
if ('.' === $t) {
// Concatenate with next token
continue;
}
if (!isset($t[1])) {
break;
}
Expand All @@ -169,19 +174,24 @@ private function getValue(\Iterator $tokenIterator)
break;
case T_ENCAPSED_AND_WHITESPACE:
case T_CONSTANT_ENCAPSED_STRING:
$message .= $t[1];
if ('' === $docToken) {
$message .= PhpStringTokenParser::parse($t[1]);
} else {
$docPart = $t[1];
}
break;
case T_END_HEREDOC:
return PhpStringTokenParser::parseDocString($docToken, $message);
$message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
$docToken = '';
$docPart = '';
break;
case T_WHITESPACE:
break;
default:
break 2;
}
}

if ($message) {
$message = PhpStringTokenParser::parse($message);
}

return $message;
}

Expand Down
Expand Up @@ -51,6 +51,7 @@ public function testExtraction($resource)
$expectedHeredoc => 'prefix'.$expectedHeredoc,
$expectedNowdoc => 'prefix'.$expectedNowdoc,
'{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples' => 'prefix{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
'concatenated message with heredoc and nowdoc' => 'prefixconcatenated message with heredoc and nowdoc',
],
'not_messages' => [
'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
Expand Down
Expand Up @@ -32,6 +32,14 @@
['%count%' => 10]
); ?>

<?php echo $view['translator']->trans('concatenated'.' message'.<<<EOF
with heredoc
EOF
.<<<'EOF'
and nowdoc
EOF
); ?>

<?php echo $view['translator']->trans('other-domain-test-no-params-short-array', [], 'not_messages'); ?>

<?php echo $view['translator']->trans('other-domain-test-no-params-long-array', [], 'not_messages'); ?>
Expand Down

0 comments on commit df73ebf

Please sign in to comment.