Skip to content

Commit 177336c

Browse files
committed
[Validator] Add a section for custom translation domain
1 parent 2cbbb39 commit 177336c

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

validation/translations.rst

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,65 @@ Now, create a ``validators`` catalog file in the ``translations/`` directory:
135135
'author.name.not_blank' => 'Please enter an author name.',
136136
];
137137
138-
You may need to clear your cache (even in the dev environment) after creating this
139-
file for the first time.
138+
You may need to clear your cache (even in the dev environment) after creating
139+
this file for the first time.
140+
141+
Custom Translation Domain
142+
-------------------------
143+
144+
The default translation domain can be changed globally using the
145+
``FrameworkBundle`` configuration:
146+
147+
.. configuration-block::
148+
149+
.. code-block:: yaml
150+
151+
# config/packages/translation.yaml
152+
framework:
153+
translator:
154+
translation_domain: validation_errors
155+
156+
.. code-block:: xml
157+
158+
<!-- config/packages/translation.xml -->
159+
<?xml version="1.0" encoding="UTF-8" ?>
160+
<container xmlns="http://symfony.com/schema/dic/services"
161+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
162+
xmlns:framework="http://symfony.com/schema/dic/symfony"
163+
xsi:schemaLocation="http://symfony.com/schema/dic/services
164+
https://symfony.com/schema/dic/services/services-1.0.xsd
165+
http://symfony.com/schema/dic/symfony
166+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
167+
168+
<framework:config>
169+
<framework:translator
170+
translation-domain="validation_errors"
171+
/>
172+
</framework:config>
173+
</container>
174+
175+
.. code-block:: php
176+
177+
// config/packages/translation.php
178+
use Symfony\Config\FrameworkConfig;
179+
180+
return static function (FrameworkConfig $framework) {
181+
// ...
182+
$framework
183+
->defaultLocale('en')
184+
->translator()
185+
->translationDomain('validation_errors')
186+
;
187+
};
188+
189+
Or it can be customized for a specific violation from a constraint validator::
190+
191+
public function validate($value, Constraint $constraint): void
192+
{
193+
// validation logic
194+
195+
$this->context->buildViolation($constraint->message)
196+
->setParameter('{{ string }}', $value)
197+
->setTranslationDomain('validation_errors')
198+
->addViolation();
199+
}

0 commit comments

Comments
 (0)