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
|
<?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 array
*/
private $resourceLocales;
/**
* Holds parameters from addResource() calls so we can defer the actual
* parent::addResource() calls until initialize() is executed.
*
* @var array
*/
private $resources = [];
private $resourceFiles;
/**
* @var string[]
*/
private $scannedDirectories;
/**
* 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 = [])
{
$this->container = $container;
$this->loaderIds = $loaderIds;
// 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']);
}
/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
{
// skip warmUp when translator doesn't use cache
if (null === $this->options['cache_dir']) {
return;
}
$locales = array_merge($this->getFallbackLocales(), [$this->getLocale()], $this->resourceLocales);
foreach (array_unique($locales) 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);
}
}
public function addResource($format, $resource, $locale, $domain = null)
{
if ($this->resourceFiles) {
$this->addResourceFiles();
}
$this->resources[] = [$format, $resource, $locale, $domain];
}
/**
* {@inheritdoc}
*/
protected function initializeCatalogue($locale)
{
$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));
}
}
protected function initialize()
{
if ($this->resourceFiles) {
$this->addResourceFiles();
}
foreach ($this->resources as $key => $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()
{
$filesByLocale = $this->resourceFiles;
$this->resourceFiles = [];
foreach ($filesByLocale as $locale => $files) {
foreach ($files as $key => $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);
}
}
}
}
|