symfony/translation · error · InvalidArgumentException

Class " " used for service " " cannot be found.

Error message

Class "%s" used for service "%s" cannot be found.

What it means

Thrown by LoggingTranslatorPass::process when the class resolved from the 'translator' service definition cannot be reflected, i.e. the class does not exist or cannot be autoloaded. The compiler pass needs to inspect the class to check it implements TranslatorInterface and TranslatorBagInterface before wiring the logging translator decorator.

Solutions

  1. Verify the class name on the 'translator' service definition (or the class the 'translator' alias points to) exists and is autoloadable with `class_exists()`
  2. Fix the class name or FQCN typo in the service definition / parameter
  3. Clear and warm the Symfony cache (cache:clear) after renaming classes
  4. If you intentionally have no translator, ensure the 'translator' alias and its definition are removed so the pass is skipped

Example fix

// before (services.yaml)
App\Translator\MyTraductor: ~
// after
App\Translator\MyTranslator: ~
Defensive patterns

Strategy: validation

Validate before calling

if (!class_exists($class = $container->getParameterBag()->resolveValue($definition->getClass()))) { throw new \LogicException("Translator class {$class} does not exist"); }

Type guard

function translatorClassExists(string $class): bool { return class_exists($class); }

Try / catch

try { $pass->process($container); } catch (InvalidArgumentException $e) { // translator class missing: fix service definition }

Prevention

When it happens

Trigger: Running the Symfony container compile with a 'translator' alias/definition whose resolved class does not exist — typically when 'translator.class' parameter is wrong, a custom translator class name is mistyped, or the class file was removed/renamed without updating the service definition.

Common situations: Custom Translator class renamed without updating services.yaml; typo in class name in a service definition; compiling a container in a context where the translator class is not autoloadable; stale cached parameters defining an old class name.

Related errors


AI-assisted analysis of symfony/translation@ae9e8a51bc (2026-09-15). Data as JSON: /api/errors/f026f97fd2d87bc1. Report an issue: GitHub.

Appendix: source

Thrown at DependencyInjection/LoggingTranslatorPass.php:40

 */
class LoggingTranslatorPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container): void
    {
        if (!$container->hasAlias('logger') || !$container->hasAlias('translator')) {
            return;
        }

        if (!$container->hasParameter('translator.logging') || !$container->getParameter('translator.logging')) {
            return;
        }

        $translatorAlias = $container->getAlias('translator');
        $definition = $container->getDefinition((string) $translatorAlias);
        $class = $container->getParameterBag()->resolveValue($definition->getClass());

        if (!$r = $container->getReflectionClass($class)) {
            throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $translatorAlias));
        }

        if (!$r->isSubclassOf(TranslatorInterface::class) || !$r->isSubclassOf(TranslatorBagInterface::class)) {
            return;
        }

        $container->getDefinition('translator.logging')->setDecoratedService('translator');
        $warmer = $container->getDefinition('translation.warmer');
        $subscriberAttributes = $warmer->getTag('container.service_subscriber');
        $warmer->clearTag('container.service_subscriber');

        foreach ($subscriberAttributes as $k => $v) {
            if ((!isset($v['id']) || 'translator' !== $v['id']) && (!isset($v['key']) || 'translator' !== $v['key'])) {
                $warmer->addTag('container.service_subscriber', $v);
            }
        }
        $warmer->addTag('container.service_subscriber', ['key' => 'translator', 'id' => 'translator.logging.inner']);
    }

View on GitHub (pinned to ae9e8a51bc)