@@ -54,9 +54,9 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
54
54
private $ resources = array ();
55
55
56
56
/**
57
- * @var MessageFormatterInterface
57
+ * @var MessageFormatterInterface[]
58
58
*/
59
- private $ formatter ;
59
+ private $ formatters ;
60
60
61
61
/**
62
62
* @var string
@@ -89,7 +89,7 @@ public function __construct(?string $locale, MessageFormatterInterface $formatte
89
89
$ formatter = new MessageFormatter ();
90
90
}
91
91
92
- $ this ->formatter = $ formatter ;
92
+ $ this ->formatters [ ' default ' ] = $ formatter ;
93
93
$ this ->cacheDir = $ cacheDir ;
94
94
$ this ->debug = $ debug ;
95
95
}
@@ -137,6 +137,15 @@ public function addResource($format, $resource, $locale, $domain = null)
137
137
}
138
138
}
139
139
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
+
140
149
/**
141
150
* {@inheritdoc}
142
151
*/
@@ -192,7 +201,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
192
201
$ domain = 'messages ' ;
193
202
}
194
203
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 );
196
205
}
197
206
198
207
/**
@@ -208,6 +217,11 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
208
217
$ domain = 'messages ' ;
209
218
}
210
219
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
+
211
225
$ id = (string ) $ id ;
212
226
$ catalogue = $ this ->getCatalogue ($ locale );
213
227
$ locale = $ catalogue ->getLocale ();
@@ -220,7 +234,7 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
220
234
}
221
235
}
222
236
223
- return $ this -> formatter ->choiceFormat ($ catalogue ->get ($ id , $ domain ), $ number , $ locale , $ parameters );
237
+ return $ formatter ->choiceFormat ($ catalogue ->get ($ id , $ domain ), $ number , $ locale , $ parameters );
224
238
}
225
239
226
240
/**
@@ -455,4 +469,17 @@ private function getConfigCacheFactory(): ConfigCacheFactoryInterface
455
469
456
470
return $ this ->configCacheFactory ;
457
471
}
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
+ }
458
485
}
0 commit comments