Skip to content

Custom Translation Loader not working #41648

Closed
@PascalBieske-Dynalinq

Description

@PascalBieske-Dynalinq

Symfony version(s) affected: 5.2.10

Description
I want to run a multi tenant Symfony applications and for it I want to implement a custom translation file loader.

According to the documentation it should be very easy: https://symfony.com/doc/current/reference/dic_tags.html#dic-tags-translation-loader

But my custom Translation Loader is never called.

How to reproduce
services.yaml

services:
  App\Translation\ProjectYamlLoader:
      tags:
        - { name: translation.loader, alias: yaml }

src/Translation/ProjectYamlLoader.php

<?php

namespace App\Translation;

use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\LogicException;
use Symfony\Component\Translation\Loader\FileLoader;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Yaml\Yaml;

/**
 * YamlFileLoader loads translations from Yaml files.
 *
 */
class ProjectYamlLoader extends FileLoader
{
    private $yamlParser;

    /**
     * {@inheritdoc}
     */
    protected function loadResource($resource)
    {
        if (null === $this->yamlParser) {
            if (!class_exists(\Symfony\Component\Yaml\Parser::class)) {
                throw new LogicException('Loading translations from the YAML format requires the Symfony Yaml component.');
            }

            $this->yamlParser = new YamlParser();
        }

        try {
            $messages = $this->yamlParser->parseFile($resource, Yaml::PARSE_CONSTANT);
        } catch (ParseException $e) {
            throw new InvalidResourceException(sprintf('The file "%s" does not contain valid YAML: ', $resource).$e->getMessage(), 0, $e);
        }

        if (null !== $messages && !\is_array($messages)) {
            throw new InvalidResourceException(sprintf('Unable to load file "%s".', $resource));
        }

        return $messages ?: [];
    }
}

I am sure the class and function loadResource is not called. I added for example a breaking function $die->here() in loadResource, but no error is thrown.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions