Skip to content

Commit

Permalink
Fix for circular reference issue after upgrading to symfony/translati…
Browse files Browse the repository at this point in the history
…on 4.2.0 (#156)

#155
  • Loading branch information
mikoczy authored and enumag committed Dec 5, 2018
1 parent b892e00 commit 6b0721c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Console/ExtractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<info>Extracting %s</info>', $dir));
$this->extractor->extract($dir, $catalogue);
Expand Down
12 changes: 11 additions & 1 deletion src/DI/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 13 additions & 5 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,21 +70,21 @@ 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
* @throws \InvalidArgumentException
*/
public function __construct(
IUserLocaleResolver $localeResolver,
MessageFormatter $formatter,
MessageFormatterInterface $formatter,
CatalogueCompiler $catalogueCompiler,
FallbackResolver $fallbackResolver,
IResourceLoader $loader
Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 6b0721c

Please sign in to comment.