comparison vendor/symfony/translation/Translator.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
12 namespace Symfony\Component\Translation; 12 namespace Symfony\Component\Translation;
13 13
14 use Symfony\Component\Translation\Loader\LoaderInterface; 14 use Symfony\Component\Translation\Loader\LoaderInterface;
15 use Symfony\Component\Translation\Exception\NotFoundResourceException; 15 use Symfony\Component\Translation\Exception\NotFoundResourceException;
16 use Symfony\Component\Translation\Exception\InvalidArgumentException; 16 use Symfony\Component\Translation\Exception\InvalidArgumentException;
17 use Symfony\Component\Translation\Exception\LogicException;
17 use Symfony\Component\Translation\Exception\RuntimeException; 18 use Symfony\Component\Translation\Exception\RuntimeException;
18 use Symfony\Component\Config\ConfigCacheInterface; 19 use Symfony\Component\Config\ConfigCacheInterface;
19 use Symfony\Component\Config\ConfigCacheFactoryInterface; 20 use Symfony\Component\Config\ConfigCacheFactoryInterface;
20 use Symfony\Component\Config\ConfigCacheFactory; 21 use Symfony\Component\Config\ConfigCacheFactory;
22 use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
23 use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface;
24 use Symfony\Component\Translation\Formatter\MessageFormatter;
21 25
22 /** 26 /**
23 * Translator.
24 *
25 * @author Fabien Potencier <fabien@symfony.com> 27 * @author Fabien Potencier <fabien@symfony.com>
26 */ 28 */
27 class Translator implements TranslatorInterface, TranslatorBagInterface 29 class Translator implements TranslatorInterface, TranslatorBagInterface
28 { 30 {
29 /** 31 /**
50 * @var array 52 * @var array
51 */ 53 */
52 private $resources = array(); 54 private $resources = array();
53 55
54 /** 56 /**
55 * @var MessageSelector 57 * @var MessageFormatterInterface
56 */ 58 */
57 private $selector; 59 private $formatter;
58 60
59 /** 61 /**
60 * @var string 62 * @var string
61 */ 63 */
62 private $cacheDir; 64 private $cacheDir;
70 * @var ConfigCacheFactoryInterface|null 72 * @var ConfigCacheFactoryInterface|null
71 */ 73 */
72 private $configCacheFactory; 74 private $configCacheFactory;
73 75
74 /** 76 /**
75 * Constructor. 77 * @param string $locale The locale
76 * 78 * @param MessageFormatterInterface|null $formatter The message formatter
77 * @param string $locale The locale 79 * @param string|null $cacheDir The directory to use for the cache
78 * @param MessageSelector|null $selector The message selector for pluralization 80 * @param bool $debug Use cache in debug mode ?
79 * @param string|null $cacheDir The directory to use for the cache
80 * @param bool $debug Use cache in debug mode ?
81 * 81 *
82 * @throws InvalidArgumentException If a locale contains invalid characters 82 * @throws InvalidArgumentException If a locale contains invalid characters
83 */ 83 */
84 public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false) 84 public function __construct($locale, $formatter = null, $cacheDir = null, $debug = false)
85 { 85 {
86 $this->setLocale($locale); 86 $this->setLocale($locale);
87 $this->selector = $selector ?: new MessageSelector(); 87
88 if ($formatter instanceof MessageSelector) {
89 $formatter = new MessageFormatter($formatter);
90 @trigger_error(sprintf('Passing a "%s" instance into the "%s" as a second argument is deprecated since Symfony 3.4 and will be removed in 4.0. Inject a "%s" implementation instead.', MessageSelector::class, __METHOD__, MessageFormatterInterface::class), E_USER_DEPRECATED);
91 } elseif (null === $formatter) {
92 $formatter = new MessageFormatter();
93 }
94
95 $this->formatter = $formatter;
88 $this->cacheDir = $cacheDir; 96 $this->cacheDir = $cacheDir;
89 $this->debug = $debug; 97 $this->debug = $debug;
90 } 98 }
91 99
92 /**
93 * Sets the ConfigCache factory to use.
94 *
95 * @param ConfigCacheFactoryInterface $configCacheFactory
96 */
97 public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory) 100 public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
98 { 101 {
99 $this->configCacheFactory = $configCacheFactory; 102 $this->configCacheFactory = $configCacheFactory;
100 } 103 }
101 104
190 { 193 {
191 if (null === $domain) { 194 if (null === $domain) {
192 $domain = 'messages'; 195 $domain = 'messages';
193 } 196 }
194 197
195 return strtr($this->getCatalogue($locale)->get((string) $id, $domain), $parameters); 198 return $this->formatter->format($this->getCatalogue($locale)->get((string) $id, $domain), $locale, $parameters);
196 } 199 }
197 200
198 /** 201 /**
199 * {@inheritdoc} 202 * {@inheritdoc}
200 */ 203 */
201 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) 204 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
202 { 205 {
203 $parameters = array_merge(array( 206 if (!$this->formatter instanceof ChoiceMessageFormatterInterface) {
204 '%count%' => $number, 207 throw new LogicException(sprintf('The formatter "%s" does not support plural translations.', get_class($this->formatter)));
205 ), $parameters); 208 }
206 209
207 if (null === $domain) { 210 if (null === $domain) {
208 $domain = 'messages'; 211 $domain = 'messages';
209 } 212 }
210 213
218 } else { 221 } else {
219 break; 222 break;
220 } 223 }
221 } 224 }
222 225
223 return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters); 226 return $this->formatter->choiceFormat($catalogue->get($id, $domain), $number, $locale, $parameters);
224 } 227 }
225 228
226 /** 229 /**
227 * {@inheritdoc} 230 * {@inheritdoc}
228 */ 231 */
361 return $fallbackContent; 364 return $fallbackContent;
362 } 365 }
363 366
364 private function getCatalogueCachePath($locale) 367 private function getCatalogueCachePath($locale)
365 { 368 {
366 return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php'; 369 return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->fallbackLocales), true)), 0, 7), '/', '_').'.php';
367 } 370 }
368 371
369 private function doLoadCatalogue($locale) 372 private function doLoadCatalogue($locale)
370 { 373 {
371 $this->catalogues[$locale] = new MessageCatalogue($locale); 374 $this->catalogues[$locale] = new MessageCatalogue($locale);
407 } 410 }
408 411
409 $locales[] = $fallback; 412 $locales[] = $fallback;
410 } 413 }
411 414
412 if (strrchr($locale, '_') !== false) { 415 if (false !== strrchr($locale, '_')) {
413 array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_')))); 416 array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
414 } 417 }
415 418
416 return array_unique($locales); 419 return array_unique($locales);
417 } 420 }