Explorar o código

Added ability to set translation domain on field description

dantleech %!s(int64=12) %!d(string=hai) anos
pai
achega
644748f497

+ 8 - 0
Admin/BaseFieldDescription.php

@@ -454,4 +454,12 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
     {
         return $this->getOption('sort_parent_association_mappings');
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getTranslationDomain()
+    {
+        return $this->getOption('translation_domain') ? : $this->getAdmin()->getTranslationDomain();
+    }
 }

+ 7 - 0
Admin/FieldDescriptionInterface.php

@@ -279,6 +279,13 @@ interface FieldDescriptionInterface
      */
     public function getLabel();
 
+    /**
+     * Return the translation domain to use for the current field.
+     *
+     * @return string
+     */
+    public function getTranslationDomain();
+
     /*
      * @return boolean
      */

+ 2 - 2
Resources/views/Form/form_admin_fields.html.twig

@@ -33,7 +33,7 @@ file that was distributed with this source code.
                     {% if not sonata_admin.admin %}
                         {{- label|trans({}, translation_domain) -}}
                     {% else %}
-                        {{- label|trans({}, sonata_admin.admin.translationDomain) -}}
+                        {{- label|trans({}, sonata_admin.field_description.translationDomain) -}}
                     {% endif%}
                 </span>
             </label>
@@ -42,7 +42,7 @@ file that was distributed with this source code.
                 {% if not sonata_admin.admin%}
                     {{- label|trans({}, translation_domain) -}}
                 {% else %}
-                    {{ sonata_admin.admin.trans(label) }}
+                {{ sonata_admin.admin.trans(label, {}, sonata_admin.field_description.translationDomain) }}
                 {% endif %}
                 {{ required ? '*' : '' }}
             </label>

+ 21 - 1
Tests/Admin/BaseFieldDescriptionTest.php

@@ -47,7 +47,6 @@ class BaseFieldDescriptionTest extends \PHPUnit_Framework_TestCase
 
         $description->setOption('label', 'trucmuche');
         $this->assertEquals('trucmuche', $description->getLabel());
-
         $this->assertNull($description->getTemplate());
         $description->setOptions(array('type' => 'integer', 'template' => 'foo.twig.html', 'help' => 'fooHelp'));
 
@@ -116,6 +115,27 @@ class BaseFieldDescriptionTest extends \PHPUnit_Framework_TestCase
         $description->setOption('bar', 'hello');
         $description->mergeOption('bar', array('exception'));
     }
+
+    public function testGetTranslationDomain()
+    {
+        $description = new FieldDescription();
+
+        $admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\Admin')
+            ->disableOriginalConstructor()
+            ->getMock();
+        $description->setAdmin($admin);
+
+        $admin->expects($this->once())
+            ->method('getTranslationDomain')
+            ->will($this->returnValue('AdminDomain'));
+
+        $this->assertEquals('AdminDomain', $description->getTranslationDomain());
+
+        $admin->expects($this->never())
+            ->method('getTranslationDomain');
+        $description->setOption('translation_domain', 'ExtensionDomain');
+        $this->assertEquals('ExtensionDomain', $description->getTranslationDomain());
+    }
 }
 
 class FieldDescription extends BaseFieldDescription