@@ -126,36 +126,28 @@ this file for the first time.
126
126
Translatable Objects
127
127
--------------------
128
128
129
- In some cases, you may want to translate content from a simple function with multiple checks or contexts.
130
- For example, when using the ``Callback `` constraints.
131
-
132
- You can use the :class: `Symfony\\ Component\\ Translation\\ TranslatableMessage ` class like this:
133
-
134
- .. configuration-block ::
135
-
136
- .. code-block :: php-attributes
137
-
138
- use Symfony\Component\Translation\TranslatableMessage;
139
- use Symfony\Component\Validator\Constraints as Assert;
140
- use Symfony\Component\Validator\Context\ExecutionContextInterface;
141
-
142
- #[Assert\Callback]
143
- public function validate(ExecutionContextInterface $context, mixed $payload): void
144
- {
145
- // somehow you have an array of "fake names"
146
- $fakeNames = [/* ... */];
147
-
148
- // check if the name is actually a fake name
149
- if (in_array($this->getFirstName(), $fakeNames)) {
150
- $context->buildViolation(new TranslatableMessage('author.name.fake', [], 'validators'))
151
- ->atPath('firstName')
152
- ->addViolation()
153
- ;
154
- }
129
+ You can also use :class: `Ns\\ TranslatableMessage ` to build your violation message::
130
+
131
+ use Symfony\Component\Translation\TranslatableMessage;
132
+ use Symfony\Component\Validator\Constraints as Assert;
133
+ use Symfony\Component\Validator\Context\ExecutionContextInterface;
134
+
135
+ #[Assert\Callback]
136
+ public function validate(ExecutionContextInterface $context, mixed $payload): void
137
+ {
138
+ // somehow you have an array of "fake names"
139
+ $fakeNames = [/* ... */];
140
+
141
+ // check if the name is actually a fake name
142
+ if (in_array($this->getFirstName(), $fakeNames)) {
143
+ $context->buildViolation(new TranslatableMessage('author.name.fake', [], 'validators'))
144
+ ->atPath('firstName')
145
+ ->addViolation()
146
+ ;
155
147
}
148
+ }
156
149
157
- Learn more about
158
- :ref: `translatable object <translatable-objects >`.
150
+ You can learn more about translatable messages in :ref: `the dedicated section <translatable-objects >`.
159
151
160
152
Custom Translation Domain
161
153
-------------------------
0 commit comments