You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/translation.rst
+109Lines changed: 109 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -821,6 +821,115 @@ The translation of database content should be handled by Doctrine through
821
821
the `Translatable Extension`_. For more information, see the documentation
822
822
for that library.
823
823
824
+
Translating Constraint Messages
825
+
-------------------------------
826
+
827
+
The best way to understand constraint translation is to see it in action. To start, suppose
828
+
you've created a plain-old-PHP object that you need to use somewhere in
829
+
your application:
830
+
831
+
.. code-block:: php
832
+
833
+
// src/Acme/BlogBundle/Entity/Author.php
834
+
namespace Acme\BlogBundle\Entity;
835
+
836
+
class Author
837
+
{
838
+
public $name;
839
+
}
840
+
841
+
Add constraints though any of the supported methods. Set the message option to the translation source text. For example, to guarantee that the $name property is not empty, add the following:
use Symfony\Component\Validator\Mapping\ClassMetadata;
888
+
use Symfony\Component\Validator\Constraints\NotBlank;
889
+
890
+
class Author
891
+
{
892
+
public $name;
893
+
894
+
public static function loadValidatorMetadata(ClassMetadata $metadata)
895
+
{
896
+
$metadata->addPropertyConstraint('name', new NotBlank(array(
897
+
'message' => 'author.name.not_blank'
898
+
)));
899
+
}
900
+
}
901
+
902
+
Create a translation file under the ``validators`` catalog for the constraint messages, typically in the ``Resources/translations/`` directory of the bundle. See `Message Catalogues`_ for more details.
0 commit comments