10000 [FrameworkBundle][Validator] Add `framework.validation.disable_transl… · symfony/symfony@a130bda · GitHub
[go: up one dir, main page]

Skip to content

Commit a130bda

Browse files
[FrameworkBundle][Validator] Add framework.validation.disable_translation config
1 parent d4566b2 commit a130bda

File tree

9 files changed

+31
-4
lines changed

9 files changed

+31
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ CHANGELOG
3030
* Add support for configuring multiple serializer instances via the configuration
3131
* Add support for `SYMFONY_TRUSTED_PROXIES`, `SYMFONY_TRUSTED_HEADERS`, `SYMFONY_TRUST_X_SENDFILE_TYPE_HEADER` and `SYMFONY_TRUSTED_HOSTS` env vars
3232
* Add `--no-fill` option to `translation:extract` command
33+
* Add `framework.validation.disable_translation` option
3334

3435
7.1
3536
---

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,9 @@ private function addValidationSection(ArrayNodeDefinition $rootNode, callable $e
10661066
->end()
10671067
->end()
10681068
->end()
1069+
->booleanNode('disable_translation')
1070+
->defaultFalse()
1071+
->end()
10691072
->arrayNode('auto_mapping')
10701073
->info('A collection of namespaces for which auto-mapping will be enabled by default, or null to opt-in with the EnableAutoMapping constraint.')
10711074
->example([

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,10 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
17221722
$validatorBuilder->addMethodCall('setMappingCache', [new Reference('validator.mapping.cache.adapter')]);
17231723
}
17241724

1725+
if ($config['disable_translation'] ?? false) {
1726+
$validatorBuilder->addMethodCall('disableTranslation');
1727+
}
1728+
17251729
$container->setParameter('validator.auto_mapping', $config['auto_mapping']);
17261730
if (!$propertyInfoEnabled || !class_exists(PropertyInfoLoader::class)) {
17271731
$container->removeDefinition('validator.property_info_loader');

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@
297297
<xsd:attribute name="enable-attributes" type="xsd:boolean" />
298298
<xsd:attribute name="static-method" type="xsd:boolean" />
299299
<xsd:attribute name="translation-domain" type="xsd:string" />
300+
<xsd:attribute name="disable-translation" type="xsd:boolean" />
300301
<xsd:attribute name="strict-email" type="xsd:boolean" />
301302
<xsd:attribute name="email-validation-mode" type="email-validation-mode" />
302303
</xsd:complexType>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ protected static function getBundleDefaultConfig()
775775
'enable_attributes' => !class_exists(FullStack::class),
776776
'static_method' => ['loadValidatorMetadata'],
777777
'translation_domain' => 'validators',
778+
'disable_translation' => false,
778779
'mapping' => [
779780
'paths' => [],
780781
],

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

Lines changed: 4 additions & 2 deletions
Original file 57AE line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function __construct(
107107
private ValidatorInterface $validator,
108108
private mixed $root,
109109
private TranslatorInterface $translator,
110-
private ?string $translationDomain = null,
110+
private string|false|null $translationDomain = null,
111111
) {
112112
$this->violations = new ConstraintViolationList();
113113
$this->cachedObjectsRefs = new \SplObjectStorage();
@@ -134,7 +134,9 @@ public function setConstraint(Constraint $constraint): void
134134
public function addViolation(string|\Stringable $message, array $parameters = []): void
135135
{
136136
$this->violations->add(new ConstraintViolation(
137-
$this->translator->trans($message, $parameters, $this->translationDomain),
137+
false === $this->translationDomain ?
138+
strtr($message, $parameters) :
139+
$this->translator->trans($message, $parameters, $this->translationDomain),
138140
$message,
139141
$parameters,
140142
$this->root,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ExecutionContextFactory implements ExecutionContextFactoryInterface
2525
{
2626
public function __construct(
2727
private TranslatorInterface $translator,
28-
private ?string $translationDomain = null,
28+
private string|false|null $translationDomain = null,
2929
) {
3030
}
3131

src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public function testSetTranslationDomain()
9999
$this->assertSame($this->builder, $this->builder->setTranslationDomain('TRANS_DOMAIN'));
100100
}
101101

102+
public function testDisableTranslation()
103+
{
104+
$this->assertSame($this->builder, $this->builder->disableTranslation());
105+
}
106+
102107
public function testGetValidator()
103108
{
104109
$this->assertInstanceOf(RecursiveValidator::class, $this->builder->getValidator());

src/Symfony/Component/Validator/ValidatorBuilder.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ValidatorBuilder
5050
private ?ContainerInterface $groupProviderLocator = null;
5151
private ?CacheItemPoolInterface $mappingCache = null;
5252
private ?TranslatorInterface $translator = null;
53-
private ?string $translationDomain = null;
53+
private string|false|null $translationDomain = null;
5454

5555
/**
5656
* Adds an object initializer to the validator.
@@ -292,6 +292,16 @@ public function setTranslationDomain(?string $translationDomain): static
292292
return $this;
293293
}
294294

295+
/**
296+
* @return $this
297+
*/
298+
public function disableTranslation(): static
299+
{
300+
$this->translationDomain = false;
301+
302+
return $this;
303+
}
304+
295305
/**
296306
* @return $this
297307
*/

0 commit comments

Comments
 (0)
0