Skip to content

Commit ff33768

Browse files
committed
bug #21359 [FrameworkBundle] fixed custom domain for translations in php templates (robinlehrmann)
This PR was merged into the 2.7 branch. Discussion ---------- [FrameworkBundle] fixed custom domain for translations in php templates | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20135 | License | MIT | Doc PR | Commits ------- 78c0ec5 [FrameworkBundle] fixed custom domain for translations in php templates
2 parents 01a0250 + 78c0ec5 commit ff33768

File tree

3 files changed

+102
-18
lines changed

3 files changed

+102
-18
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/views/translation.html.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@
3131
10,
3232
array('%count%' => 10)
3333
) ?>
34+
35+
<?php echo $view['translator']->trans('other-domain-test-no-params-short-array', [], 'not_messages'); ?>
36+
37+
<?php echo $view['translator']->trans('other-domain-test-no-params-long-array', array(), 'not_messages'); ?>
38+
39+
<?php echo $view['translator']->trans('other-domain-test-params-short-array', ['foo' => 'bar'], 'not_messages'); ?>
40+
41+
<?php echo $view['translator']->trans('other-domain-test-params-long-array', array('foo' => 'bar'), 'not_messages'); ?>
42+
43+
<?php echo $view['translator']->transChoice('other-domain-test-trans-choice-short-array-%count%', 10, ['%count%' => 10], 'not_messages'); ?>
44+
45+
<?php echo $view['translator']->transChoice('other-domain-test-trans-choice-long-array-%count%', 10, array('%count%' => 10), 'not_messages'); ?>
46+
47+
<?php echo $view['translator']->trans('typecast', ['a' => (int) '123'], 'not_messages'); ?>
48+
<?php echo $view['translator']->transChoice('msg1', 10 + 1, [], 'not_messages'); ?>
49+
<?php echo $view['translator']->transChoice('msg2', intval(4.5), [], 'not_messages'); ?>

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/PhpExtractorTest.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,31 @@ public function testExtraction($resource)
3939
nowdoc key with whitespace and nonescaped \$\n sequences
4040
EOF;
4141
// Assert
42-
$expectedCatalogue = array('messages' => array(
43-
'single-quoted key' => 'prefixsingle-quoted key',
44-
'double-quoted key' => 'prefixdouble-quoted key',
45-
'heredoc key' => 'prefixheredoc key',
46-
'nowdoc key' => 'prefixnowdoc key',
47-
"double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
48-
'single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \$\n\' sequences',
49-
'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
50-
$expectedHeredoc => 'prefix'.$expectedHeredoc,
51-
$expectedNowdoc => 'prefix'.$expectedNowdoc,
52-
'{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',
53-
));
42+
$expectedCatalogue = array(
43+
'messages' => array(
44+
'single-quoted key' => 'prefixsingle-quoted key',
45+
'double-quoted key' => 'prefixdouble-quoted key',
46+
'heredoc key' => 'prefixheredoc key',
47+
'nowdoc key' => 'prefixnowdoc key',
48+
"double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
49+
'single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \$\n\' sequences',
50+
'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
51+
$expectedHeredoc => 'prefix'.$expectedHeredoc,
52+
$expectedNowdoc => 'prefix'.$expectedNowdoc,
53+
'{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',
54+
),
55+
'not_messages' => array(
56+
'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
57+
'other-domain-test-no-params-long-array' => 'prefixother-domain-test-no-params-long-array',
58+
'other-domain-test-params-short-array' => 'prefixother-domain-test-params-short-array',
59+
'other-domain-test-params-long-array' => 'prefixother-domain-test-params-long-array',
60+
'other-domain-test-trans-choice-short-array-%count%' => 'prefixother-domain-test-trans-choice-short-array-%count%',
61+
'other-domain-test-trans-choice-long-array-%count%' => 'prefixother-domain-test-trans-choice-long-array-%count%',
62+
'typecast' => 'prefixtypecast',
63+
'msg1' => 'prefixmsg1',
64+
'msg2' => 'prefixmsg2',
65+
),
66+
);
5467
$actualCatalogue = $catalogue->all();
5568

5669
$this->assertEquals($expectedCatalogue, $actualCatalogue);

src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
2525
{
2626
const MESSAGE_TOKEN = 300;
27+
const METHOD_ARGUMENTS_TOKEN = 1000;
28+
const DOMAIN_TOKEN = 1001;
2729

2830
/**
2931
* Prefix for new found message.
@@ -38,6 +40,28 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
3840
* @var array
3941
*/
4042
protected $sequences = array(
43+
array(
44+
'->',
45+
'trans',
46+
'(',
47+
self::MESSAGE_TOKEN,
48+
',',
49+
self::METHOD_ARGUMENTS_TOKEN,
50+
',',
51+
self::DOMAIN_TOKEN,
52+
),
53+
array(
54+
'->',
55+
'transChoice',
56+
'(',
57+
self::MESSAGE_TOKEN,
58+
',',
59+
self::METHOD_ARGUMENTS_TOKEN,
60+
',',
61+
self::METHOD_ARGUMENTS_TOKEN,
62+
',',
63+
self::DOMAIN_TOKEN,
64+
),
4165
array(
4266
'->',
4367
'trans',
@@ -105,11 +129,32 @@ private function seekToNextRelevantToken(\Iterator $tokenIterator)
105129
}
106130
}
107131

132+
private function skipMethodArgument(\Iterator $tokenIterator)
133+
{
134+
$openBraces = 0;
135+
136+
for (; $tokenIterator->valid(); $tokenIterator->next()) {
137+
$t = $tokenIterator->current();
138+
139+
if ('[' === $t[0] || '(' === $t[0]) {
140+
++$openBraces;
141+
}
142+
143+
if (']' === $t[0] || ')' === $t[0]) {
144+
--$openBraces;
145+
}
146+
147+
if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) {
148+
break;
149+
}
150+
}
151+
}
152+
108153
/**
109154
* Extracts the message from the iterator while the tokens
110155
* match allowed message tokens.
111156
*/
112-
private function getMessage(\Iterator $tokenIterator)
157+
private function getValue(\Iterator $tokenIterator)
113158
{
114159
$message = '';
115160
$docToken = '';
@@ -155,24 +200,34 @@ protected function parseTokens($tokens, MessageCatalogue $catalog)
155200
for ($key = 0; $key < $tokenIterator->count(); ++$key) {
156201
foreach ($this->sequences as $sequence) {
157202
$message = '';
203+
$domain = 'messages';
158204
$tokenIterator->seek($key);
159205

160-
foreach ($sequence as $item) {
206+
foreach ($sequence as $sequenceKey => $item) {
161207
$this->seekToNextRelevantToken($tokenIterator);
162208

163-
if ($this->normalizeToken($tokenIterator->current()) == $item) {
209+
if ($this->normalizeToken($tokenIterator->current()) === $item) {
164210
$tokenIterator->next();
165211
continue;
166-
} elseif (self::MESSAGE_TOKEN == $item) {
167-
$message = $this->getMessage($tokenIterator);
212+
} elseif (self::MESSAGE_TOKEN === $item) {
213+
$message = $this->getValue($tokenIterator);
214+
215+
if (count($sequence) === ($sequenceKey + 1)) {
216+
break;
217+
}
218+
} elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
219+
$this->skipMethodArgument($tokenIterator);
220+
} elseif (self::DOMAIN_TOKEN === $item) {
221+
$domain = $this->getValue($tokenIterator);
222+
168223
break;
169224
} else {
170225
break;
171226
}
172227
}
173228

174229
if ($message) {
175-
$catalog->set($message, $this->prefix.$message);
230+
$catalog->set($message, $this->prefix.$message, $domain);
176231
break;
177232
}
178233
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy