8000 [Contracts] Add Translation\TranslatorInterface + decouple symfony/va… · symfony/symfony@e850ffa · GitHub
[go: up one dir, main page]

Skip to content

Commit e850ffa

Browse files
[Contracts] Add Translation\TranslatorInterface + decouple symfony/validator from symfony/translation
1 parent f0168e3 commit e850ffa

23 files changed

+452
-113
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
use Symfony\Component\Stopwatch\Stopwatch;
9292
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
9393
use Symfony\Component\Translation\Translator;
94+
use Symfony\Component\Translation\Util\XliffUtils;
95+
use Symfony\Component\Validator\Constraints\DivisibleBy;
9496
use Symfony\Component\Validator\ConstraintValidatorInterface;
9597
use Symfony\Component\Validator\ObjectInitializerInterface;
9698
use Symfony\Component\WebLink\HttpHeaderSerializer;
@@ -966,6 +968,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
966968

967969
$loader->load('translation.xml');
968970

971+
if (!\class_exists(XliffUtils::class)) {
972+
$container->getDefinition('translator.formatter.default')->replaceArgument(0, new Reference('translator.selector'));
973+
}
974+
969975
// Use the "real" translator instead of the identity default
970976
$container->setAlias('translator', 'translator.default')->setPublic(true);
971977
$container->setAlias('translator.formatter', new Alias($config['formatter'], false));
@@ -1067,6 +1073,11 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
10671073

10681074
$validatorBuilder = $container->getDefinition('validator.builder');
10691075

1076+
if (!\class_exists(DivisibleBy::class)) {
1077+
$container->getDefinition('validator')->setArguments(array());
1078+
$validatorBuilder->addMethodCall('setTranslator', array(new Reference('translator')));
1079+
}
1080+
10701081
$container->setParameter('validator.translation_domain', $config['translation_domain']);
10711082

10721083
$files = array('xml' => array(), 'yml' => array());

src/Symfony/Bundle/FrameworkBundle/Resources/config/identity_translator.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
<services>
77
<defaults public="false" />
88

9-
<service id="translator" class="Symfony\Component\Translation\IdentityTranslator" public="true">
10-
<argument type="service" id="translator.selector" />
11-
</service>
9+
<service id="translator" class="Symfony\Component\Translation\IdentityTranslator" public="true" />
1210
<service id="Symfony\Component\Translation\TranslatorInterface" alias="translator" />
1311

14-
<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector" />
12+
<service id="identity_translator" class="Symfony\Component\Translation\IdentityTranslator" />
13+
<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector">
14+
<deprecated>The "%service_id%" service is deprecated 10000 since Symfony 4.2, use "identity_translator" instead.</deprecated>
15+
</service>
1516
</services>
1617
</container>

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</service>
3030

3131
<service id="translator.formatter.default" class="Symfony\Component\Translation\Formatter\MessageFormatter">
32-
<argument type="service" id="translator.selector" />
32+
<argument type="service" id="identity_translator" />
3333
</service>
3434

3535
<service id="translation.loader.php" class="Symfony\Component\Translation\Loader\PhpFileLoader">

src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
<service id="validator" class="Symfony\Component\Validator\Validator\ValidatorInterface" public="true">
1616
<factory service="validator.builder" method="getValidator" />
17+
<argument type="service" id="translator" />
1718
</service>
1819
<service id="Symfony\Component\Validator\Validator\ValidatorInterface" alias="validator" />
1920

@@ -22,9 +23,6 @@
2223
<call method="setConstraintValidatorFactory">
2324
<argument type="service" id="validator.validator_factory" />
2425
</call>
25-
<call method="setTranslator">
26-
<argument type="service" id="translator" />
27-
</call>
2826
<call method="setTranslationDomain">
2927
<argument>%validator.translation_domain%</argument>
3028
</call>

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,20 +733,20 @@ public function testValidation()
733733
strtr($projectDir.'/config/validator/foo.xml', '/', \DIRECTORY_SEPARATOR),
734734
);
735735

736+
$this->assertEquals(array(new Reference('translator')), $container->getDefinition('validator')->getArguments());
737+
736738
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
737739

738740
$annotations = !class_exists(FullStack::class) && class_exists(Annotation::class);
739741

740-
$this->assertCount($annotations ? 7 : 6, $calls);
742+
$this->assertCount($annotations ? 6 : 5, $calls);
741743
$this->assertSame('setConstraintValidatorFactory', $calls[0][0]);
742744
$this->assertEquals(array(new Reference('validator.validator_factory')), $calls[0][1]);
743-
$this->assertSame('setTranslator', $calls[1][0]);
744-
$this->assertEquals(array(new Reference('translator')), $calls[1][1]);
745-
$this->assertSame('setTranslationDomain', $calls[2][0]);
746-
$this->assertSame(array('%validator.translation_domain%'), $calls[2][1]);
747-
$this->assertSame('addXmlMappings', $calls[3][0]);
748-
$this->assertSame(array($xmlMappings), $calls[3][1]);
749-
$i = 3;
745+
$this->assertSame('setTranslationDomain', $calls[1][0]);
746+
$this->assertSame(array('%validator.translation_domain%'), $calls[1][1]);
747+
$this->assertSame('addXmlMappings', $calls[2][0]);
748+
$this->assertSame(array($xmlMappings), $calls[2][1]);
749+
$i = 2;
750750
if ($annotations) {
751751
$this->assertSame('enableAnnotationMapping', $calls[++$i][0]);
752752
}

src/Symfony/Component/Translation/Formatter/MessageFormatter.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@
1111

1212
namespace Symfony\Component\Translation\Formatter;
1313

14+
use Symfony\Component\Translation\IdentityTranslator;
1415
use Symfony\Component\Translation\MessageSelector;
16+
use Symfony\Contracts\Translation\TranslatorInterface;
1517

1618
/**
1719
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
1820
*/
1921
class MessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
2022
{
21-
private $selector;
23+
private $translator;
2224

2325
/**
24-
* @param MessageSelector|null $selector The message selector for pluralization
26+
* @param TranslatorInterface|null $selector The identity translator to use as selector for pluralization
2527
*/
26-
public function __construct(MessageSelector $selector = null)
28+
public function __construct($translator = null)
2729
{
28-
$this->selector = $selector ?: new MessageSelector();
30+
$this->translator = $translator instanceof MessageSelector ? new IdentityTranslator($translator) : $translator ?? new IdentityTranslator();
2931
}
3032

3133
/**
@@ -43,6 +45,6 @@ public function choiceFormat($message, $number, $locale, array $parameters = arr
4345
{
4446
$parameters = array_merge(array('%count%' => $number), $parameters);
4547

46-
return $this->format($this->selector->choose($message, (int) $number, $locale), $locale, $parameters);
48+
return $this->format($this->translator->transChoice($message, $number, array(), null, $locale), $locale, $parameters);
4749
}
4850
}

src/Symfony/Component/Translation/IdentityTranslator.php

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,38 @@
1111

1212
namespace Symfony\Component\Translation;
1313

14+
use Symfony\Contracts\Translation\TranslatorTrait;
15+
1416
/**
1517
* IdentityTranslator does not translate anything.
1618
*
1719
* @author Fabien Potencier <fabien@symfony.com>
1820
*/
1921
class IdentityTranslator implements TranslatorInterface
2022
{
23+
use TranslatorTrait {
24+
transChoice as private doTransChoice;
25+
}
26+
2127
private $selector;
22-
private $locale;
2328

2429
/**
2530
* @param MessageSelector|null $selector The message selector for pluralization
2631
*/
2732
public function __construct(MessageSelector $selector = null)
2833
{
29-
$this->selector = $selector ?: new MessageSelector();
30-
}
31-
32-
/**
33-
* {@inheritdoc}
34-
*/
35-
public function setLocale($locale)
36-
{
37-
$this->locale = $locale;
38-
}
39-
40-
/**
41-
* {@inheritdoc}
42-
*/
43-
public function getLocale()
44-
{
45-
return $this->locale ?: \Locale::getDefault();
46-
}
47-
48-
/**
49-
* {@inheritdoc}
50-
*/
51-
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
52-
{
53-
return strtr((string) $id, $parameters);
34+
$this->selector = $selector;
5435
}
5536

5637
/**
5738
* {@inheritdoc}
5839
*/
5940
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
6041
{
61-
return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
42+
if ($this->selector) {
43+
return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
44+
}
45+
46+
return $this->doTransChoice($id, $number, $parameters, $domain, $locale);
6247
}
6348
}

src/Symfony/Component/Translation/Interval.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Translation;
1313

14+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use IdentityTranslator instead.', Interval::class), E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Translation\Exception\InvalidArgumentException;
1517

1618
/**
@@ -32,6 +34,7 @@
3234
* @author Fabien Potencier <fabien@symfony.com>
3335
*
3436
* @see http://en.wikipedia.org/wiki/Interval_%28mathematics%29#The_ISO_notation
37+
* @deprecated since Symfony 4.2, use IdentityTranslator instead
3538
*/
3639
class Interval
3740
{

src/Symfony/Component/Translation/MessageSelector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
namespace Symfony\Component\Translation;
1313

14+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use IdentityTranslator instead.', MessageSelector::class), E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Translation\Exception\InvalidArgumentException;
1517

1618
/**
1719
* MessageSelector.
1820
*
1921
* @author Fabien Potencier <fabien@symfony.com>
2022
* @author Bernhard Schussek <bschussek@gmail.com>
23+
*
24+
* @deprecated since Symfony 4.2, use IdentityTranslator instead.
2125
*/
2226
class MessageSelector
2327
{

C848 src/Symfony/Component/Translation/PluralizationRules.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* Returns the plural rules for a given locale.
1616
*
1717
* @author Fabien Potencier <fabien@symfony.com>
18+
*
19+
* @deprecated since Symfony 4.2, use IdentityTranslator instead
1820
*/
1921
class PluralizationRules
2022
{
@@ -28,8 +30,12 @@ class PluralizationRules
2830
*
2931
* @return int The plural position
3032
*/
31-
public static function get($number, $locale)
33+
public static function get($number, $locale/*, bool $triggerDeprecation = true*/)
3234
{
35+
if (3 > \func_num_args() || \func_get_arg(2)) {
36+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
37+
}
38+
3339
if ('pt_BR' === $locale) {
3440
// temporary set a locale for brazilian
3541
$locale = 'xbr';
@@ -196,6 +202,8 @@ public static function get($number, $locale)
196202
*/
197203
public static function set(callable $rule, $locale)
198204
{
205+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
206+
199207
if ('pt_BR' === $locale) {
200208
// temporary set a locale for brazilian
201209
$locale = 'xbr';

src/Symfony/Component/Translation/TranslatorInterface.php

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,11 @@
1111

1212
namespace Symfony\Component\Translation;
1313

14-
use Symfony\Component\Translation\Exception\InvalidArgumentException;
14+
use Symfony\Contracts\Translation\TranslatorInterface as BaseTranslatorInterface;
1515

1616
/**
17-
* TranslatorInterface.
18-
*
19-
* @author Fabien Potencier <fabien@symfony.com>
17+
* @deprecated since Symfony 4.2, use the same interface from the Symfony\Contracts\Translation namespace
2018
*/
21-
interface TranslatorInterface
19+
interface TranslatorInterface extends BaseTranslatorInterface
2220
{
23-
/**
24-
* Translates the given message.
25-
*
26-
* @param string $id The message id (may also be an object that can be cast to string)
27-
* @param array $parameters An array of parameters for the message
28-
* @param string|null $domain The domain for the message or null to use the default
29-
* @param string|null $locale The locale or null to use the default
30-
*
31-
* @return string The translated string
32-
*
33-
* @throws InvalidArgumentException If the locale contains invalid characters
34-
*/
35-
public function trans($id, array $parameters = array(), $domain = null, $locale = null);
36-
37-
/**
38-
* Translates the given choice message by choosing a translation according to a number.
39-
*
40-
* @param string $id The message id (may also be an object that can be cast to string)
41-
* @param int $number The number to use to find the indice of the message
42-
* @param array $parameters An array of parameters for the message
43-
* @param string|null $domain The domain for the message or null to use the default
44-
* @param string|null $locale The locale or null to use the default
45-
*
46-
* @return string The translated string
47-
*
48-
* @throws InvalidArgumentException If the locale contains invalid characters
49-
*/
50-
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null);
51-
52-
/**
53-
* Sets the current locale.
54-
*
55-
* @param string $locale The locale
56-
*
57-
* @throws InvalidArgumentException If the locale contains invalid characters
58-
*/
59-
public function setLocale($locale);
60-
61-
/**
62-
* Returns the current locale.
63-
*
64-
* @return string The locale
65-
*/
66-
public function getLocale();
6721
}

src/Symfony/Component/Translation/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.1.3",
20+
"symfony/contracts": "^1.0",
2021
"symfony/polyfill-mbstring": "~1.0"
2122
},
2223
"require-dev": {

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

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

1212
namespace Symfony\Component\Validator\Context;
1313

14-
use Symfony\Component\Translation\TranslatorInterface;
1514
use Symfony\Component\Validator\Constraint;
1615
use Symfony\Component\Validator\ConstraintViolation;
1716
use Symfony\Component\Validator\ConstraintViolationList;
@@ -22,6 +21,7 @@
2221
use Symfony\Component\Validator\Util\PropertyPath;
2322
use Symfony\Component\Validator\Validator\ValidatorInterface;
2423
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
24+
use Symfony\Contracts\Translation\TranslatorInterface;
2525

2626
/**
2727
* The context used and created by {@link ExecutionContextFactory}.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Symfony\Component\Validator\Context;
1313

14-
use Symfony\Component\Translation\TranslatorInterface;
1514
use Symfony\Component\Validator\Validator\ValidatorInterface;
15+
use Symfony\Contracts\Translation\TranslatorInterface;
1616

1717
/**
1818
* Creates new {@link ExecutionContext} instances.

src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Validator\Context\ExecutionContextInterface;
2222
use Symfony\Component\Validator\Mapping\ClassMetadata;
2323
use Symfony\Component\Validator\Mapping\PropertyMetadata;
24+
use Symfony\Contracts\Translation\TranslatorInterface;
2425

2526
/**
2627
* A test case to ease testing Constraint Validators.
@@ -95,7 +96,7 @@ protected function restoreDefaultTimezone()
9596

9697
protected function createContext()
9798
{
98-
$translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
99+
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
99100
$validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock();
100101
$contextualValidator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ContextualValidatorInterface')->getMock();
101102

0 commit comments

Comments
 (0)
0