Skip to content

Commit 02bc23a

Browse files
aitboudadfabpot
authored andcommitted
[Twig][Bridge][TranslationDefaultDomain] add support of named arguments.
1 parent ec1cae8 commit 02bc23a

File tree

4 files changed

+81
-16
lines changed

4 files changed

+81
-16
lines changed

src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,20 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
6262
}
6363

6464
if ($node instanceof \Twig_Node_Expression_Filter && in_array($node->getNode('filter')->getAttribute('value'), array('trans', 'transchoice'))) {
65-
$ind = 'trans' === $node->getNode('filter')->getAttribute('value') ? 1 : 2;
6665
$arguments = $node->getNode('arguments');
67-
if (!$arguments->hasNode($ind)) {
68-
if (!$arguments->hasNode($ind - 1)) {
69-
$arguments->setNode($ind - 1, new \Twig_Node_Expression_Array(array(), $node->getLine()));
66+
$ind = 'trans' === $node->getNode('filter')->getAttribute('value') ? 1 : 2;
67+
if ($this->isNamedArguments($arguments)) {
68+
if (!$arguments->hasNode('domain') && !$arguments->hasNode($ind)) {
69+
$arguments->setNode('domain', $this->scope->get('domain'));
7070
}
71+
} else {
72+
if (!$arguments->hasNode($ind)) {
73+
if (!$arguments->hasNode($ind - 1)) {
74+
$arguments->setNode($ind - 1, new \Twig_Node_Expression_Array(array(), $node->getLine()));
75+
}
7176

72-
$arguments->setNode($ind, $this->scope->get('domain'));
77+
$arguments->setNode($ind, $this->scope->get('domain'));
78+
}
7379
}
7480
} elseif ($node instanceof TransNode) {
7581
if (null === $node->getNode('domain')) {
@@ -103,4 +109,18 @@ public function getPriority()
103109
{
104110
return -10;
105111
}
112+
113+
/**
114+
* @return bool
115+
*/
116+
private function isNamedArguments($arguments)
117+
{
118+
foreach ($arguments as $name => $node) {
119+
if (!is_int($name)) {
120+
return true;
121+
}
122+
}
123+
124+
return false;
125+
}
106126
}

src/Symfony/Bridge/Twig/Tests/Extension/TranslationExtensionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,40 @@ public function testDefaultTranslationDomain()
148148
$this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
149149
}
150150

151+
public function testDefaultTranslationDomainWithNamedArguments()
152+
{
153+
$templates = array(
154+
'index' => '
155+
{%- trans_default_domain "foo" %}
156+
157+
{%- block content %}
158+
{{- "foo"|trans(arguments = {}, domain = "custom") }}
159+
{{- "foo"|transchoice(count = 1) }}
160+
{{- "foo"|transchoice(count = 1, arguments = {}, domain = "custom") }}
161+
{{- "foo"|trans({}, domain = "custom") }}
162+
{{- "foo"|trans({}, "custom", locale = "fr") }}
163+
{{- "foo"|transchoice(1, arguments = {}, domain = "custom") }}
164+
{{- "foo"|transchoice(1, {}, "custom", locale = "fr") }}
165+
{% endblock %}
166+
',
167+
168+
'base' => '
169+
{%- block content "" %}
170+
',
171+
);
172+
173+
$translator = new Translator('en', new MessageSelector());
174+
$translator->addLoader('array', new ArrayLoader());
175+
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
176+
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
177+
$translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
178+
$translator->addResource('array', array('foo' => 'foo (fr)'), 'fr', 'custom');
179+
180+
$template = $this->getTemplate($templates, $translator);
181+
182+
$this->assertEquals('foo (custom)foo (foo)foo (custom)foo (custom)foo (fr)foo (custom)foo (fr)', trim($template->render(array())));
183+
}
184+
151185
protected function getTemplate($template, $translator = null)
152186
{
153187
if (null === $translator) {

src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ public function getDefaultDomainAssignmentTestData()
7878
array(TwigNodeProvider::getTransFilter(self::$message)),
7979
array(TwigNodeProvider::getTransChoiceFilter(self::$message)),
8080
array(TwigNodeProvider::getTransTag(self::$message)),
81+
// with named arguments
82+
array(TwigNodeProvider::getTransFilter(self::$message, null, array(
83+
'arguments' => new \Twig_Node_Expression_Array(array(), 0),
84+
))),
85+
array(TwigNodeProvider::getTransChoiceFilter(self::$message), null, array(
86+
'arguments' => new \Twig_Node_Expression_Array(array(), 0),
87+
)),
8188
);
8289
}
8390
}

src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public static function getModule($content)
2929
);
3030
}
3131

32-
public static function getTransFilter($message, $domain = null)
32+
public static function getTransFilter($message, $domain = null, $arguments = null)
3333
{
34-
$arguments = $domain ? array(
35-
new \Twig_Node_Expression_Array(array(), 0),
36-
new \Twig_Node_Expression_Constant($domain, 0),
37-
) : array();
34+
if (!$arguments) {
35+
$arguments = $domain ? array(
36+
new \Twig_Node_Expression_Array(array(), 0),
37+
new \Twig_Node_Expression_Constant($domain, 0),
38+
) : array();
39+
}
3840

3941
return new \Twig_Node_Expression_Filter(
4042
new \Twig_Node_Expression_Constant($message, 0),
@@ -44,13 +46,15 @@ public static function getTransFilter($message, $domain = null)
4446
);
4547
}
4648

47-
public static function getTransChoiceFilter($message, $domain = null)
49+
public static function getTransChoiceFilter($message, $domain = null, $arguments = null)
4850
{
49-
$arguments = $domain ? array(
50-
new \Twig_Node_Expression_Constant(0, 0),
51-
new \Twig_Node_Expression_Array(array(), 0),
52-
new \Twig_Node_Expression_Constant($domain, 0),
53-
) : array();
51+
if (!$arguments) {
52+
$arguments = $domain ? array(
53+
new \Twig_Node_Expression_Constant(0, 0),
54+
new \Twig_Node_Expression_Array(array(), 0),
55+
new \Twig_Node_Expression_Constant($domain, 0),
56+
) : array();
57+
}
5458

5559
return new \Twig_Node_Expression_Filter(
5660
new \Twig_Node_Expression_Constant($message, 0),

0 commit comments

Comments
 (0)