Désactivation des traductions pour l'environnement de test avec Symfony

Publié le 31/01/2023 • Actualisé le 31/01/2023


English language detected! 🇬🇧

  We noticed that your browser is using English. Do you want to read this post in this language?

Read the english version 🇬🇧 Close

Dans ce bout de code, nous voyons comment désactivation des traductions pour l'environnement de test avec Symfony. Le but est de tester les codes de traduction au lieu des textes traduits pour une langue donnée. Ça rend les tests plus robustes, car ils ne casseront pas si un texte donné est modifié. Vous pouvez utiliser la classe suivante qui décore le service translator existant. La configuration à appliquer est dans la propriété yaml afin de pouvoir être copiée-collée facilement. Vous pouvez la supprimer du service une fois dans la configuration. Veuillez noter que c'est une version simple : elle ne prend pas en charge les paramètres, mais uniquement les codes de traduction.


<?php

declare(strict_types=1);

namespace App\Translator;

use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * Disable translations in the test env.
 *
 * @see https://jolicode.com/blog/how-to-properly-manage-translations-in-symfony
 */
final class NoTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface
{
    public string $yaml;

    /**
     * @param TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator
     */
    public function __construct(
        private readonly TranslatorInterface $translator
    ) {
        $this->yaml = <<<HEREDOC

when@test:
    services:
        // Disable the translations to test on codes instead of the translated text
        App\Translator\NoTranslator:
            decorates: translator
            arguments:
                - '@App\Translator\NoTranslator.inner'

HEREDOC;
    }

    /**
     * We don't test parameters for now. My advice would be: don't test parameters
     * until you have to fix a related bug reported or if you use them a lot.
     *
     * @param array<mixed> $parameters
     */
    public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
    {
        // to find EasyAdmin translations codes, uncomment this
        //         if ($domain === 'EasyAdminBundle') {
        //             dump($id);
        //         }

        return $id;
    }

    /**
     * @return array<MessageCatalogueInterface>
     */
    public function getCatalogues(): array
    {
        return $this->translator->getCatalogues();
    }

    public function getCatalogue(?string $locale = null): MessageCatalogueInterface
    {
        return $this->translator->getCatalogue($locale);
    }

    public function setLocale(string $locale): void
    {
        $this->translator->setLocale($locale);
    }

    public function getLocale(): string
    {
        return $this->translator->getLocale();
    }
}

 Plus sur le web  Snippet aléatoire

  Travaillez avec moi !