Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b43fe21

Browse files
committed
Add support for multiple formatters
1 parent c2b3dc0 commit b43fe21

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/Symfony/Component/Translation/Formatter/IntlMessageFormatter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @author Guilherme Blanco <[email protected]>
1616
* @author Abdellatif Ait boudad <[email protected]>
1717
*/
18-
class IntlMessageFormatter implements MessageFormatterInterface
18+
class IntlMessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
1919
{
2020
/**
2121
* {@inheritdoc}
@@ -34,4 +34,12 @@ public function format($message, $locale, array $parameters = array())
3434

3535
return $message;
3636
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function choiceFormat($message, $number, $locale, array $parameters = array())
42+
{
43+
return $this->format($message, $locale, $parameters);
44+
}
3745
}

src/Symfony/Component/Translation/Translator.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)