22
33namespace Webfactory \IcuTranslationBundle \Translator ;
44
5- use Symfony \Component \Translation \TranslatorInterface as LegacyTranslatorInterface ;
5+ use Symfony \Contracts \Translation \LocaleAwareInterface ;
66use Symfony \Contracts \Translation \TranslatorInterface ;
77use 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}
0 commit comments