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 47bc809

Browse filesBrowse files
committed
[TwigBundle] added tests for trans tag and filter
1 parent 7712528 commit 47bc809
Copy full SHA for 47bc809

File tree

Expand file treeCollapse file tree

3 files changed

+114
-26
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+114
-26
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Node/TransNode.php
+19-11Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public function compile($compiler)
3232
{
3333
$compiler->addDebugInfo($this);
3434

35+
$defaults = null;
3536
if ($this->getAttribute('is_simple')) {
36-
list($msg, $vars) = $this->compileString($this->getNode('body'));
37+
list($msg, $defaults) = $this->compileString($this->getNode('body'));
3738
} else {
3839
$msg = $this->getNode('body');
39-
$vars = $this->getNode('vars');
4040
}
4141

4242
$method = null === $this->getNode('count') ? 'trans' : 'transChoice';
@@ -55,25 +55,33 @@ public function compile($compiler)
5555
;
5656
}
5757

58-
$compiler->raw('array(');
58+
$compiler->raw('array_merge(');
5959

60-
if (is_array($vars)) {
61-
foreach ($vars as $var) {
60+
if (null === $defaults) {
61+
$compiler->raw('array()');
62+
} else {
63+
$compiler->raw('array(');
64+
foreach ($defaults as $default) {
6265
$compiler
63-
->string('{{ '.$var['name'].' }}')
66+
->string('{{ '.$default->getAttribute('name').' }}')
6467
->raw(' => ')
65-
->subcompile($var)
68+
->subcompile($default)
6669
->raw(', ')
6770
;
6871
}
69-
} elseif (null !== $vars) {
70-
$compiler->subcompile($vars);
71-
} else {
72+
$compiler->raw(')');
73+
}
74+
75+
$compiler->raw(', ');
76+
77+
if (null === $this->getNode('vars')) {
7278
$compiler->raw('array()');
79+
} else {
80+
$compiler->subcompile($this->getNode('vars'));
7381
}
7482

7583
$compiler
76-
->raw("), ")
84+
->raw('), ')
7785
->subcompile($this->getNode('domain'))
7886
->raw(");\n")
7987
;
+86Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle\Tests;
13+
14+
use Symfony\Bundle\TwigBundle\Tests\TestCase;
15+
use Symfony\Bundle\TwigBundle\Extension\TransExtension;
16+
use Symfony\Component\Translation\Translator;
17+
use Symfony\Component\Translation\MessageSelector;
18+
19+
class TransTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider getTransTests
23+
*/
24+
public function testTrans($template, $expected, array $variables = array())
25+
{
26+
27+
if ($expected != $this->getTemplate($template)->render($variables)) {
28+
print $template."\n";
29+
$loader = new \Twig_Loader_Array(array('index' => $template));
30+
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
31+
$twig->addExtension(new TransExtension(new Translator('en', new MessageSelector())));
32+
33+
echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n";
34+
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
35+
exit();
36+
}
37+
38+
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
39+
}
40+
41+
public function getTransTests()
42+
{
43+
return array(
44+
// trans tag
45+
array('{% trans "Hello" %}', 'Hello'),
46+
array('{% trans name %}', 'Symfony2', array('name' => 'Symfony2')),
47+
array('{% trans hello with [\'{{ name }}\': \'Symfony2\'] %}', 'Hello Symfony2', array('hello' => 'Hello {{ name }}')),
48+
array('{% set vars = [\'{{ name }}\': \'Symfony2\'] %}{% trans hello with vars %}', 'Hello Symfony2', array('hello' => 'Hello {{ name }}')),
49+
50+
array('{% trans %}Hello{% endtrans %}', 'Hello'),
51+
array('{% trans %}{{ name }}{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),
52+
53+
array('{% trans "Hello" from elsewhere %}', 'Hello'),
54+
array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
55+
56+
array('{% trans %}Hello {{ name }}{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')),
57+
array('{% trans with [\'{{ name }}\': \'Symfony2\'] %}Hello {{ name }}{% endtrans %}', 'Hello Symfony2'),
58+
array('{% set vars = [\'{{ name }}\': \'Symfony2\'] %}{% trans with vars %}Hello {{ name }}{% endtrans %}', 'Hello Symfony2'),
59+
60+
// transchoice
61+
array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples{% endtranschoice %}',
62+
'There is no apples', array('count' => 0)),
63+
array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples{% endtranschoice %}',
64+
'There is 5 apples', array('count' => 5)),
65+
array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples ({{ name }}){% endtranschoice %}',
66+
'There is 5 apples (Symfony2)', array('count' => 5, 'name' => 'Symfony2')),
67+
array('{% transchoice count with [\'{{ name }}\': \'Symfony2\'] %}{0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples ({{ name }}){% endtranschoice %}',
68+
'There is 5 apples (Symfony2)', array('count' => 5)),
69+
70+
// trans filter
71+
array('{{ "Hello"|trans }}', 'Hello'),
72+
array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
73+
array('{{ hello|trans([\'{{ name }}\': \'Symfony2\']) }}', 'Hello Symfony2', array('hello' => 'Hello {{ name }}')),
74+
array('{% set vars = [\'{{ name }}\': \'Symfony2\'] %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello {{ name }}')),
75+
);
76+
}
77+
78+
protected function getTemplate($template)
79+
{
80+
$loader = new \Twig_Loader_Array(array('index' => $template));
81+
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
82+
$twig->addExtension(new TransExtension(new Translator('en', new MessageSelector())));
83+
84+
return $twig->loadTemplate('index');
85+
}
86+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/TokenParser/TransTokenParser.php
+9-15Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ public function parse(\Twig_Token $token)
3333
$stream = $this->parser->getStream();
3434

3535
$vars = null;
36+
$body = null;
37+
$isSimple = false;
3638
$domain = new \Twig_Node_Expression_Constant('messages', $lineno);
3739
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
38-
$body = null;
39-
if (!$stream->test('from')) {
40+
if (!$stream->test('from') && !$stream->test('with')) {
4041
// {% trans "message" %}
4142
// {% trans message %}
4243
$body = $this->parser->getExpressionParser()->parseExpression();
@@ -52,24 +53,21 @@ public function parse(\Twig_Token $token)
5253
// {% trans "message" from "messages" %}
5354
$stream->next();
5455
$domain = $this->parser->getExpressionParser()->parseExpression();
55-
56-
// {% trans from "messages" %}message{% endtrans %}
57-
if (null === $body) {
58-
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
59-
$body = $this->parser->subparse(array($this, 'decideTransFork'), true);
60-
}
6156
} elseif (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
6257
throw new \Twig_SyntaxError(sprintf('Unexpected token. Twig was looking for the "from" keyword line %s)', $lineno), -1);
6358
}
64-
} else {
59+
}
60+
61+
if (null === $body) {
6562
// {% trans %}message{% endtrans %}
6663
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
6764
$body = $this->parser->subparse(array($this, 'decideTransFork'), true);
65+
$isSimple = $this->isSimpleString($body);
6866
}
6967

7068
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
7169

72-
return new TransNode($body, $domain, null, $vars, $this->isSimpleString($body), $lineno, $this->getTag());
70+
return new TransNode($body, $domain, null, $vars, $isSimple, $lineno, $this->getTag());
7371
}
7472

7573
public function decideTransFork($token)
@@ -79,15 +77,11 @@ public function decideTransFork($token)
7977

8078
protected function isSimpleString(\Twig_NodeInterface $body)
8179
{
82-
if (0 === count($body)) {
83-
return false;
84-
}
85-
8680
foreach ($body as $i => $node) {
8781
if (
8882
$node instanceof \Twig_Node_Text
8983
||
90-
($node instanceof \Twig_Node_Print && $node->expr instanceof \Twig_Node_Expression_Name)
84+
($node instanceof \Twig_Node_Print && $node->getNode('expr') instanceof \Twig_Node_Expression_Name)
9185
) {
9286
continue;
9387
}

0 commit comments

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