8000 Allow to use translation_domain false for validators · symfony/symfony@a554b34 · GitHub
[go: up one dir, main page]

Skip to content

Commit a554b34

Browse files
Allow to use translation_domain false for validators
1 parent e411712 commit a554b34

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
lines changed

src/Symfony/Component/Validator/Context/ExecutionContext.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ExecutionContext implements ExecutionContextInterface
4545
private mixed $root;
4646

4747
private TranslatorInterface $translator;
48-
private ?string $translationDomain;
48+
private string|false|null $translationDomain;
4949

5050
/**
5151
* The violations generated in the current context.
@@ -111,7 +111,7 @@ class ExecutionContext implements ExecutionContextInterface
111111
/**
112112
* @internal Called by {@link ExecutionContextFactory}. Should not be used in user code.
113113
*/
114-
public function __construct(ValidatorInterface $validator, mixed $root, TranslatorInterface $translator, string $translationDomain = null)
114+
public function __construct(ValidatorInterface $validator, mixed $root, TranslatorInterface $translator, string|false|null $translationDomain = null)
115115
{
116116
$this->validator = $validator;
117117
$this->root = $root;
@@ -139,10 +139,12 @@ public function setConstraint(Constraint $constraint)
139139
$this->constraint = $constraint;
140140
}
141141

142-
public function addViolation(string $message, array $parameters = [])
142+
public function addViolation(string $message, array $parameters = [], string|false|null $translationDomain = null)
143143
{
144144
$this->violations->add(new ConstraintViolation(
145-
$this->translator->trans($message, $parameters, $this->translationDomain),
145+
false === $translationDomain
146+
? $message
147+
: $this->translator->trans($message, $parameters, $translationDomain ?? $this->translationDomain),
146148
$message,
147149
$parameters,
148150
$this->root,
@@ -154,7 +156,7 @@ public function addViolation(string $message, array $parameters = [])
154156
));
155157
}
156158

157-
public function buildViolation(string $message, array $parameters = []): ConstraintViolationBuilderInterface
159+
public function buildViolation(string $message, array $parameters = [], string|false|null $translationDomain = null): ConstraintViolationBuilderInterface
158160
{
159161
return new ConstraintViolationBuilder(
160162
$this->violations,
@@ -165,7 +167,7 @@ public function buildViolation(string $message, array $parameters = []): Constra
165167
$this->propertyPath,
166168
$this->getValue(),
167169
$this->translator,
168-
$this->translationDomain
170+
$translationDomain ?? $this->translationDomain
169171
);
170172
}
171173

src/Symfony/Component/Validator/Context/ExecutionContextFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
class ExecutionContextFactory implements ExecutionContextFactoryInterface
2525
{
2626
private TranslatorInterface $translator;
27-
private ?string $translationDomain;
27+
private string|false|null $translationDomain;
2828

29-
public function __construct(TranslatorInterface $translator, string $translationDomain = null)
29+
public function __construct(TranslatorInterface $translator, string|false|null $translationDomain = null)
3030
{
3131
$this->translator = $translator;
3232
$this->translationDomain = $translationDomain;

src/Symfony/Component/Validator/Context/ExecutionContextInterface.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ interface ExecutionContextInterface
6464
/**
6565
* Adds a violation at the current node of the validation graph.
6666
*
67-
* @param string|\Stringable $message The error message as a string or a stringable object
68-
* @param array $params The parameters substituted in the error message
67+
* @param string|\Stringable $message The error message as a string or a stringable object
68+
* @param array $params The parameters substituted in the error message
69+
* @param string|false|null $translationDomain The translation domain of the error message
6970
*/
70-
public function addViolation(string $message, array $params = []);
71+
public function addViolation(string $message, array $params = []/* , string|false|null $translationDomain */);
7172

7273
/**
7374
* Returns a builder for adding a violation with extended information.
@@ -83,8 +84,9 @@ public function addViolation(string $message, array $params = []);
8384
*
8485
* @param string|\Stringable $message The error message as a string or a stringable object
8586
* @param array $parameters The parameters substituted in the error message
87+
* @param string|false|null $translationDomain The translation domain of the error message
8688
*/
87-
public function buildViolation(string $message, array $parameters = []): ConstraintViolationBuilderInterface;
89+
public function buildViolation(string $message, array $parameters = []/* , string|false|null $translationDomain */): ConstraintViolationBuilderInterface;
8890

8991
/**
9092
* Returns the validator.

src/Symfony/Component/Validator/ValidatorBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ValidatorBuilder
5454
private ConstraintValidatorFactoryInterface $validatorFactory;
5555
private ?CacheItemPoolInterface $mappingCache = null;
5656
private ?TranslatorInterface $translator = null;
57-
private ?string $translationDomain = null;
57+
private string|false|null $translationDomain = null;
5858

5959
/**
6060
* Adds an object initializer to the validator.
@@ -300,7 +300,7 @@ public function setTranslator(TranslatorInterface $translator): static
300300
*
301301
* @return $this
302302
*/
303-
public function setTranslationDomain(?string $translationDomain): static
303+
public function setTranslationDomain(string|false|null $translationDomain): static
304304
{
305305
$this->translationDomain = $translationDomain;
306306

src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
3333
private mixed $invalidValue;
3434
private string $propertyPath;
3535
private TranslatorInterface $translator;
36-
private ?string $translationDomain;
36+
private string|false|null $translationDomain;
3737
private ?int $plural = null;
3838
private ?Constraint $constraint;
3939
private ?string $code = null;
4040
private mixed $cause = null;
4141

42-
public function __construct(ConstraintViolationList $violations, ?Constraint $constraint, string|\Stringable $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string $translationDomain = null)
42+
public function __construct(ConstraintViolationList $violations, ?Constraint $constraint, string|\Stringable $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string|false $translationDomain = null)
4343
{
4444
$this->violations = $violations;
4545
$this->message = $message;
@@ -73,7 +73,7 @@ public function setParameters(array $parameters): static
7373
return $this;
7474
}
7575

76-
public function setTranslationDomain(string $translationDomain): static
76+
public function setTranslationDomain(string|false $translationDomain): static
7777
{
7878
$this->translationDomain = $translationDomain;
7979

@@ -110,7 +110,9 @@ public function setCause(mixed $cause): static
110110

111111
public function addViolation()
112112
{
113-
if (null === $this->plural) {
113+
if (false === $this->translationDomain) {
114+
$translatedMessage = $this->message;
115+
} elseif (null === $this->plural) {
114116
$translatedMessage = $this->translator->trans(
115117
$this->message,
116118
$this->parameters,

src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ public function setParameters(array $parameters): static;
6060
* Sets the translation domain which should be used for translating the
6161
* violation message.
6262
*
63-
* @param string $translationDomain The translation domain
63+
* @param string|false $translationDomain The translation domain
6464
*
6565
* @return $this
6666
*
6767
* @see \Symfony\Contracts\Translation\TranslatorInterface
6868
*/
69-
public function setTranslationDomain(string $translationDomain): static;
69+
public function setTranslationDomain(string|false $translationDomain): static;
7070

7171
/**
7272
* Sets the invalid value that caused this violation.

0 commit comments

Comments
 (0)
0