8000 [TwigBundle] made trans and transchoice tags more flexible · brtriver/symfony@eff1bdf · GitHub
[go: up one dir, main page]

Skip to content

Commit eff1bdf

Browse files
committed
[TwigBundle] made trans and transchoice tags more flexible
Both tags accept variables now: {% trans label %} {% transchoice %} {{ error }} {% endtranschoice %} Optionally, the with keywords allows to pass the placeholder values: {% trans label with vars %}
1 parent 4297609 commit eff1bdf

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

src/Symfony/Bundle/TwigBundle/Node/TransNode.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
class TransNode extends \Twig_Node
2020
{
21-
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, $lineno, $tag = null)
21+
public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, $lineno, $tag = null)
2222
{
23-
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain), array(), $lineno, $tag);
23+
parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array(), $lineno, $tag);
2424
}
2525

2626
/**
@@ -32,7 +32,12 @@ public function compile($compiler)
3232
{
3333
$compiler->addDebugInfo($this);
3434

35-
list($msg, $vars) = $this->compileString($this->body);
35+
if ($this->isSimpleString($this->body)) {
36+
list($msg, $vars) = $this->compileString($this->body);
37+
} else {
38+
$msg = $this->body;
39+
$vars = $this->vars;
40+
}
3641

3742
$method = null === $this->count ? 'trans' : 'transChoice';
3843

@@ -52,13 +57,19 @@ public function compile($compiler)
5257

5358
$compiler->raw('array(');
5459

55-
foreach ($vars as $var) {
56-
$compiler
57-
->string('{{ '.$var['name'].' }}')
58-
->raw(' => ')
59-
->subcompile($var)
60-
->raw(', ')
61-
;
60+
if (is_array($vars)) {
61+
foreach ($vars as $var) {
62+
$compiler
63+
->string('{{ '.$var['name'].' }}')
64+
->raw(' => ')
65+
->subcompile($var)
66+
->raw(', ')
67+
;
68+
}
69+
} elseif (null !== $vars) {
70+
$compiler->subcompile($vars);
71+
} else {
72+
$compiler->raw('array()');
6273
}
6374

6475
$compiler
@@ -91,4 +102,21 @@ protected function compileString(\Twig_NodeInterface $body)
91102

92103
return array(new \Twig_Node(array(new \Twig_Node_Expression_Constant(trim($msg), $node->getLine()))), $vars);
93104
}
105+
106+
protected function isSimpleString(\Twig_NodeInterface $body)
107+
{
108+
foreach ($body as $i => $node) {
109+
if (
110+
$node instanceof \Twig_Node_Text
111+
||
112+
($node instanceof \Twig_Node_Print && $node->expr instanceof \Twig_Node_Expression_Name)
113+
) {
114+
continue;
115+
}
116+
117+
return false;
118+
}
119+
120+
return true;
121+
}
94122
}

src/Symfony/Bundle/TwigBundle/TokenParser/TransChoiceTokenParser.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,20 @@ public function parse(\Twig_Token $token)
3232
$lineno = $token->getLine();
3333
$stream = $this->parser->getStream();
3434

35+
$vars = null;
36+
3537
$count = $this->parser->getExpressionParser()->parseExpression();
3638

3739
$domain = new \Twig_Node_Expression_Constant('messages', $lineno);
38-
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test('from')) {
40+
41+
if ($stream->test('with')) {
42+
// {% transchoice count with vars %}
43+
$stream->next();
44+
$vars = $this->parser->getExpressionParser()->parseExpression();
45+
}
46+
47+
if ($stream->test('from')) {
48+
// {% transchoice count from "messages" %}
3949
$stream->next();
4050
$domain = $this->parser->getExpressionParser()->parseExpression();
4151
}
@@ -46,9 +56,7 @@ public function parse(\Twig_Token $token)
4656

4757
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
4858

49-
$this->checkTransString($body, $lineno);
50-
51-
return new TransNode($body, $domain, $count, $lineno, $this->getTag());
59+
return new TransNode($body, $domain, $count, $vars, $lineno, $this->getTag());
5260
}
5361

5462
public function decideTransChoiceFork($token)

src/Symfony/Bundle/TwigBundle/TokenParser/TransTokenParser.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,22 @@ public function parse(\Twig_Token $token)
3232
$lineno = $token->getLine();
3333
$stream = $this->parser->getStream();
3434

35+
$vars = null;
3536
$domain = new \Twig_Node_Expression_Constant('messages', $lineno);
3637
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
3738
$body = null;
3839
if (!$stream->test('from')) {
3940
// {% trans "message" %}
41+
// {% trans message %}
4042
$body = $this->parser->getExpressionParser()->parseExpression();
4143
}
4244

45+
if ($stream->test('with')) {
46+
// {% trans "message" with vars %}
47+
$stream->next();
48+
$vars = $this->parser->getExpressionParser()->parseExpression();
49+
}
50+
4351
if ($stream->test('from')) {
4452
// {% trans "message" from "messages" %}
4553
$stream->next();
@@ -61,9 +69,7 @@ public function parse(\Twig_Token $token)
6169

6270
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
6371

64-
$this->checkTransString($body, $lineno);
65-
66-
return new TransNode($body, $domain, null, $lineno, $this->getTag());
72+
return new TransNode($body, $domain, null, $vars, $lineno, $this->getTag());
6773
}
6874

6975
public function decideTransFork($token)
@@ -80,19 +86,4 @@ public function getTag()
8086
{
8187
return 'trans';
8288
}
83-
84-
protected function checkTransString(\Twig_NodeInterface $body, $lineno)
85-
{
86-
foreach ($body as $i => $node) {
87-
if (
88-
$node instanceof \Twig_Node_Text
89-
||
90-
($node instanceof \Twig_Node_Print && $node->expr instanceof \Twig_Node_Expression_Name)
91-
) {
92-
continue;
93-
}
94-
95-
throw new \Twig_SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
96-
}
97-
}
9889
}

0 commit comments

Comments
 (0)
0