8000 [TwigBridge] suggest Translation Component when TranslationExtension is used by nicolas-grekas · Pull Request #31761 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge] suggest Translation Component when TranslationExtension is used #31761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ public function __construct($translator = null, NodeVisitorInterface $translatio
}

/**
* @deprecated since Symfony 4.2
* @return TranslatorInterface|null
*/
public function getTranslator()
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED);
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;
}
Expand Down Expand Up @@ -108,31 +116,22 @@ public function trans($message, array $arguments = [], $domain = null, $locale =
if (null !== $count) {
$arguments['%count%'] = $count;
}
if (null === $this->translator) {
$this->translator = new class() implements TranslatorInterface {
use TranslatorTrait;
};
}

return $this->translator->trans($message, $arguments, $domain, $locale);
return $this->getTranslator()->trans($message, $arguments, $domain, $locale);
}

/**
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
*/
public function transchoice($message, $count, array $arguments = [], $domain = null, $locale = null)
{
if (null === $this->translator) {
$this->translator = new class() implements TranslatorInterface {
use TranslatorTrait;
};
}
$translator = $this->getTranslator();

if ($this->translator instanceof TranslatorInterface) {
return $this->translator->trans($message, array_merge(['%count%' => $count], $arguments), $domain, $locale);
if ($translator instanceof TranslatorInterface) {
return $translator->trans($message, array_merge(['%count%' => $count], $arguments), $domain, $locale);
}

return $this->translator->transChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
return $translator->transChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
}

/**
Expand Down
0