Mercurial > hg > isophonics-drupal-site
diff 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 |
line wrap: on
line diff
--- a/vendor/symfony/translation/Translator.php Mon Apr 23 09:33:26 2018 +0100 +++ b/vendor/symfony/translation/Translator.php Mon Apr 23 09:46:53 2018 +0100 @@ -14,14 +14,16 @@ use Symfony\Component\Translation\Loader\LoaderInterface; use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Translation\Exception\InvalidArgumentException; +use Symfony\Component\Translation\Exception\LogicException; use Symfony\Component\Translation\Exception\RuntimeException; use Symfony\Component\Config\ConfigCacheInterface; use Symfony\Component\Config\ConfigCacheFactoryInterface; use Symfony\Component\Config\ConfigCacheFactory; +use Symfony\Component\Translation\Formatter\MessageFormatterInterface; +use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface; +use Symfony\Component\Translation\Formatter\MessageFormatter; /** - * Translator. - * * @author Fabien Potencier <fabien@symfony.com> */ class Translator implements TranslatorInterface, TranslatorBagInterface @@ -52,9 +54,9 @@ private $resources = array(); /** - * @var MessageSelector + * @var MessageFormatterInterface */ - private $selector; + private $formatter; /** * @var string @@ -72,28 +74,29 @@ private $configCacheFactory; /** - * Constructor. - * - * @param string $locale The locale - * @param MessageSelector|null $selector The message selector for pluralization - * @param string|null $cacheDir The directory to use for the cache - * @param bool $debug Use cache in debug mode ? + * @param string $locale The locale + * @param MessageFormatterInterface|null $formatter The message formatter + * @param string|null $cacheDir The directory to use for the cache + * @param bool $debug Use cache in debug mode ? * * @throws InvalidArgumentException If a locale contains invalid characters */ - public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false) + public function __construct($locale, $formatter = null, $cacheDir = null, $debug = false) { $this->setLocale($locale); - $this->selector = $selector ?: new MessageSelector(); + + if ($formatter instanceof MessageSelector) { + $formatter = new MessageFormatter($formatter); + @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); + } elseif (null === $formatter) { + $formatter = new MessageFormatter(); + } + + $this->formatter = $formatter; $this->cacheDir = $cacheDir; $this->debug = $debug; } - /** - * Sets the ConfigCache factory to use. - * - * @param ConfigCacheFactoryInterface $configCacheFactory - */ public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory) { $this->configCacheFactory = $configCacheFactory; @@ -192,7 +195,7 @@ $domain = 'messages'; } - return strtr($this->getCatalogue($locale)->get((string) $id, $domain), $parameters); + return $this->formatter->format($this->getCatalogue($locale)->get((string) $id, $domain), $locale, $parameters); } /** @@ -200,9 +203,9 @@ */ public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) { - $parameters = array_merge(array( - '%count%' => $number, - ), $parameters); + if (!$this->formatter instanceof ChoiceMessageFormatterInterface) { + throw new LogicException(sprintf('The formatter "%s" does not support plural translations.', get_class($this->formatter))); + } if (null === $domain) { $domain = 'messages'; @@ -220,7 +223,7 @@ } } - return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters); + return $this->formatter->choiceFormat($catalogue->get($id, $domain), $number, $locale, $parameters); } /** @@ -363,7 +366,7 @@ private function getCatalogueCachePath($locale) { - return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php'; + return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->fallbackLocales), true)), 0, 7), '/', '_').'.php'; } private function doLoadCatalogue($locale) @@ -409,7 +412,7 @@ $locales[] = $fallback; } - if (strrchr($locale, '_') !== false) { + if (false !== strrchr($locale, '_')) { array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_')))); }