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
-------
78c0ec5c13
[FrameworkBundle] fixed custom domain for translations in php templates
This commit is contained in:
commit
ff33768eda
@ -31,3 +31,19 @@ EOF
|
|||||||
10,
|
10,
|
||||||
array('%count%' => 10)
|
array('%count%' => 10)
|
||||||
) ?>
|
) ?>
|
||||||
|
|
||||||
|
<?php echo $view['translator']->trans('other-domain-test-no-params-short-array', [], 'not_messages'); ?>
|
||||||
|
|
||||||
|
<?php echo $view['translator']->trans('other-domain-test-no-params-long-array', array(), 'not_messages'); ?>
|
||||||
|
|
||||||
|
<?php echo $view['translator']->trans('other-domain-test-params-short-array', ['foo' => 'bar'], 'not_messages'); ?>
|
||||||
|
|
||||||
|
<?php echo $view['translator']->trans('other-domain-test-params-long-array', array('foo' => 'bar'), 'not_messages'); ?>
|
||||||
|
|
||||||
|
<?php echo $view['translator']->transChoice('other-domain-test-trans-choice-short-array-%count%', 10, ['%count%' => 10], 'not_messages'); ?>
|
||||||
|
|
||||||
|
<?php echo $view['translator']->transChoice('other-domain-test-trans-choice-long-array-%count%', 10, array('%count%' => 10), 'not_messages'); ?>
|
||||||
|
|
||||||
|
<?php echo $view['translator']->trans('typecast', ['a' => (int) '123'], 'not_messages'); ?>
|
||||||
|
<?php echo $view['translator']->transChoice('msg1', 10 + 1, [], 'not_messages'); ?>
|
||||||
|
<?php echo $view['translator']->transChoice('msg2', intval(4.5), [], 'not_messages'); ?>
|
||||||
|
@ -39,18 +39,31 @@ EOF;
|
|||||||
nowdoc key with whitespace and nonescaped \$\n sequences
|
nowdoc key with whitespace and nonescaped \$\n sequences
|
||||||
EOF;
|
EOF;
|
||||||
// Assert
|
// Assert
|
||||||
$expectedCatalogue = array('messages' => array(
|
$expectedCatalogue = array(
|
||||||
'single-quoted key' => 'prefixsingle-quoted key',
|
'messages' => array(
|
||||||
'double-quoted key' => 'prefixdouble-quoted key',
|
'single-quoted key' => 'prefixsingle-quoted key',
|
||||||
'heredoc key' => 'prefixheredoc key',
|
'double-quoted key' => 'prefixdouble-quoted key',
|
||||||
'nowdoc key' => 'prefixnowdoc key',
|
'heredoc key' => 'prefixheredoc key',
|
||||||
"double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
|
'nowdoc key' => 'prefixnowdoc key',
|
||||||
'single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \$\n\' sequences',
|
"double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
|
||||||
'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
|
'single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \$\n\' sequences',
|
||||||
$expectedHeredoc => 'prefix'.$expectedHeredoc,
|
'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
|
||||||
$expectedNowdoc => 'prefix'.$expectedNowdoc,
|
$expectedHeredoc => 'prefix'.$expectedHeredoc,
|
||||||
'{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',
|
$expectedNowdoc => 'prefix'.$expectedNowdoc,
|
||||||
));
|
'{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',
|
||||||
|
),
|
||||||
|
'not_messages' => array(
|
||||||
|
'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
|
||||||
|
'other-domain-test-no-params-long-array' => 'prefixother-domain-test-no-params-long-array',
|
||||||
|
'other-domain-test-params-short-array' => 'prefixother-domain-test-params-short-array',
|
||||||
|
'other-domain-test-params-long-array' => 'prefixother-domain-test-params-long-array',
|
||||||
|
'other-domain-test-trans-choice-short-array-%count%' => 'prefixother-domain-test-trans-choice-short-array-%count%',
|
||||||
|
'other-domain-test-trans-choice-long-array-%count%' => 'prefixother-domain-test-trans-choice-long-array-%count%',
|
||||||
|
'typecast' => 'prefixtypecast',
|
||||||
|
'msg1' => 'prefixmsg1',
|
||||||
|
'msg2' => 'prefixmsg2',
|
||||||
|
),
|
||||||
|
);
|
||||||
$actualCatalogue = $catalogue->all();
|
$actualCatalogue = $catalogue->all();
|
||||||
|
|
||||||
$this->assertEquals($expectedCatalogue, $actualCatalogue);
|
$this->assertEquals($expectedCatalogue, $actualCatalogue);
|
||||||
|
@ -24,6 +24,8 @@ use Symfony\Component\Translation\Extractor\ExtractorInterface;
|
|||||||
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
|
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
|
||||||
{
|
{
|
||||||
const MESSAGE_TOKEN = 300;
|
const MESSAGE_TOKEN = 300;
|
||||||
|
const METHOD_ARGUMENTS_TOKEN = 1000;
|
||||||
|
const DOMAIN_TOKEN = 1001;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prefix for new found message.
|
* Prefix for new found message.
|
||||||
@ -38,6 +40,28 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $sequences = array(
|
protected $sequences = array(
|
||||||
|
array(
|
||||||
|
'->',
|
||||||
|
'trans',
|
||||||
|
'(',
|
||||||
|
self::MESSAGE_TOKEN,
|
||||||
|
',',
|
||||||
|
self::METHOD_ARGUMENTS_TOKEN,
|
||||||
|
',',
|
||||||
|
self::DOMAIN_TOKEN,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'->',
|
||||||
|
'transChoice',
|
||||||
|
'(',
|
||||||
|
self::MESSAGE_TOKEN,
|
||||||
|
',',
|
||||||
|
self::METHOD_ARGUMENTS_TOKEN,
|
||||||
|
',',
|
||||||
|
self::METHOD_ARGUMENTS_TOKEN,
|
||||||
|
',',
|
||||||
|
self::DOMAIN_TOKEN,
|
||||||
|
),
|
||||||
array(
|
array(
|
||||||
'->',
|
'->',
|
||||||
'trans',
|
'trans',
|
||||||
@ -105,11 +129,32 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function skipMethodArgument(\Iterator $tokenIterator)
|
||||||
|
{
|
||||||
|
$openBraces = 0;
|
||||||
|
|
||||||
|
for (; $tokenIterator->valid(); $tokenIterator->next()) {
|
||||||
|
$t = $tokenIterator->current();
|
||||||
|
|
||||||
|
if ('[' === $t[0] || '(' === $t[0]) {
|
||||||
|
++$openBraces;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (']' === $t[0] || ')' === $t[0]) {
|
||||||
|
--$openBraces;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts the message from the iterator while the tokens
|
* Extracts the message from the iterator while the tokens
|
||||||
* match allowed message tokens.
|
* match allowed message tokens.
|
||||||
*/
|
*/
|
||||||
private function getMessage(\Iterator $tokenIterator)
|
private function getValue(\Iterator $tokenIterator)
|
||||||
{
|
{
|
||||||
$message = '';
|
$message = '';
|
||||||
$docToken = '';
|
$docToken = '';
|
||||||
@ -155,16 +200,26 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
|
|||||||
for ($key = 0; $key < $tokenIterator->count(); ++$key) {
|
for ($key = 0; $key < $tokenIterator->count(); ++$key) {
|
||||||
foreach ($this->sequences as $sequence) {
|
foreach ($this->sequences as $sequence) {
|
||||||
$message = '';
|
$message = '';
|
||||||
|
$domain = 'messages';
|
||||||
$tokenIterator->seek($key);
|
$tokenIterator->seek($key);
|
||||||
|
|
||||||
foreach ($sequence as $item) {
|
foreach ($sequence as $sequenceKey => $item) {
|
||||||
$this->seekToNextRelevantToken($tokenIterator);
|
$this->seekToNextRelevantToken($tokenIterator);
|
||||||
|
|
||||||
if ($this->normalizeToken($tokenIterator->current()) == $item) {
|
if ($this->normalizeToken($tokenIterator->current()) === $item) {
|
||||||
$tokenIterator->next();
|
$tokenIterator->next();
|
||||||
continue;
|
continue;
|
||||||
} elseif (self::MESSAGE_TOKEN == $item) {
|
} elseif (self::MESSAGE_TOKEN === $item) {
|
||||||
$message = $this->getMessage($tokenIterator);
|
$message = $this->getValue($tokenIterator);
|
||||||
|
|
||||||
|
if (count($sequence) === ($sequenceKey + 1)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
|
||||||
|
$this->skipMethodArgument($tokenIterator);
|
||||||
|
} elseif (self::DOMAIN_TOKEN === $item) {
|
||||||
|
$domain = $this->getValue($tokenIterator);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -172,7 +227,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($message) {
|
if ($message) {
|
||||||
$catalog->set($message, $this->prefix.$message);
|
$catalog->set($message, $this->prefix.$message, $domain);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user