Skip to content

Commit 5a7abb0

Browse files
authored
Remove Translator-related interfaces that were deprecated in Symfony ^4.2 (#28)
After #29 (released as `0.8.0`) implemented the new interfaces from `symfony/translation-contracts` in addition to the deprecated ones, this PR drops support for the old `\Symfony\Component\Translation\TranslatorInterface`.
1 parent f910c37 commit 5a7abb0

File tree

5 files changed

+42
-119
lines changed

5 files changed

+42
-119
lines changed

src/Translator/FormatterDecorator.php

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@
22

33
namespace Webfactory\IcuTranslationBundle\Translator;
44

5-
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
5+
use Symfony\Contracts\Translation\LocaleAwareInterface;
66
use Symfony\Contracts\Translation\TranslatorInterface;
77
use Webfactory\IcuTranslationBundle\Translator\Formatting\FormatterInterface;
8+
use Webfactory\IcuTranslationBundle\Translator\Formatting\IntlFormatter;
89

910
/**
1011
* Decorates a Symfony translator and adds support for message formatting.
1112
*/
12-
class FormatterDecorator implements LegacyTranslatorInterface, TranslatorInterface
13+
class FormatterDecorator implements TranslatorInterface, LocaleAwareInterface
1314
{
1415
/**
1516
* The inner translator.
1617
*
17-
* @var \Symfony\Component\Translation\TranslatorInterface
18+
* @var TranslatorInterface
1819
*/
1920
protected $translator;
2021

2122
/**
2223
* The formatter that is used to apply message transformations.
2324
*
24-
* @var \Webfactory\IcuTranslationBundle\Translator\Formatting\IntlFormatter
25+
* @var IntlFormatter
2526
*/
2627
protected $formatter;
2728

28-
/**
29-
* Creates a decorator for the provided translator.
30-
*
31-
* @param \Webfactory\IcuTranslationBundle\Translator\Formatting\FormatterInterface the formatter that is used
32-
*/
33-
public function __construct(LegacyTranslatorInterface $translator, FormatterInterface $formatter)
29+
public function __construct(TranslatorInterface $translator, FormatterInterface $formatter)
3430
{
31+
if (!$translator instanceof LocaleAwareInterface) {
32+
throw new \InvalidArgumentException(sprintf('The translator passed to "%s()" must implement "%s".', __METHOD__, LocaleAwareInterface::class));
33+
}
34+
3535
$this->translator = $translator;
3636
$this->formatter = $formatter;
3737
}
@@ -43,34 +43,14 @@ public function __construct(LegacyTranslatorInterface $translator, FormatterInte
4343
* @param array $parameters An array of parameters for the message
4444
* @param string $domain The domain for the message
4545
* @param string $locale The locale
46-
*
47-
* @return string The translated string
4846
*/
49-
public function trans($id, array $parameters = [], $domain = null, $locale = null)
47+
public function trans($id, array $parameters = [], $domain = null, $locale = null): string
5048
{
5149
$message = $this->translator->trans($id, $parameters, $domain, $locale);
5250

5351
return $this->handleFormatting($id, $message, $parameters, $locale);
5452
}
5553

56-
/**
57-
* Translates the given choice message by choosing a translation according to a number.
58-
*
59-
* @param string $id The message id
60-
* @param int $number The number to use to find the indice of the message
61-
* @param array $parameters An array of parameters for the message
62-
* @param string $domain The domain for the message
63-
* @param string $locale The locale
64-
*
65-
* @return string The translated string
66-
*/
67-
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
68-
{
69-
$message = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
70-
71-
return $this->handleFormatting($id, $message, $parameters, $locale);
72-
}
73-
7454
/**
7555
* Sets the current locale.
7656
*
@@ -81,35 +61,25 @@ public function setLocale($locale)
8161
$this->translator->setLocale($locale);
8262
}
8363

84-
/**
85-
* Returns the current locale.
86-
*
87-
* @return string The locale
88-
*/
89-
public function getLocale()
64+
public function getLocale(): string
9065
{
9166
return $this->translator->getLocale();
9267
}
9368

9469
/**
9570
* Formats the message if possible and throws a normalized exception in case of error.
9671
*
97-
* @param string $id the translation message ID
98-
* @param string $message the message pattern that will be used for formatting
99-
* @param array(mixed) $parameters
100-
* @param string|null $locale
101-
*
102-
* @return string the formatted message
103-
*
10472
* @throws \Webfactory\IcuTranslationBundle\Translator\FormattingException if formatting fails
10573
*/
106-
protected function handleFormatting($id, $message, array $parameters, $locale)
74+
protected function handleFormatting(string $id, string $message, array $parameters = [], string $locale = null): string
10775
{
10876
if (empty($message)) {
10977
// No formatting needed.
11078
return $message;
11179
}
80+
11281
$locale = $this->toLocale($locale);
82+
11383
try {
11484
return $this->format($message, $parameters, $locale);
11585
} catch (\Exception $e) {
@@ -119,14 +89,8 @@ protected function handleFormatting($id, $message, array $parameters, $locale)
11989

12090
/**
12191
* Applies Intl formatting on the provided message.
122-
*
123-
* @param string $message
124-
* @param array(mixed) $parameters
125-
* @param string $locale
126-
*
127-
* @return string
12892
*/
129-
protected function format($message, array $parameters, $locale)
93+
protected function format(string $message, array $parameters = [], string $locale = null): string
13094
{
13195
return $this->formatter->format($locale, $message, $parameters);
13296
}
@@ -136,13 +100,9 @@ protected function format($message, array $parameters, $locale)
136100
*
137101
* If a correct locale is provided that one is used.
138102
* Otherwise, the default locale is returned.
139-
*
140-
* @param string|null $locale
141-
*
142-
* @return string
143103
*/
144-
protected function toLocale($locale = null)
104+
protected function toLocale(string $locale = null): string
145105
{
146-
return (null === $locale) ? $this->getLocale() : $locale;
106+
return $locale ?? $this->getLocale();
147107
}
148108
}

src/Twig/IcuFormattingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Webfactory\IcuTranslationBundle\Twig;
44

5-
use Symfony\Component\Translation\TranslatorInterface;
5+
use Symfony\Contracts\Translation\TranslatorInterface;
66
use Twig\Extension\AbstractExtension;
77
use Twig\TwigFilter;
88
use Webfactory\IcuTranslationBundle\Translator\Formatting\FormatterInterface;

tests/Functional/SymfonyIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Webfactory\TranslationBundle\Tests\Functional;
44

55
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6-
use Symfony\Component\Translation\TranslatorInterface;
6+
use Symfony\Contracts\Translation\TranslatorInterface;
77
use Webfactory\IcuTranslationBundle\Translator\FormatterDecorator;
88

99
class SymfonyIntegrationTest extends KernelTestCase

tests/Functional/TranslationFormattingTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,6 @@ public function translatorResolvesSelectExpressionCorrectly()
7575
self::assertEquals('She is tested.', $this->translator->trans($message, ['%gender%' => 'female']));
7676
}
7777

78-
/**
79-
* Ensures that the translator formats messages that are returned by transChoice().
80-
*
81-
* @test
82-
*/
83-
public function translatorFormatsMessagesReturnedByTransChoice()
84-
{
85-
$message = 'We need {0,number,integer} tests.';
86-
self::assertEquals('We need 42 tests.', $this->translator->transChoice($message, 7, ['%0%' => 42]));
87-
}
88-
8978
/**
9079
* Ensures that the translator resolves an expression as expected when the checked variable
9180
* is not passed.

tests/Translator/FormatterDecoratorTest.php

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Webfactory\IcuTranslationBundle\Tests\Translator;
44

5+
use PHPUnit\Framework\MockObject\MockObject;
56
use PHPUnit\Framework\TestCase;
7+
use Symfony\Contracts\Translation\LocaleAwareInterface;
8+
use Symfony\Contracts\Translation\TranslatorInterface;
69
use Webfactory\IcuTranslationBundle\Translator\FormatterDecorator;
10+
use Webfactory\IcuTranslationBundle\Translator\Formatting\FormatterInterface;
711
use Webfactory\IcuTranslationBundle\Translator\FormattingException;
812

913
/**
@@ -14,21 +18,21 @@ class FormatterDecoratorTest extends TestCase
1418
/**
1519
* System under test.
1620
*
17-
* @var \Webfactory\IcuTranslationBundle\Translator\FormatterDecorator
21+
* @var FormatterDecorator
1822
*/
1923
protected $decorator = null;
2024

2125
/**
2226
* The simulated inner translator.
2327
*
24-
* @var \PHPUnit_Framework_MockObject_MockObject
28+
* @var MockObject
2529
*/
2630
protected $translator = null;
2731

2832
/**
2933
* The mocked formatter that is used in the tests.
3034
*
31-
* @var \PHPUnit_Framework_MockObject_MockObject
35+
* @var MockObject
3236
*/
3337
protected $formatter = null;
3438

@@ -38,33 +42,22 @@ class FormatterDecoratorTest extends TestCase
3842
protected function setUp(): void
3943
{
4044
parent::setUp();
41-
$this->translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
42-
$this->formatter = $this->createMock('Webfactory\IcuTranslationBundle\Translator\Formatting\FormatterInterface');
45+
$this->translator = $this->createMock(LocaleAwareTranslator::class);
46+
$this->formatter = $this->createMock(FormatterInterface::class);
4347
$this->decorator = new FormatterDecorator(
4448
$this->translator,
4549
$this->formatter
4650
);
4751
}
4852

49-
/**
50-
* Cleans up the test environment.
51-
*/
52-
protected function tearDown(): void
53-
{
54-
$this->decorator = null;
55-
$this->formatter = null;
56-
$this->translator = null;
57-
parent::tearDown();
58-
}
59-
6053
/**
6154
* Checks if the decorator implements the Translator interface.
6255
*
6356
* @test
6457
*/
6558
public function implementsTranslatorInterface()
6659
{
67-
$this->assertInstanceOf('Symfony\Component\Translation\TranslatorInterface', $this->decorator);
60+
$this->assertInstanceOf(TranslatorInterface::class, $this->decorator);
6861
}
6962

7063
/**
@@ -76,37 +69,12 @@ public function decoratorForwardsTransCalls()
7669
{
7770
$this->translator->expects($this->once())
7871
->method('trans')
72+
->with('test', ['foo' => 'bar'], 'domain', 'locale')
7973
->willReturn('test');
8074

81-
$this->decorator->trans('test');
82-
}
83-
84-
/**
85-
* Checks if the decorator forwards calls to transChoice() to the inner translator.
86-
*
87-
* @test
88-
*/
89-
public function decoratorForwardsTransChoiceCalls()
90-
{
91-
$this->translator->expects($this->once())
92-
->method('transChoice')
93-
->willReturn('test');
94-
95-
$this->decorator->transChoice('test', 42);
96-
}
75+
$this->formatter->method('format')->willReturn('some string');
9776

98-
/**
99-
* Checks if the decorator forwards calls to setLocale() to the inner translator.
100-
*
101-
* @test
102-
*/
103-
public function decoratorForwardsSetLocaleCalls()
104-
{
105-
$this->translator->expects($this->once())
106-
->method('setLocale')
107-
->with('de_DE');
108-
109-
$this->decorator->setLocale('de_DE');
77+
$this->decorator->trans('test', ['foo' => 'bar'], 'domain', 'locale');
11078
}
11179

11280
/**
@@ -130,14 +98,16 @@ public function getLocaleReturnsLocaleFromInnerTranslator()
13098
*/
13199
public function decoratorPassesResultFromTranslatorToFormatter()
132100
{
101+
$this->translator->method('getLocale')->willReturn('some_locale');
133102
$this->translator->expects($this->once())
134103
->method('trans')
135104
->willReturn('test message');
136105
$this->formatter->expects($this->once())
137106
->method('format')
138-
->with($this->anything(), 'test message');
107+
->with($this->anything(), 'test message')
108+
->willReturn('some string');
139109

140-
$this->decorator->trans('message_id');
110+
$this->decorator->trans('message_id', ['foo' => 'bar'], 'domain', 'locale');
141111
}
142112

143113
/**
@@ -155,7 +125,7 @@ public function decoratorReturnsResultFromFormatter()
155125
->method('format')
156126
->willReturn('formatted message');
157127

158-
$translated = $this->decorator->trans('message_id');
128+
$translated = $this->decorator->trans('message_id', ['foo' => 'bar'], 'domain', 'locale');
159129

160130
$this->assertEquals('formatted message', $translated);
161131
}
@@ -178,3 +148,7 @@ public function decoratorNormalizesFormatterException()
178148
$this->decorator->trans('test', ['test' => 'value'], 'messages', 'en');
179149
}
180150
}
151+
152+
interface LocaleAwareTranslator extends TranslatorInterface, LocaleAwareInterface
153+
{
154+
}

0 commit comments

Comments
 (0)