@@ -855,6 +855,117 @@ The translation of database content should be handled by Doctrine through
855
855
the `Translatable Extension `_. For more information, see the documentation
856
856
for that library.
857
857
858
+ Translating Constraint Messages
859
+ -------------------------------
860
+
861
+ The best way to understand constraint translation is to see it in action. To start,
862
+ suppose you've created a plain-old-PHP object that you need to use somewhere in
863
+ your application:
864
+
865
+ .. code-block :: php
866
+
867
+ // src/Acme/BlogBundle/Entity/Author.php
868
+ namespace Acme\BlogBundle\Entity;
869
+
870
+ class Author
871
+ {
872
+ public $name;
873
+ }
874
+
875
+ Add constraints though any of the supported methods. Set the message option to the
876
+ translation source text. For example, to guarantee that the $name property is not
877
+ empty, add the following:
878
+
879
+ .. configuration-block ::
880
+
881
+ .. code-block :: yaml
882
+
883
+ # src/Acme/BlogBundle/Resources/config/validation.yml
884
+ Acme\BlogBundle\Entity\Author :
885
+ properties :
886
+ name :
887
+ - NotBlank : { message: "author.name.not_blank" }
888
+
889
+ .. code-block :: php-annotations
890
+
891
+ // src/Acme/BlogBundle/Entity/Author.php
892
+ use Symfony\Component\Validator\Constraints as Assert;
893
+
894
+ class Author
895
+ {
896
+ /**
897
+ * @Assert\NotBlank(message = "author.name.not_blank")
898
+ */
899
+ public $name;
900
+ }
901
+
902
+ .. code-block :: xml
903
+
904
+ <!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
905
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
906
+ <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
907
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
908
+ xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
909
+
910
+ <class name =" Acme\BlogBundle\Entity\Author" >
911
+ <property name =" name" >
912
+ <constraint name =" NotBlank" >
913
+ <option name =" message" >author.name.not_blank</option >
914
+ </constraint >
915
+ </property >
916
+ </class >
917
+ </constraint-mapping >
918
+
919
+ .. code-block :: php
920
+
921
+ // src/Acme/BlogBundle/Entity/Author.php
922
+
923
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
924
+ use Symfony\Component\Validator\Constraints\NotBlank;
925
+
926
+ class Author
927
+ {
928
+ public $name;
929
+
930
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
931
+ {
932
+ $metadata->addPropertyConstraint('name', new NotBlank(array(
933
+ 'message' => 'author.name.not_blank'
934
+ )));
935
+ }
936
+ }
937
+
938
+ 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.
939
+
940
+ .. configuration-block ::
941
+
942
+ .. code-block :: xml
943
+
944
+ <!-- validators.fr.xliff -->
945
+ <?xml version =" 1.0" ?>
946
+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
947
+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
948
+ <body >
949
+ <trans-unit id =" 1" >
950
+ <source >author.name.not_blank</source >
951
+ <target >Please enter an author name.</target >
952
+ </trans-unit >
953
+ </body >
954
+ </file >
955
+ </xliff >
956
+
957
+ .. code-block :: php
958
+
959
+ // validators.fr.php
960
+ return array(
961
+ 'author.name.not_blank' => 'Please enter an author name.',
962
+ );
963
+
964
+ .. code-block :: yaml
965
+
966
+ # validators.fr.yml
967
+ author.name.not_blank : Please enter an author name.
968
+
858
969
Summary
859
970
-------
860
971
0 commit comments