Skip to content

Commit

Permalink
bug #29961 [Translation] Concatenated translation messages (Stadly)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.4 branch (closes #29961).

Discussion
----------

[Translation] Concatenated translation messages

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29956
| License       | MIT
| Doc PR        | none

Concatenated translation messages are now treated correctly.

Commits
-------

df73ebf [Translation] Concatenated translation messages
  • Loading branch information
nicolas-grekas committed Jan 25, 2019
2 parents 765539b + df73ebf commit 13d1d76
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 13d1d76

Please sign in to comment.