I want to run a multi tenant Symfony (version 5.3) applications and for it I want to implement a custom translation file loader.
According to the Symfony documentation it should be very easy: https://symfony.com/doc/current/reference/dic_tags.html#dic-tags-translation-loader
However, it is not working for me. I have added the class and tag to the services.
AppTranslationProjectYamlLoader: tags: - { name: translation.loader, alias: yaml }
And this is the Loader file
<?php namespace AppTranslation; use SymfonyComponentTranslationExceptionInvalidResourceException; use SymfonyComponentTranslationExceptionLogicException; use SymfonyComponentTranslationLoaderFileLoader; use SymfonyComponentYamlExceptionParseException; use SymfonyComponentYamlParser as YamlParser; use SymfonyComponentYamlYaml; /** * YamlFileLoader loads translations from Yaml files. * */ class ProjectYamlLoader extends FileLoader { private $yamlParser; /** * {@inheritdoc} */ protected function loadResource($resource) { $die->here(); if (null === $this->yamlParser) { if (!class_exists(SymfonyComponentYamlParser::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 ?: []; } }
My functionality wasn’t working so I added the $die->here();
for debug puropses. I am now sure the class is not called, because there is no error throw.
What did I miss? Why is this not working?
Advertisement
Answer
It is not possible to add a yml
loader, because .yml
is already an extension for which Symfony has a loader.
The solution is to overriding that loader:
# services.yaml translation.loader.yml: alias: AppTranslationProjectYamlLoader