@@ -54,9 +54,9 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
5454 private $ resources = array ();
5555
5656 /**
57- * @var MessageFormatterInterface
57+ * @var MessageFormatterInterface[]
5858 */
59- private $ formatter ;
59+ private $ formatters ;
6060
6161 /**
6262 * @var string
@@ -89,7 +89,7 @@ public function __construct(?string $locale, MessageFormatterInterface $formatte
8989 $ formatter = new MessageFormatter ();
9090 }
9191
92- $ this ->formatter = $ formatter ;
92+ $ this ->formatters [ ' default ' ] = $ formatter ;
9393 $ this ->cacheDir = $ cacheDir ;
9494 $ this ->debug = $ debug ;
9595 }
@@ -137,6 +137,15 @@ public function addResource($format, $resource, $locale, $domain = null)
137137 }
138138 }
139139
140+ /**
141+ * @param string $domain
142+ * @param MessageFormatterInterface $formatter
143+ */
144+ public function addFormatter (string $ domain , MessageFormatterInterface $ formatter )
145+ {
146+ $ this ->formatters [$ domain ] = $ formatter ;
147+ }
148+
140149 /**
141150 * {@inheritdoc}
142151 */
@@ -192,7 +201,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
192201 $ domain = 'messages ' ;
193202 }
194203
195- return $ this ->formatter ->format ($ this ->getCatalogue ($ locale )->get ((string ) $ id , $ domain ), $ locale , $ parameters );
204+ return $ this ->getFormatter ( $ domain ) ->format ($ this ->getCatalogue ($ locale )->get ((string ) $ id , $ domain ), $ locale , $ parameters );
196205 }
197206
198207 /**
@@ -208,6 +217,11 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
208217 $ domain = 'messages ' ;
209218 }
210219
220+ $ formatter = $ this ->getFormatter ($ domain );
221+ if (!$ formatter instanceof ChoiceMessageFormatterInterface) {
222+ throw new LogicException (sprintf ('The formatter "%s" does not support plural translations. ' , get_class ($ formatter )));
223+ }
224+
211225 $ id = (string ) $ id ;
212226 $ catalogue = $ this ->getCatalogue ($ locale );
213227 $ locale = $ catalogue ->getLocale ();
@@ -220,7 +234,7 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
220234 }
221235 }
222236
223- return $ this -> formatter ->choiceFormat ($ catalogue ->get ($ id , $ domain ), $ number , $ locale , $ parameters );
237+ return $ formatter ->choiceFormat ($ catalogue ->get ($ id , $ domain ), $ number , $ locale , $ parameters );
224238 }
225239
226240 /**
@@ -455,4 +469,17 @@ private function getConfigCacheFactory(): ConfigCacheFactoryInterface
455469
456470 return $ this ->configCacheFactory ;
457471 }
472+
473+ /**
474+ * @param string $domain
475+ * @return MessageFormatterInterface
476+ */
477+ private function getFormatter (string $ domain )
478+ {
479+ if (isset ($ this ->formatters [$ domain ])) {
480+ return $ this ->formatters [$ domain ];
481+ }
482+
483+ return $ this ->formatters ['default ' ];
484+ }
458485}
0 commit comments