Skip to content

Deprecated LegacyTranslatorInterface still in use #31092

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

Closed
dvdknaap opened this issue Apr 12, 2019 · 1 comment
Closed

Deprecated LegacyTranslatorInterface still in use #31092

dvdknaap opened this issue Apr 12, 2019 · 1 comment

Comments

@dvdknaap
Copy link

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.

@nicolas-grekas
Copy link
Member

Correct, that's required to preserve BC, see last comment in #31025.

fabpot pushed a commit that referenced this issue Apr 17, 2019
…or with LegacyTranslatorProxy (nicolas-grekas)

This PR was merged into the 4.2 branch.

Discussion
----------

[FrameworkBundle] decorate the ValidatorBuilder's translator with LegacyTranslatorProxy

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31092, #31025
| License       | MIT
| Doc PR        | -

This allows defining a translator that implements only the new interface and use it with ValidatorBuilder.

ping @dvdknaap, @snebes since you were affected.

Commits
-------

a12656e [FrameworkBundle] decorate the ValidatorBuilder's translator with LegacyTranslatorProxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants