[英]Symfony 5.3 custom FileLoader not working
我想运行一个多租户 Symfony(5.3 版)应用程序,为此我想实现一个自定义翻译文件加载器。
根据 Symfony 文档,它应该很容易: https://symfony.com/doc/current/reference/dic_tags.html#dic-tags-translation-loader
但是,它对我不起作用。 我已将 class 和标签添加到服务中。
App\Translation\ProjectYamlLoader:
tags:
- { name: translation.loader, alias: yaml }
这是加载程序文件
<?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)
{
$die->here();
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 ?: [];
}
}
我的功能不起作用,所以我添加了$die->here();
用于调试目的。 我现在确定没有调用 class,因为没有错误抛出。
我错过了什么? 为什么这不起作用?
无法添加yml
加载器,因为.yml
已经是 Symfony 有加载器的扩展。
解决方案是覆盖该加载程序:
# services.yaml
translation.loader.yml:
alias: App\Translation\ProjectYamlLoader
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.