Skip to content

Commit 07f7806

Browse files
[Contracts] Add Translation\TranslatorInterface + decouple symfony/validator from symfony/translation
1 parent 6355b76 commit 07f7806

14 files changed

+71
-156
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
-----
66

77
* Started using ICU parent locales as fallback locales.
8+
* deprecated `TranslatorInterface` in favor of `Symfony\Contracts\Translation\TranslatorInterface`
9+
* deprecated `MessageSelector`, `Interval` and `PluralizationRules`; use `IdentityTranslator` instead
810

911
4.1.0
1012
-----

DataCollectorTranslator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
namespace Symfony\Component\Translation;
1313

1414
use Symfony\Component\Translation\Exception\InvalidArgumentException;
15+
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
16+
use Symfony\Contracts\Translation\TranslatorInterface;
1517

1618
/**
1719
* @author Abdellatif Ait boudad <[email protected]>
1820
*/
19-
class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInterface
21+
class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorBagInterface
2022
{
2123
const MESSAGE_DEFINED = 0;
2224
const MESSAGE_MISSING = 1;

Formatter/MessageFormatter.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@
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 <[email protected]>
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 $translator An 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+
if ($translator instanceof MessageSelector) {
31+
$translator = new IdentityTranslator($translator);
32+
} elseif (null !== $translator && !$translator instanceof TranslatorInterface) {
33+
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)));
34+
}
35+
36+
$this->translator = $translator ?? new IdentityTranslator();
2937
}
3038

3139
/**
@@ -43,6 +51,6 @@ public function choiceFormat($message, $number, $locale, array $parameters = arr
4351
{
4452
$parameters = array_merge(array('%count%' => $number), $parameters);
4553

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

IdentityTranslator.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,47 @@
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 <[email protected]>
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-
}
34+
$this->selector = $selector;
3135

32-
/**
33-
* {@inheritdoc}
34-
*/
35-
public function setLocale($locale)
36-
{
37-
$this->locale = $locale;
36+
if (\get_class($this) !== __CLASS__) {
37+
@trigger_error(sprintf('Calling "%s()" is deprecated since Symfony 4.2.'), E_USER_DEPRECATED);
38+
}
3839
}
3940

4041
/**
4142
* {@inheritdoc}
4243
*/
43-
public function getLocale()
44+
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
4445
{
45-
return $this->locale ?: \Locale::getDefault();
46-
}
46+
if ($this->selector) {
47+
return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
48+
}
4749

48-
/**
49-
* {@inheritdoc}
50-
*/
51-
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
52-
{
53-
return strtr((string) $id, $parameters);
50+
return $this->doTransChoice($id, $number, $parameters, $domain, $locale);
5451
}
5552

56-
/**
57-
* {@inheritdoc}
58-
*/
59-
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
53+
private function getPluralizationRule(int $number, string $locale): int
6054
{
61-
return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
55+
return PluralizationRules::get($number, $locale, false);
6256
}
6357
}

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 <[email protected]>
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
{

LoggingTranslator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\Translation\Exception\InvalidArgumentException;
16+
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
1618

1719
/**
1820
* @author Abdellatif Ait boudad <[email protected]>
1921
*/
20-
class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface
22+
class LoggingTranslator implements LegacyTranslatorInterface, TranslatorBagInterface
2123
{
2224
/**
2325
* @var TranslatorInterface|TranslatorBagInterface

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 <[email protected]>
2022
* @author Bernhard Schussek <[email protected]>
23+
*
24+
* @deprecated since Symfony 4.2, use IdentityTranslator instead.
2125
*/
2226
class MessageSelector
2327
{

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 <[email protected]>
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';

Tests/IdentityTranslatorTest.php

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

1212
namespace Symfony\Component\Translation\Tests;
1313

14-
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Intl\Util\IntlTestHelper;
1614
use Symfony\Component\Translation\IdentityTranslator;
15+
use Symfony\Contracts\Tests\Translation\TranslatorTest;
1716

18-
class IdentityTranslatorTest extends TestCase
17+
class IdentityTranslatorTest extends TranslatorTest
1918
{
20-
/**
21-
* @dataProvider getTransTests
22-
*/
23-
public function testTrans($expected, $id, $parameters)
19+
public function getTranslator()
2420
{
25-
$translator = new IdentityTranslator();
26-
27-
$this->assertEquals($expected, $translator->trans($id, $parameters));
28-
}
29-
30-
/**
31-
* @dataProvider getTransChoiceTests
32-
*/
33-
public function testTransChoiceWithExplicitLocale($expected, $id, $number, $parameters)
34-
{
35-
$translator = new IdentityTranslator();
36-
$translator->setLocale('en');
37-
38-
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
39-
}
40-
41-
/**
42-
* @dataProvider getTransChoiceTests
43-
*/
44-
public function testTransChoiceWithDefaultLocale($expected, $id, $number, $parameters)
45-
{
46-
\Locale::setDefault('en');
47-
48-
$translator = new IdentityTranslator();
49-
50-
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
51-
}
52-
53-
public function testGetSetLocale()
54-
{
55-
$translator = new IdentityTranslator();
56-
$translator->setLocale('en');
57-
58-
$this->assertEquals('en', $translator->getLocale());
59-
}
60-
61-
public function testGetLocaleReturnsDefaultLocaleIfNotSet()
62-
{
63-
// in order to test with "pt_BR"
64-
IntlTestHelper::requireFullIntl($this, false);
65-
66-
$translator = new IdentityTranslator();
67-
68-
\Locale::setDefault('en');
69-
$this->assertEquals('en', $translator->getLocale());
70-
71-
\Locale::setDefault('pt_BR');
72-
$this->assertEquals('pt_BR', $translator->getLocale());
73-
}
74-
75-
public function getTransTests()
76-
{
77-
return array(
78-
array('Symfony is great!', 'Symfony is great!', array()),
79-
array('Symfony is awesome!', 'Symfony is %what%!', array('%what%' => 'awesome')),
80-
);
81-
}
82-
83-
public function getTransChoiceTests()
84-
{
85-
return array(
86-
array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0, array('%count%' => 0)),
87-
array('There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1, array('%count%' => 1)),
88-
array('There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10, array('%count%' => 10)),
89-
array('There are 0 apples', 'There is 1 apple|There are %count% apples', 0, array('%count%' => 0)),
90-
array('There is 1 apple', 'There is 1 apple|There are %count% apples', 1, array('%count%' => 1)),
91-
array('There are 10 apples', 'There is 1 apple|There are %count% apples', 10, array('%count%' => 10)),
92-
// custom validation messages may be coded with a fixed value
93-
array('There are 2 apples', 'There are 2 apples', 2, array('%count%' => 2)),
94-
);
21+
return new IdentityTranslator();
9522
}
9623
}

Tests/IntervalTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Translation\Interval;
1616

17+
/**
18+
* @group legacy
19+
*/
1720
class IntervalTest extends TestCase
1821
{
1922
/**

0 commit comments

Comments
 (0)