Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 218bf5e

Browse filesBrowse files
committed
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 ------- 4721097 deal with indented heredoc/nowdoc tokens
2 parents 37506f3 + 4721097 commit 218bf5e
Copy full SHA for 218bf5e

File tree

3 files changed

+47
-0
lines changed
Filter options

3 files changed

+47
-0
lines changed

‎src/Symfony/Component/Translation/Extractor/PhpExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Extractor/PhpExtractor.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ private function getValue(\Iterator $tokenIterator)
178178
}
179179
break;
180180
case \T_END_HEREDOC:
181+
if ($indentation = strspn($t[1], ' ')) {
182+
$docPartWithLineBreaks = $docPart;
183+
$docPart = '';
184+
185+
foreach (preg_split('~(\r\n|\n|\r)~', $docPartWithLineBreaks, -1, \PREG_SPLIT_DELIM_CAPTURE) as $str) {
186+
if (\in_array($str, ["\r\n", "\n", "\r"], true)) {
187+
$docPart .= $str;
188+
} else {
189+
$docPart .= substr($str, $indentation);
190+
}
191+
}
192+
}
193+
181194
$message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
182195
$docToken = '';
183196
$docPart = '';

‎src/Symfony/Component/Translation/Tests/Extractor/PhpExtractorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/Extractor/PhpExtractorTest.php
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ public function testExtraction($resource)
7575
$this->assertEquals(['sources' => [$filename.':43']], $catalogue->getMetadata('other-domain-test-no-params-short-array', 'not_messages'));
7676
}
7777

78+
/**
79+
* @requires PHP 7.3
80+
*/
81+
public function testExtractionFromIndentedHeredocNowdoc()
82+
{
83+
$catalogue = new MessageCatalogue('en');
84+
85+
$extractor = new PhpExtractor();
86+
$extractor->setPrefix('prefix');
87+
$extractor->extract(__DIR__.'/../fixtures/extractor-7.3/translation.html.php', $catalogue);
88+
89+
$expectedCatalogue = [
90+
'messages' => [
91+
"heredoc\nindented\n further" => "prefixheredoc\nindented\n further",
92+
"nowdoc\nindented\n further" => "prefixnowdoc\nindented\n further",
93+
],
94+
];
95+
96+
$this->assertEquals($expectedCatalogue, $catalogue->all());
97+
}
98+
7899
public function resourcesProvider()
79100
{
80101
$directory = __DIR__.'/../fixtures/extractor/';
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This template is used for translation message extraction tests
2+
<?php echo $view['translator']->trans(<<<EOF
3+
heredoc
4+
indented
5+
further
6+
EOF
7+
); ?>
8+
<?php echo $view['translator']->trans(<<<'EOF'
9+
nowdoc
10+
indented
11+
further
12+
EOF
13+
); ?>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.