Spaces:
Runtime error
Runtime error
File size: 5,934 Bytes
7a66774 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | <?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Translation;
use Psr\Container\ContainerInterface;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileExistenceResource;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
use Symfony\Component\Translation\Translator as BaseTranslator;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Translator extends BaseTranslator implements WarmableInterface
{
protected $container;
protected $loaderIds;
protected $options = [
'cache_dir' => null,
'debug' => false,
'resource_files' => [],
'scanned_directories' => [],
'cache_vary' => [],
];
/**
* @var list<string>
*/
private array $resourceLocales;
/**
* Holds parameters from addResource() calls so we can defer the actual
* parent::addResource() calls until initialize() is executed.
*
* @var array[]
*/
private array $resources = [];
/**
* @var string[][]
*/
private array $resourceFiles;
/**
* @var string[]
*/
private array $scannedDirectories;
/**
* @var string[]
*/
private array $enabledLocales;
/**
* Constructor.
*
* Available options:
*
* * cache_dir: The cache directory (or null to disable caching)
* * debug: Whether to enable debugging or not (false by default)
* * resource_files: List of translation resources available grouped by locale.
* * cache_vary: An array of data that is serialized to generate the cached catalogue name.
*
* @throws InvalidArgumentException
*/
public function __construct(ContainerInterface $container, MessageFormatterInterface $formatter, string $defaultLocale, array $loaderIds = [], array $options = [], array $enabledLocales = [])
{
$this->container = $container;
$this->loaderIds = $loaderIds;
$this->enabledLocales = $enabledLocales;
// check option names
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
throw new InvalidArgumentException(\sprintf('The Translator does not support the following options: \'%s\'.', implode('\', \'', $diff)));
}
$this->options = array_merge($this->options, $options);
$this->resourceLocales = array_keys($this->options['resource_files']);
$this->resourceFiles = $this->options['resource_files'];
$this->scannedDirectories = $this->options['scanned_directories'];
parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug'], $this->options['cache_vary']);
}
/**
* @param string|null $buildDir
*/
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
{
// skip warmUp when translator doesn't use cache
if (null === $this->options['cache_dir']) {
return [];
}
$localesToWarmUp = $this->enabledLocales ?: array_merge($this->getFallbackLocales(), [$this->getLocale()], $this->resourceLocales);
foreach (array_unique($localesToWarmUp) as $locale) {
// reset catalogue in case it's already loaded during the dump of the other locales.
if (isset($this->catalogues[$locale])) {
unset($this->catalogues[$locale]);
}
$this->loadCatalogue($locale);
}
return [];
}
public function addResource(string $format, mixed $resource, string $locale, ?string $domain = null): void
{
if ($this->resourceFiles) {
$this->addResourceFiles();
}
$this->resources[] = [$format, $resource, $locale, $domain];
}
protected function initializeCatalogue(string $locale): void
{
$this->initialize();
parent::initializeCatalogue($locale);
}
/**
* @internal
*/
protected function doLoadCatalogue(string $locale): void
{
parent::doLoadCatalogue($locale);
foreach ($this->scannedDirectories as $directory) {
$resourceClass = file_exists($directory) ? DirectoryResource::class : FileExistenceResource::class;
$this->catalogues[$locale]->addResource(new $resourceClass($directory));
}
}
/**
* @return void
*/
protected function initialize()
{
if ($this->resourceFiles) {
$this->addResourceFiles();
}
foreach ($this->resources as $params) {
[$format, $resource, $locale, $domain] = $params;
parent::addResource($format, $resource, $locale, $domain);
}
$this->resources = [];
foreach ($this->loaderIds as $id => $aliases) {
foreach ($aliases as $alias) {
$this->addLoader($alias, $this->container->get($id));
}
}
}
private function addResourceFiles(): void
{
$filesByLocale = $this->resourceFiles;
$this->resourceFiles = [];
foreach ($filesByLocale as $files) {
foreach ($files as $file) {
// filename is domain.locale.format
$fileNameParts = explode('.', basename($file));
$format = array_pop($fileNameParts);
$locale = array_pop($fileNameParts);
$domain = implode('.', $fileNameParts);
$this->addResource($format, $file, $locale, $domain);
}
}
}
}
|