bug #13250 [Twig][Bridge][TranslationDefaultDomain] add support of named arguments. (aitboudad)
This PR was squashed before being merged into the 2.3 branch (closes #13250).
Discussion
----------
[Twig][Bridge][TranslationDefaultDomain] add support of named arguments.
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Fixed tickets | #10859
| Tests pass? | yes
| License | MIT
Commits
-------
02bc23a
[Twig][Bridge][TranslationDefaultDomain] add support of named arguments.
This commit is contained in:
commit
94e8e03eea
@ -62,14 +62,20 @@ class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($node instanceof \Twig_Node_Expression_Filter && in_array($node->getNode('filter')->getAttribute('value'), array('trans', 'transchoice'))) {
|
if ($node instanceof \Twig_Node_Expression_Filter && in_array($node->getNode('filter')->getAttribute('value'), array('trans', 'transchoice'))) {
|
||||||
$ind = 'trans' === $node->getNode('filter')->getAttribute('value') ? 1 : 2;
|
|
||||||
$arguments = $node->getNode('arguments');
|
$arguments = $node->getNode('arguments');
|
||||||
if (!$arguments->hasNode($ind)) {
|
$ind = 'trans' === $node->getNode('filter')->getAttribute('value') ? 1 : 2;
|
||||||
if (!$arguments->hasNode($ind - 1)) {
|
if ($this->isNamedArguments($arguments)) {
|
||||||
$arguments->setNode($ind - 1, new \Twig_Node_Expression_Array(array(), $node->getLine()));
|
if (!$arguments->hasNode('domain') && !$arguments->hasNode($ind)) {
|
||||||
|
$arguments->setNode('domain', $this->scope->get('domain'));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (!$arguments->hasNode($ind)) {
|
||||||
|
if (!$arguments->hasNode($ind - 1)) {
|
||||||
|
$arguments->setNode($ind - 1, new \Twig_Node_Expression_Array(array(), $node->getLine()));
|
||||||
|
}
|
||||||
|
|
||||||
$arguments->setNode($ind, $this->scope->get('domain'));
|
$arguments->setNode($ind, $this->scope->get('domain'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} elseif ($node instanceof TransNode) {
|
} elseif ($node instanceof TransNode) {
|
||||||
if (null === $node->getNode('domain')) {
|
if (null === $node->getNode('domain')) {
|
||||||
@ -103,4 +109,18 @@ class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface
|
|||||||
{
|
{
|
||||||
return -10;
|
return -10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isNamedArguments($arguments)
|
||||||
|
{
|
||||||
|
foreach ($arguments as $name => $node) {
|
||||||
|
if (!is_int($name)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,6 +148,40 @@ class TranslationExtensionTest extends TestCase
|
|||||||
$this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
|
$this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDefaultTranslationDomainWithNamedArguments()
|
||||||
|
{
|
||||||
|
$templates = array(
|
||||||
|
'index' => '
|
||||||
|
{%- trans_default_domain "foo" %}
|
||||||
|
|
||||||
|
{%- block content %}
|
||||||
|
{{- "foo"|trans(arguments = {}, domain = "custom") }}
|
||||||
|
{{- "foo"|transchoice(count = 1) }}
|
||||||
|
{{- "foo"|transchoice(count = 1, arguments = {}, domain = "custom") }}
|
||||||
|
{{- "foo"|trans({}, domain = "custom") }}
|
||||||
|
{{- "foo"|trans({}, "custom", locale = "fr") }}
|
||||||
|
{{- "foo"|transchoice(1, arguments = {}, domain = "custom") }}
|
||||||
|
{{- "foo"|transchoice(1, {}, "custom", locale = "fr") }}
|
||||||
|
{% endblock %}
|
||||||
|
',
|
||||||
|
|
||||||
|
'base' => '
|
||||||
|
{%- block content "" %}
|
||||||
|
',
|
||||||
|
);
|
||||||
|
|
||||||
|
$translator = new Translator('en', new MessageSelector());
|
||||||
|
$translator->addLoader('array', new ArrayLoader());
|
||||||
|
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
|
||||||
|
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
|
||||||
|
$translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
|
||||||
|
$translator->addResource('array', array('foo' => 'foo (fr)'), 'fr', 'custom');
|
||||||
|
|
||||||
|
$template = $this->getTemplate($templates, $translator);
|
||||||
|
|
||||||
|
$this->assertEquals('foo (custom)foo (foo)foo (custom)foo (custom)foo (fr)foo (custom)foo (fr)', trim($template->render(array())));
|
||||||
|
}
|
||||||
|
|
||||||
protected function getTemplate($template, $translator = null)
|
protected function getTemplate($template, $translator = null)
|
||||||
{
|
{
|
||||||
if (null === $translator) {
|
if (null === $translator) {
|
||||||
|
@ -78,6 +78,13 @@ class TranslationDefaultDomainNodeVisitorTest extends TestCase
|
|||||||
array(TwigNodeProvider::getTransFilter(self::$message)),
|
array(TwigNodeProvider::getTransFilter(self::$message)),
|
||||||
array(TwigNodeProvider::getTransChoiceFilter(self::$message)),
|
array(TwigNodeProvider::getTransChoiceFilter(self::$message)),
|
||||||
array(TwigNodeProvider::getTransTag(self::$message)),
|
array(TwigNodeProvider::getTransTag(self::$message)),
|
||||||
|
// with named arguments
|
||||||
|
array(TwigNodeProvider::getTransFilter(self::$message, null, array(
|
||||||
|
'arguments' => new \Twig_Node_Expression_Array(array(), 0),
|
||||||
|
))),
|
||||||
|
array(TwigNodeProvider::getTransChoiceFilter(self::$message), null, array(
|
||||||
|
'arguments' => new \Twig_Node_Expression_Array(array(), 0),
|
||||||
|
)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,12 +29,14 @@ class TwigNodeProvider
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getTransFilter($message, $domain = null)
|
public static function getTransFilter($message, $domain = null, $arguments = null)
|
||||||
{
|
{
|
||||||
$arguments = $domain ? array(
|
if (!$arguments) {
|
||||||
new \Twig_Node_Expression_Array(array(), 0),
|
$arguments = $domain ? array(
|
||||||
new \Twig_Node_Expression_Constant($domain, 0),
|
new \Twig_Node_Expression_Array(array(), 0),
|
||||||
) : array();
|
new \Twig_Node_Expression_Constant($domain, 0),
|
||||||
|
) : array();
|
||||||
|
}
|
||||||
|
|
||||||
return new \Twig_Node_Expression_Filter(
|
return new \Twig_Node_Expression_Filter(
|
||||||
new \Twig_Node_Expression_Constant($message, 0),
|
new \Twig_Node_Expression_Constant($message, 0),
|
||||||
@ -44,13 +46,15 @@ class TwigNodeProvider
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getTransChoiceFilter($message, $domain = null)
|
public static function getTransChoiceFilter($message, $domain = null, $arguments = null)
|
||||||
{
|
{
|
||||||
$arguments = $domain ? array(
|
if (!$arguments) {
|
||||||
new \Twig_Node_Expression_Constant(0, 0),
|
$arguments = $domain ? array(
|
||||||
new \Twig_Node_Expression_Array(array(), 0),
|
new \Twig_Node_Expression_Constant(0, 0),
|
||||||
new \Twig_Node_Expression_Constant($domain, 0),
|
new \Twig_Node_Expression_Array(array(), 0),
|
||||||
) : array();
|
new \Twig_Node_Expression_Constant($domain, 0),
|
||||||
|
) : array();
|
||||||
|
}
|
||||||
|
|
||||||
return new \Twig_Node_Expression_Filter(
|
return new \Twig_Node_Expression_Filter(
|
||||||
new \Twig_Node_Expression_Constant($message, 0),
|
new \Twig_Node_Expression_Constant($message, 0),
|
||||||
|
Reference in New Issue
Block a user