1111
1212namespace Symfony \Component \Validator ;
1313
14+ use Symfony \Component \Translation \TranslatorInterface ;
15+
1416/**
1517 * Default implementation of {@link ExecutionContextInterface}.
1618 *
@@ -26,6 +28,16 @@ class ExecutionContext implements ExecutionContextInterface
2628 */
2729 private $ globalContext ;
2830
31+ /**
32+ * @var TranslatorInterface
33+ */
34+ private $ translator ;
35+
36+ /**
37+ * @var null|string
38+ */
39+ private $ translationDomain ;
40+
2941 /**
3042 * @var MetadataInterface
3143 */
@@ -49,19 +61,23 @@ class ExecutionContext implements ExecutionContextInterface
4961 /**
5062 * Creates a new execution context.
5163 *
52- * @param GlobalExecutionContextInterface $globalContext The global context storing node-independent state.
53- * @param MetadataInterface $metadata The metadata of the validated node.
54- * @param mixed $value The value of the validated node.
55- * @param string $group The current validation group.
56- * @param string $propertyPath The property path to the current node.
64+ * @param GlobalExecutionContextInterface $globalContext The global context storing node-independent state.
65+ * @param TranslatorInterface $translator The translator for translating violation messages.
66+ * @param null|string $translationDomain The domain of the validation messages.
67+ * @param MetadataInterface $metadata The metadata of the validated node.
68+ * @param mixed $value The value of the validated node.
69+ * @param string $group The current validation group.
70+ * @param string $propertyPath The property path to the current node.
5771 */
58- public function __construct (GlobalExecutionContextInterface $ globalContext , MetadataInterface $ metadata = null , $ value = null , $ group = null , $ propertyPath = '' )
72+ public function __construct (GlobalExecutionContextInterface $ globalContext , TranslatorInterface $ translator , $ translationDomain = null , MetadataInterface $ metadata = null , $ value = null , $ group = null , $ propertyPath = '' )
5973 {
6074 if (null === $ group ) {
6175 $ group = Constraint::DEFAULT_GROUP ;
6276 }
6377
6478 $ this ->globalContext = $ globalContext ;
79+ $ this ->translator = $ translator ;
80+ $ this ->translationDomain = $ translationDomain ;
6581 $ this ->metadata = $ metadata ;
6682 $ this ->value = $ value ;
6783 $ this ->propertyPath = $ propertyPath ;
@@ -74,6 +90,9 @@ public function __construct(GlobalExecutionContextInterface $globalContext, Meta
7490 public function addViolation ($ message , array $ params = array (), $ invalidValue = null , $ pluralization = null , $ code = null )
7591 {
7692 $ this ->globalContext ->getViolations ()->add (new ConstraintViolation (
93+ null === $ pluralization
94+ ? $ this ->translator ->trans ($ message , $ params , $ this ->translationDomain )
95+ : $ this ->translator ->transChoice ($ message , $ pluralization , $ params , $ this ->translationDomain ),
7796 $ message ,
7897 $ params ,
7998 $ this ->globalContext ->getRoot (),
@@ -103,6 +122,9 @@ public function addViolationAtPath($propertyPath, $message, array $params = arra
103122 trigger_error ('addViolationAtPath() is deprecated since version 2.2 and will be removed in 2.3. ' , E_USER_DEPRECATED );
104123
105124 $ this ->globalContext ->getViolations ()->add (new ConstraintViolation (
125+ null === $ pluralization
126+ ? $ this ->translator ->trans ($ message , $ params , $ this ->translationDomain )
127+ : $ this ->translator ->transChoice ($ message , $ pluralization , $ params , $ this ->translationDomain ),
106128 $ message ,
107129 $ params ,
108130 $ this ->globalContext ->getRoot (),
@@ -146,6 +168,9 @@ public function addViolationAtSubPath($subPath, $message, array $params = array(
146168 public function addViolationAt ($ subPath , $ message , array $ params = array (), $ invalidValue = null , $ pluralization = null , $ code = null )
147169 {
148170 $ this ->globalContext ->getViolations ()->add (new ConstraintViolation (
171+ null === $ pluralization
172+ ? $ this ->translator ->trans ($ message , $ params , $ this ->translationDomain )
173+ : $ this ->translator ->transChoice ($ message , $ pluralization , $ params , $ this ->translationDomain ),
149174 $ message ,
150175 $ params ,
151176 $ this ->globalContext ->getRoot (),
0 commit comments