Skip to content

Commit 0e2b304

Browse files
Make trans + %count% parameter resolve plurals
1 parent a7eac93 commit 0e2b304

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

Context/ExecutionContext.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Context;
1313

14+
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
1415
use Symfony\Component\Validator\Constraint;
1516
use Symfony\Component\Validator\ConstraintViolation;
1617
use Symfony\Component\Validator\ConstraintViolationList;
@@ -140,8 +141,11 @@ class ExecutionContext implements ExecutionContextInterface
140141
* @internal Called by {@link ExecutionContextFactory}. Should not be used
141142
* in user code.
142143
*/
143-
public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, string $translationDomain = null)
144+
public function __construct(ValidatorInterface $validator, $root, $translator, string $translationDomain = null)
144145
{
146+
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
147+
throw new \TypeError(sprintf('Argument 3 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
148+
}
145149
$this->validator = $validator;
146150
$this->root = $root;
147151
$this->translator = $translator;

Context/ExecutionContextFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Context;
1313

14+
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
1415
use Symfony\Component\Validator\Validator\ValidatorInterface;
1516
use Symfony\Contracts\Translation\TranslatorInterface;
1617

@@ -34,8 +35,12 @@ class ExecutionContextFactory implements ExecutionContextFactoryInterface
3435
* use for translating
3536
* violation messages
3637
*/
37-
public function __construct(TranslatorInterface $translator, string $translationDomain = null)
38+
public function __construct($translator, string $translationDomain = null)
3839
{
40+
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
41+
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
42+
}
43+
3944
$this->translator = $translator;
4045
$this->translationDomain = $translationDomain;
4146
}

Util/LegacyTranslatorProxy.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111

1212
namespace Symfony\Component\Validator\Util;
1313

14-
use Symfony\Component\Translation\LegacyTranslatorTrait;
1514
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
1615
use Symfony\Contracts\Translation\TranslatorInterface;
1716

1817
/**
1918
* @internal to be removed in Symfony 5.0.
2019
*/
21-
class LegacyTranslatorProxy implements LegacyTranslatorInterface
20+
class LegacyTranslatorProxy implements LegacyTranslatorInterface, TranslatorInterface
2221
{
23-
use LegacyTranslatorTrait {
24-
transChoice as private doTransChoice;
25-
}
2622
private $translator;
2723

2824
public function __construct(TranslatorInterface $translator)
@@ -64,10 +60,6 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
6460
*/
6561
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
6662
{
67-
if ($this->translator instanceof LegacyTranslatorInterface) {
68-
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
69-
}
70-
71-
$this->doTransChoice($id, $number, $parameters, $domain, $locale);
63+
return $this->translator->trans($id, array('%count%' => $number) + $parameters, $domain, $locale);
7264
}
7365
}

Violation/ConstraintViolationBuilder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
4444
*/
4545
private $cause;
4646

47-
public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
47+
/**
48+
* @param TranslatorInterface $translator
49+
*/
50+
public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, $translator, $translationDomain = null)
4851
{
52+
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
53+
throw new \TypeError(sprintf('Argument 8 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
54+
}
4955
$this->violations = $violations;
5056
$this->message = $message;
5157
$this->parameters = $parameters;
@@ -142,12 +148,18 @@ public function setCause($cause)
142148
*/
143149
public function addViolation()
144150
{
145-
if (null === $this->plural || !$this->translator instanceof LegacyTranslatorInterface) {
151+
if (null === $this->plural) {
146152
$translatedMessage = $this->translator->trans(
147153
$this->message,
148154
$this->parameters,
149155
$this->translationDomain
150156
);
157+
} elseif ($this->translator instanceof TranslatorInterface) {
158+
$translatedMessage = $this->translator->trans(
159+
$this->message,
160+
array('%count%' => $this->plural) + $this->parameters,
161+
$this->translationDomain
162+
);
151163
} else {
152164
try {
153165
$translatedMessage = $this->translator->transChoice(

0 commit comments

Comments
 (0)