Skip to content

Commit bce6a5a

Browse files
committed
[Translation] Fix extracting qualified t() function calls
1 parent 637c511 commit bce6a5a

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

Extractor/Visitor/TransMethodVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function leaveNode(Node $node): ?Node
3939
return null;
4040
}
4141

42-
$name = (string) $node->name;
42+
$name = $node->name instanceof Node\Name ? $node->name->getLast() : (string) $node->name;
4343

4444
if ('trans' === $name || 't' === $name) {
4545
$firstNamedArgumentIndex = $this->nodeFirstNamedArgumentIndex($node);

Tests/Extractor/PhpAstExtractorTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ public function testExtraction(iterable|string $resource)
8282
'translatable-short '.$expectedNowdoc => 'prefixtranslatable-short '.$expectedNowdoc,
8383
'translatable-short concatenated message with heredoc and nowdoc' => 'prefixtranslatable-short concatenated message with heredoc and nowdoc',
8484
'translatable-short default domain' => 'prefixtranslatable-short default domain',
85+
'translatable-short-fqn single-quoted key' => 'prefixtranslatable-short-fqn single-quoted key',
86+
'translatable-short-fqn double-quoted key' => 'prefixtranslatable-short-fqn double-quoted key',
87+
'translatable-short-fqn heredoc key' => 'prefixtranslatable-short-fqn heredoc key',
88+
'translatable-short-fqn nowdoc key' => 'prefixtranslatable-short-fqn nowdoc key',
89+
"translatable-short-fqn double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixtranslatable-short-fqn double-quoted key with whitespace and escaped \$\n\" sequences",
90+
'translatable-short-fqn single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixtranslatable-short-fqn single-quoted key with whitespace and nonescaped \$\n\' sequences',
91+
'translatable-short-fqn single-quoted key with "quote mark at the end"' => 'prefixtranslatable-short-fqn single-quoted key with "quote mark at the end"',
92+
'translatable-short-fqn '.$expectedHeredoc => 'prefixtranslatable-short-fqn '.$expectedHeredoc,
93+
'translatable-short-fqn '.$expectedNowdoc => 'prefixtranslatable-short-fqn '.$expectedNowdoc,
94+
'translatable-short-fqn concatenated message with heredoc and nowdoc' => 'prefixtranslatable-short-fqn concatenated message with heredoc and nowdoc',
95+
'translatable-short-fqn default domain' => 'prefixtranslatable-short-fqn default domain',
8596
'single-quoted key' => 'prefixsingle-quoted key',
8697
'double-quoted key' => 'prefixdouble-quoted key',
8798
'heredoc key' => 'prefixheredoc key',
@@ -113,6 +124,11 @@ public function testExtraction(iterable|string $resource)
113124
'translatable-short other-domain-test-params-short-array' => 'prefixtranslatable-short other-domain-test-params-short-array',
114125
'translatable-short other-domain-test-params-long-array' => 'prefixtranslatable-short other-domain-test-params-long-array',
115126
'translatable-short typecast' => 'prefixtranslatable-short typecast',
127+
'translatable-short-fqn other-domain-test-no-params-short-array' => 'prefixtranslatable-short-fqn other-domain-test-no-params-short-array',
128+
'translatable-short-fqn other-domain-test-no-params-long-array' => 'prefixtranslatable-short-fqn other-domain-test-no-params-long-array',
129+
'translatable-short-fqn other-domain-test-params-short-array' => 'prefixtranslatable-short-fqn other-domain-test-params-short-array',
130+
'translatable-short-fqn other-domain-test-params-long-array' => 'prefixtranslatable-short-fqn other-domain-test-params-long-array',
131+
'translatable-short-fqn typecast' => 'prefixtranslatable-short-fqn typecast',
116132
'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
117133
'other-domain-test-no-params-long-array' => 'prefixother-domain-test-no-params-long-array',
118134
'other-domain-test-params-short-array' => 'prefixother-domain-test-params-short-array',
@@ -159,6 +175,10 @@ public function testExtraction(iterable|string $resource)
159175
$this->assertEquals(['sources' => [$filename.':2']], $catalogue->getMetadata('translatable-short single-quoted key'));
160176
$this->assertEquals(['sources' => [$filename.':37']], $catalogue->getMetadata('translatable-short other-domain-test-no-params-short-array', 'not_messages'));
161177

178+
$filename = str_replace(\DIRECTORY_SEPARATOR, '/', __DIR__).'/../Fixtures/extractor-ast/translatable-short-fqn.html.php';
179+
$this->assertEquals(['sources' => [$filename.':2']], $catalogue->getMetadata('translatable-short-fqn single-quoted key'));
180+
$this->assertEquals(['sources' => [$filename.':37']], $catalogue->getMetadata('translatable-short-fqn other-domain-test-no-params-short-array', 'not_messages'));
181+
162182
$filename = str_replace(\DIRECTORY_SEPARATOR, '/', __DIR__).'/../Fixtures/extractor-ast/translation.html.php';
163183
$this->assertEquals(['sources' => [$filename.':2']], $catalogue->getMetadata('single-quoted key'));
164184
$this->assertEquals(['sources' => [$filename.':37']], $catalogue->getMetadata('other-domain-test-no-params-short-array', 'not_messages'));
@@ -199,7 +219,7 @@ public static function resourcesProvider(): array
199219
if ($fileInfo->isDot()) {
200220
continue;
201221
}
202-
if (\in_array($fileInfo->getBasename(), ['translatable.html.php', 'translatable-fqn.html.php', 'translatable-short.html.php', 'translation.html.php', 'validator-constraints.php'], true)) {
222+
if (\in_array($fileInfo->getBasename(), ['translatable.html.php', 'translatable-fqn.html.php', 'translatable-short.html.php', 'translatable-short-fqn.html.php', 'translation.html.php', 'validator-constraints.php'], true)) {
203223
$phpFiles[] = $fileInfo->getPathname();
204224
}
205225
$splFiles[] = $fileInfo->getFileInfo();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
This template is used for translation message extraction tests
2+
<?php \Symfony\Component\Translation\t('translatable-short-fqn single-quoted key'); ?>
3+
<?php \Symfony\Component\Translation\t('translatable-short-fqn double-quoted key'); ?>
4+
<?php \Symfony\Component\Translation\t(<<<EOF
5+
translatable-short-fqn heredoc key
6+
EOF
7+
); ?>
8+
<?php \Symfony\Component\Translation\t(<<<'EOF'
9+
translatable-short-fqn nowdoc key
10+
EOF
11+
); ?>
12+
<?php \Symfony\Component\Translation\t(
13+
"translatable-short-fqn double-quoted key with whitespace and escaped \$\n\" sequences"
14+
); ?>
15+
<?php \Symfony\Component\Translation\t(
16+
'translatable-short-fqn single-quoted key with whitespace and nonescaped \$\n\' sequences'
17+
); ?>
18+
<?php \Symfony\Component\Translation\t(<<<EOF
19+
translatable-short-fqn heredoc key with whitespace and escaped \$\n sequences
20+
EOF
21+
); ?>
22+
<?php \Symfony\Component\Translation\t(<<<'EOF'
23+
translatable-short-fqn nowdoc key with whitespace and nonescaped \$\n sequences
24+
EOF
25+
); ?>
26+
27+
<?php \Symfony\Component\Translation\t('translatable-short-fqn single-quoted key with "quote mark at the end"'); ?>
28+
29+
<?php \Symfony\Component\Translation\t('translatable-short-fqn concatenated'.' message'.<<<EOF
30+
with heredoc
31+
EOF
32+
.<<<'EOF'
33+
and nowdoc
34+
EOF
35+
); ?>
36+
37+
<?php \Symfony\Component\Translation\t('translatable-short-fqn other-domain-test-no-params-short-array', [], 'not_messages'); ?>
38+
39+
<?php \Symfony\Component\Translation\t('translatable-short-fqn other-domain-test-no-params-long-array', [], 'not_messages'); ?>
40+
41+
<?php \Symfony\Component\Translation\t('translatable-short-fqn other-domain-test-params-short-array', ['foo' => 'bar'], 'not_messages'); ?>
42+
43+
<?php \Symfony\Component\Translation\t('translatable-short-fqn other-domain-test-params-long-array', ['foo' => 'bar'], 'not_messages'); ?>
44+
45+
<?php \Symfony\Component\Translation\t('translatable-short-fqn typecast', ['a' => (int) '123'], 'not_messages'); ?>
46+
47+
<?php \Symfony\Component\Translation\t('translatable-short-fqn default domain', [], null); ?>

0 commit comments

Comments
 (0)