Browse Source

Merge pull request #1365 from dantleech/ext_trans_domain

[WIP] Added ability to set translation domain on field description
Thomas 12 năm trước cách đây
mục cha
commit
07d15b5072

+ 2 - 8
Admin/Admin.php

@@ -2091,11 +2091,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * set the translation domain
-     *
-     * @param string $translationDomain the translation domain
-     *
-     * @return void
+     * {@inheritdoc}
      */
     public function setTranslationDomain($translationDomain)
     {
@@ -2103,9 +2099,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returns the translation domain
-     *
-     * @return string the translation domain
+     * {@inheritdoc}
      */
     public function getTranslationDomain()
     {

+ 16 - 0
Admin/AdminInterface.php

@@ -555,4 +555,20 @@ interface AdminInterface
      * @return null|string
      */
     public function getTemplate($name);
+
+    /**
+     * Set the translation domain
+     *
+     * @param string $translationDomain the translation domain
+     *
+     * @return void
+     */
+    public function setTranslationDomain($translationDomain);
+
+    /**
+     * Returns the translation domain
+     *
+     * @return string the translation domain
+     */
+    public function getTranslationDomain();
 }

+ 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
      */

+ 31 - 3
Resources/doc/reference/translation.rst

@@ -41,7 +41,7 @@ You have two options to configure the catalogue for the admin class:
 
 
 An admin instance always gets the ``translator`` instance, so it can be used to
-translate messages within the ``configure*Fields`` method or in templates.
+translate messages within the ``configureFields`` method or in templates.
 
 .. code-block:: jinja
 
@@ -61,8 +61,33 @@ Translate field labels
 ----------------------
 
 The Admin bundle comes with a customized form field template. The most notable
-changes from the original one is the use of the translation domain provided by
-the Admin instance to translate label.
+change from the original one is the use of the translation domain provided by
+either the Admin instance or the field description to translate labels.
+
+Setting the translation domain
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+The translation domain of the Admin instance is used by default, however this
+can be overridden when defining new form fields:
+
+.. code-block:: php
+
+        $formMapper->with('form.my_group')
+            ->add('publishable', 'checkbox', array(
+                // ...
+                'translation_domain' => 'MyTranslationDomain',
+            ))
+            ->add('start_date', 'date', array(
+                // ...
+                'translation_domain' => 'MyTranslationDomain',
+            ));
+
+This is of particular use when using admin :doc:`extensions <extensions>`,
+where the extension and the translations would be defined in one bundle, but
+implemented in many different admin instances.
+
+Setting the label name
+^^^^^^^^^^^^^^^^^^^^^^
 
 By default, the label is the field name. However a label can be defined as
 third argument of the ``add`` method:
@@ -78,6 +103,9 @@ third argument of the ``add`` method:
         }
     }
 
+Label strategies
+^^^^^^^^^^^^^^^^
+
 There is another option for rapid prototyping or to avoid spending too much time
 adding the ``label`` key to all option fields: ``Label Strategies``. By default
 labels are generated by using a simple rule ::

+ 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>

+ 19 - 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,25 @@ class BaseFieldDescriptionTest extends \PHPUnit_Framework_TestCase
         $description->setOption('bar', 'hello');
         $description->mergeOption('bar', array('exception'));
     }
+
+    public function testGetTranslationDomain()
+    {
+        $description = new FieldDescription();
+
+        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $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