mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-01 22:48:45 +02:00
migration symfony 5 4 (#300)
* symfony 5.4 (diff dev) * symfony 5.4 (working) * symfony 5.4 (update autoload) * symfony 5.4 (remove swiftmailer mailer implementation) * symfony 5.4 (php doc and split Global accessor class) ### Impacted packages: composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
@@ -13,63 +13,80 @@ namespace Symfony\Bridge\Twig\Extension;
|
||||
|
||||
use Symfony\Bridge\Twig\NodeVisitor\TranslationDefaultDomainNodeVisitor;
|
||||
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
|
||||
use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
|
||||
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
|
||||
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Translation\TranslatableMessage;
|
||||
use Symfony\Contracts\Translation\TranslatableInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorTrait;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\NodeVisitor\NodeVisitorInterface;
|
||||
use Twig\TokenParser\AbstractTokenParser;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
// Help opcache.preload discover always-needed symbols
|
||||
class_exists(TranslatorInterface::class);
|
||||
class_exists(TranslatorTrait::class);
|
||||
|
||||
/**
|
||||
* Provides integration of the Translation component with Twig.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class TranslationExtension extends AbstractExtension
|
||||
final class TranslationExtension extends AbstractExtension
|
||||
{
|
||||
private $translator;
|
||||
private $translationNodeVisitor;
|
||||
|
||||
public function __construct(TranslatorInterface $translator = null, NodeVisitorInterface $translationNodeVisitor = null)
|
||||
public function __construct(TranslatorInterface $translator = null, TranslationNodeVisitor $translationNodeVisitor = null)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
$this->translationNodeVisitor = $translationNodeVisitor;
|
||||
}
|
||||
|
||||
public function getTranslator()
|
||||
public function getTranslator(): TranslatorInterface
|
||||
{
|
||||
if (null === $this->translator) {
|
||||
if (!interface_exists(TranslatorInterface::class)) {
|
||||
throw new \LogicException(sprintf('You cannot use the "%s" if the Translation Contracts are not available. Try running "composer require symfony/translation".', __CLASS__));
|
||||
}
|
||||
|
||||
$this->translator = new class() implements TranslatorInterface {
|
||||
use TranslatorTrait;
|
||||
};
|
||||
}
|
||||
|
||||
return $this->translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFilters()
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('trans', [$this, 'trans']),
|
||||
new TwigFilter('transchoice', [$this, 'transchoice']),
|
||||
new TwigFunction('t', [$this, 'createTranslatable']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the token parser instance to add to the existing list.
|
||||
*
|
||||
* @return AbstractTokenParser[]
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTokenParsers()
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('trans', [$this, 'trans']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTokenParsers(): array
|
||||
{
|
||||
return [
|
||||
// {% trans %}Symfony is great!{% endtrans %}
|
||||
new TransTokenParser(),
|
||||
|
||||
// {% transchoice count %}
|
||||
// {0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples
|
||||
// {% endtranschoice %}
|
||||
new TransChoiceTokenParser(),
|
||||
|
||||
// {% trans_default_domain "foobar" %}
|
||||
new TransDefaultDomainTokenParser(),
|
||||
];
|
||||
@@ -78,39 +95,51 @@ class TranslationExtension extends AbstractExtension
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNodeVisitors()
|
||||
public function getNodeVisitors(): array
|
||||
{
|
||||
return [$this->getTranslationNodeVisitor(), new TranslationDefaultDomainNodeVisitor()];
|
||||
}
|
||||
|
||||
public function getTranslationNodeVisitor()
|
||||
public function getTranslationNodeVisitor(): TranslationNodeVisitor
|
||||
{
|
||||
return $this->translationNodeVisitor ?: $this->translationNodeVisitor = new TranslationNodeVisitor();
|
||||
}
|
||||
|
||||
public function trans($message, array $arguments = [], $domain = null, $locale = null)
|
||||
{
|
||||
if (null === $this->translator) {
|
||||
return strtr($message, $arguments);
|
||||
}
|
||||
|
||||
return $this->translator->trans($message, $arguments, $domain, $locale);
|
||||
}
|
||||
|
||||
public function transchoice($message, $count, array $arguments = [], $domain = null, $locale = null)
|
||||
{
|
||||
if (null === $this->translator) {
|
||||
return strtr($message, $arguments);
|
||||
}
|
||||
|
||||
return $this->translator->transChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @param string|\Stringable|TranslatableInterface|null $message
|
||||
* @param array|string $arguments Can be the locale as a string when $message is a TranslatableInterface
|
||||
*/
|
||||
public function getName()
|
||||
public function trans($message, $arguments = [], string $domain = null, string $locale = null, int $count = null): string
|
||||
{
|
||||
return 'translator';
|
||||
if ($message instanceof TranslatableInterface) {
|
||||
if ([] !== $arguments && !\is_string($arguments)) {
|
||||
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be a locale passed as a string when the message is a "%s", "%s" given.', __METHOD__, TranslatableInterface::class, get_debug_type($arguments)));
|
||||
}
|
||||
|
||||
return $message->trans($this->getTranslator(), $locale ?? (\is_string($arguments) ? $arguments : null));
|
||||
}
|
||||
|
||||
if (!\is_array($arguments)) {
|
||||
throw new \TypeError(sprintf('Unless the message is a "%s", argument 2 passed to "%s()" must be an array of parameters, "%s" given.', TranslatableInterface::class, __METHOD__, get_debug_type($arguments)));
|
||||
}
|
||||
|
||||
if ('' === $message = (string) $message) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (null !== $count) {
|
||||
$arguments['%count%'] = $count;
|
||||
}
|
||||
|
||||
return $this->getTranslator()->trans($message, $arguments, $domain, $locale);
|
||||
}
|
||||
|
||||
public function createTranslatable(string $message, array $parameters = [], string $domain = null): TranslatableMessage
|
||||
{
|
||||
if (!class_exists(TranslatableMessage::class)) {
|
||||
throw new \LogicException(sprintf('You cannot use the "%s" as the Translation Component is not installed. Try running "composer require symfony/translation".', __CLASS__));
|
||||
}
|
||||
|
||||
return new TranslatableMessage($message, $parameters, $domain);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user