bug #40318 [Translation] deal with indented heredoc/nowdoc tokens (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Translation] deal with indented heredoc/nowdoc tokens

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #40278
| License       | MIT
| Doc PR        |

Commits
-------

4721097cab deal with indented heredoc/nowdoc tokens
This commit is contained in:
Fabien Potencier 2021-03-04 16:39:09 +01:00
commit 218bf5ea9a
3 changed files with 47 additions and 0 deletions

View File

@ -178,6 +178,19 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
}
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 = '';

View File

@ -75,6 +75,27 @@ EOF;
$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/';

View File

@ -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
); ?>