Description
Symfony version(s) affected: 4.2.5
Description
I've created an translator decorator, the decorator needs interface TranslatorInterface.
The Symfony\Component\Translation\TranslatorInterface is deprecated but when you want to use the new interface you get an error in prod mode with the deprecated TranslatorInterface this only happens in prod mode in dev mode i don't see an error.
An quick search in the symfony project shows that 'use Symfony\Component\Translation\TranslatorInterface' is still used in 24 places
Dev mode
./bin/console c:c
// Clearing the cache for the dev environment with debug true
[OK] Cache for the "dev" environment (debug=true) was successfully cleared.
Prod mode
./bin/console --env=prod
// Clearing the cache for the prod environment with debug false
In ValidatorBuilder.php line 259:Argument 1 passed to Symfony\Component\Validator\ValidatorBuilder::setTranslator() must implement interface Symfony\Component\Translation\TranslatorInterface, instance of App\Service\Translator\TranslatorDecorator given, called in var/cache/pro_/ContainerYjknBhx/getValidator_BuilderService.php on line 32
How to reproduce
In config/services.yaml add the following line:
app.decorating_translator:
class: App\Service\Translator\TranslatorDecorator
decorates: translator
arguments: ['@app.decorating_translator.inner'] # original translator
public: false
Add file src/Service/Translator/TranslatorDecorator.php
<?php
namespace App\Service\Translator;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class TranslatorDecorator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface
{
/** @var Translator */
private $translator;
/**
* TranslatorDecorator constructor.
*
* @param Translator $translator
*/
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
/**
* @param $id
* @param array $parameters
* @param null $domain
* @param null $locale
*
* @return string|void
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
$parantTrans = $this->translator->trans($id, $parameters, $domain, $locale);
// Check if translation is missing
if ($parantTrans === (string)$id) {
dump($parantTrans);
}
return $parantTrans;
}
/**
* Gets the catalogue by locale.
*
* @param string|null $locale The locale or null to use the default
*
* @return MessageCatalogueInterface
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
public function getCatalogue($locale = null): MessageCatalogueInterface
{
return $this->translator->getCatalogue($locale);
}
/**
* Sets the current locale.
*
* @param string $locale The locale
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
public function setLocale($locale)
{
return $this->translator->setLocale($locale);
}
/**
* Returns the current locale.
*
* @return string The locale
*/
public function getLocale():string
{
return $this->translator->getLocale();
}
}
The do an symfony cache clear ./bin/console cache:clear --env=prod
on prod and the error will appear.