From 6b0721c767a7be7f15b2fb13c529bea8536230aa Mon Sep 17 00:00:00 2001 From: Tibor Mikoczy Date: Wed, 5 Dec 2018 07:13:26 +0100 Subject: [PATCH] Fix for circular reference issue after upgrading to symfony/translation 4.2.0 (#156) #155 --- src/Console/ExtractCommand.php | 2 +- src/DI/TranslationExtension.php | 12 +++++++++++- src/Translator.php | 18 +++++++++++++----- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Console/ExtractCommand.php b/src/Console/ExtractCommand.php index 04c60133..84697b62 100644 --- a/src/Console/ExtractCommand.php +++ b/src/Console/ExtractCommand.php @@ -109,7 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - $catalogue = new MessageCatalogue($input->getOption('catalogue-language')); + $catalogue = new MessageCatalogue((string) $input->getOption('catalogue-language')); foreach ($this->scanDirs as $dir) { $output->writeln(sprintf('Extracting %s', $dir)); $this->extractor->extract($dir, $catalogue); diff --git a/src/DI/TranslationExtension.php b/src/DI/TranslationExtension.php index a37a2c86..929a8edc 100644 --- a/src/DI/TranslationExtension.php +++ b/src/DI/TranslationExtension.php @@ -42,7 +42,10 @@ use Nette\Utils\Finder; use Nette\Utils\Validators; use Symfony\Component\Translation\Extractor\ChainExtractor; +use Symfony\Component\Translation\Formatter\IntlFormatter; +use Symfony\Component\Translation\Formatter\IntlFormatterInterface; use Symfony\Component\Translation\Formatter\MessageFormatter; +use Symfony\Component\Translation\Formatter\MessageFormatterInterface; use Symfony\Component\Translation\Loader\LoaderInterface; use Symfony\Component\Translation\MessageSelector; use Symfony\Component\Translation\Writer\TranslationWriter; @@ -141,8 +144,15 @@ public function loadConfiguration() $builder->addDefinition($this->prefix('selector')) ->setClass(MessageSelector::class); + if (interface_exists(IntlFormatterInterface::class)) { + $builder->addDefinition($this->prefix('intlFormatter')) + ->setType(IntlFormatterInterface::class) + ->setFactory(IntlFormatter::class); + } + $builder->addDefinition($this->prefix('formatter')) - ->setClass(MessageFormatter::class); + ->setType(MessageFormatterInterface::class) + ->setFactory(MessageFormatter::class); $builder->addDefinition($this->prefix('extractor')) ->setClass(ChainExtractor::class); diff --git a/src/Translator.php b/src/Translator.php index 091fb5ff..d7ff0e84 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -15,7 +15,8 @@ use Nette\Utils\IHtmlString as NetteHtmlString; use Nette\Utils\Strings; use Psr\Log\LoggerInterface; -use Symfony\Component\Translation\Formatter\MessageFormatter; +use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface; +use Symfony\Component\Translation\Formatter\MessageFormatterInterface; use Symfony\Component\Translation\Loader\LoaderInterface; class Translator extends \Symfony\Component\Translation\Translator implements \Kdyby\Translation\ITranslator @@ -69,13 +70,13 @@ class Translator extends \Symfony\Component\Translation\Translator implements \K private $localeWhitelist; /** - * @var \Symfony\Component\Translation\Formatter\MessageFormatter + * @var \Symfony\Component\Translation\Formatter\MessageFormatterInterface */ private $formatter; /** * @param \Kdyby\Translation\IUserLocaleResolver $localeResolver - * @param \Symfony\Component\Translation\Formatter\MessageFormatter $formatter + * @param \Symfony\Component\Translation\Formatter\MessageFormatterInterface $formatter * @param \Kdyby\Translation\CatalogueCompiler $catalogueCompiler * @param \Kdyby\Translation\FallbackResolver $fallbackResolver * @param \Kdyby\Translation\IResourceLoader $loader @@ -83,7 +84,7 @@ class Translator extends \Symfony\Component\Translation\Translator implements \K */ public function __construct( IUserLocaleResolver $localeResolver, - MessageFormatter $formatter, + MessageFormatterInterface $formatter, CatalogueCompiler $catalogueCompiler, FallbackResolver $fallbackResolver, IResourceLoader $loader @@ -247,7 +248,14 @@ public function transChoice($message, $number, array $parameters = [], $domain = $result = strtr($message, $parameters); } else { - $result = $this->formatter->choiceFormat($message, (int) $number, $locale, $parameters); + if (!$this->formatter instanceof ChoiceMessageFormatterInterface) { + $result = $id; + if ($this->panel !== NULL) { + $this->panel->choiceError(new \Symfony\Component\Translation\Exception\LogicException(sprintf('The formatter "%s" does not support plural translations.', get_class($this->formatter))), $domain); + } + } else { + $result = $this->formatter->choiceFormat($message, (int) $number, $locale, $parameters); + } } }