8000 [Translation] deal with indented heredoc/nowdoc tokens by xabbuh · Pull Request #40318 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] deal with indented heredoc/nowdoc tokens #40318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Symfony/Component/Translation/Extractor/PhpExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ private function getValue(\Iterator $tokenIterator)
}
break;
case \T_END_HEREDOC:
if ($indentation = strspn($t[1], ' ')) {
$docPartWithLineBreaks = $docPart;
$docPart = '';

foreach (preg_split('~(\r\n|\n|\r)~', $docPartWithLineBreaks, -1, \PREG_SPLIT_DELIM_CAPTURE) as $str) {
if (\in_array($str, ["\r\n", "\n", "\r"], true)) {
$docPart .= $str;
} else {
$docPart .= substr($str, $indentation);
}
}
}

$message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
$docToken = '';
$docPart = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ public function testExtraction($resource)
$this->assertEquals(['sources' => [$filename.':43']], $catalogue->getMetadata('other-domain-test-no-params-short-array', 'not_messages'));
}

/**
* @requires PHP 7.3
*/
public function testExtractionFromIndentedHeredocNowdoc()
{
$catalogue = new MessageCatalogue('en');

$extractor = new PhpExtractor();
$extractor->setPrefix('prefix');
$extractor->extract(__DIR__.'/../fixtures/extractor-7.3/translation.html.php', $catalogue);

$expectedCatalogue = [
'messages' => [
"heredoc\nindented\n further" => "prefixheredoc\nindented\n further",
"nowdoc\nindented\n further" => "prefixnowdoc\nindented\n further",
],
];

$this->assertEquals($expectedCatalogue, $catalogue->all());
}

public function resourcesProvider()
{
$directory = __DIR__.'/../fixtures/extractor/';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This template is used for translation message extraction tests
<?php echo $view['translator']->trans(<<<EOF
heredoc
indented
further
EOF
); ?>
<?php echo $view['translator']->trans(<<<'EOF'
nowdoc
indented
further
EOF
); ?>
0