Upgrade framework
This commit is contained in:
290
vendor/symfony/translation/Translator.php
vendored
290
vendor/symfony/translation/Translator.php
vendored
@@ -11,89 +11,78 @@
|
||||
|
||||
namespace Symfony\Component\Translation;
|
||||
|
||||
use Symfony\Component\Translation\Loader\LoaderInterface;
|
||||
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
||||
use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Translation\Exception\RuntimeException;
|
||||
use Symfony\Component\Config\ConfigCacheInterface;
|
||||
use Symfony\Component\Config\ConfigCacheFactoryInterface;
|
||||
use Symfony\Component\Config\ConfigCacheFactory;
|
||||
use Symfony\Component\Config\ConfigCacheFactoryInterface;
|
||||
use Symfony\Component\Config\ConfigCacheInterface;
|
||||
use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
||||
use Symfony\Component\Translation\Exception\RuntimeException;
|
||||
use Symfony\Component\Translation\Formatter\IntlFormatterInterface;
|
||||
use Symfony\Component\Translation\Formatter\MessageFormatter;
|
||||
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
|
||||
use Symfony\Component\Translation\Loader\LoaderInterface;
|
||||
use Symfony\Contracts\Translation\LocaleAwareInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
// Help opcache.preload discover always-needed symbols
|
||||
class_exists(MessageCatalogue::class);
|
||||
|
||||
/**
|
||||
* Translator.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var MessageCatalogueInterface[]
|
||||
*/
|
||||
protected $catalogues = array();
|
||||
protected $catalogues = [];
|
||||
|
||||
private string $locale;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @var string[]
|
||||
*/
|
||||
private $locale;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $fallbackLocales = array();
|
||||
private array $fallbackLocales = [];
|
||||
|
||||
/**
|
||||
* @var LoaderInterface[]
|
||||
*/
|
||||
private $loaders = array();
|
||||
private array $loaders = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $resources = array();
|
||||
private array $resources = [];
|
||||
|
||||
/**
|
||||
* @var MessageSelector
|
||||
*/
|
||||
private $selector;
|
||||
private $formatter;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $cacheDir;
|
||||
private ?string $cacheDir;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $debug;
|
||||
private bool $debug;
|
||||
|
||||
private array $cacheVary;
|
||||
|
||||
/**
|
||||
* @var ConfigCacheFactoryInterface|null
|
||||
*/
|
||||
private $configCacheFactory;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $locale The locale
|
||||
* @param MessageSelector|null $selector The message selector for pluralization
|
||||
* @param string|null $cacheDir The directory to use for the cache
|
||||
* @param bool $debug Use cache in debug mode ?
|
||||
*
|
||||
* @throws InvalidArgumentException If a locale contains invalid characters
|
||||
*/
|
||||
public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false)
|
||||
{
|
||||
$this->setLocale($locale);
|
||||
$this->selector = $selector ?: new MessageSelector();
|
||||
$this->cacheDir = $cacheDir;
|
||||
$this->debug = $debug;
|
||||
}
|
||||
private array $parentLocales;
|
||||
|
||||
private bool $hasIntlFormatter;
|
||||
|
||||
/**
|
||||
* Sets the ConfigCache factory to use.
|
||||
*
|
||||
* @param ConfigCacheFactoryInterface $configCacheFactory
|
||||
* @throws InvalidArgumentException If a locale contains invalid characters
|
||||
*/
|
||||
public function __construct(string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false, array $cacheVary = [])
|
||||
{
|
||||
$this->setLocale($locale);
|
||||
|
||||
if (null === $formatter) {
|
||||
$formatter = new MessageFormatter();
|
||||
}
|
||||
|
||||
$this->formatter = $formatter;
|
||||
$this->cacheDir = $cacheDir;
|
||||
$this->debug = $debug;
|
||||
$this->cacheVary = $cacheVary;
|
||||
$this->hasIntlFormatter = $formatter instanceof IntlFormatterInterface;
|
||||
}
|
||||
|
||||
public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
|
||||
{
|
||||
$this->configCacheFactory = $configCacheFactory;
|
||||
@@ -102,10 +91,9 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
/**
|
||||
* Adds a Loader.
|
||||
*
|
||||
* @param string $format The name of the loader (@see addResource())
|
||||
* @param LoaderInterface $loader A LoaderInterface instance
|
||||
* @param string $format The name of the loader (@see addResource())
|
||||
*/
|
||||
public function addLoader($format, LoaderInterface $loader)
|
||||
public function addLoader(string $format, LoaderInterface $loader)
|
||||
{
|
||||
$this->loaders[$format] = $loader;
|
||||
}
|
||||
@@ -115,23 +103,22 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
*
|
||||
* @param string $format The name of the loader (@see addLoader())
|
||||
* @param mixed $resource The resource name
|
||||
* @param string $locale The locale
|
||||
* @param string $domain The domain
|
||||
*
|
||||
* @throws InvalidArgumentException If the locale contains invalid characters
|
||||
*/
|
||||
public function addResource($format, $resource, $locale, $domain = null)
|
||||
public function addResource(string $format, mixed $resource, string $locale, string $domain = null)
|
||||
{
|
||||
if (null === $domain) {
|
||||
$domain = 'messages';
|
||||
}
|
||||
|
||||
$this->assertValidLocale($locale);
|
||||
$locale ?: $locale = class_exists(\Locale::class) ? \Locale::getDefault() : 'en';
|
||||
|
||||
$this->resources[$locale][] = array($format, $resource, $domain);
|
||||
$this->resources[$locale][] = [$format, $resource, $domain];
|
||||
|
||||
if (in_array($locale, $this->fallbackLocales)) {
|
||||
$this->catalogues = array();
|
||||
if (\in_array($locale, $this->fallbackLocales)) {
|
||||
$this->catalogues = [];
|
||||
} else {
|
||||
unset($this->catalogues[$locale]);
|
||||
}
|
||||
@@ -140,7 +127,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setLocale($locale)
|
||||
public function setLocale(string $locale)
|
||||
{
|
||||
$this->assertValidLocale($locale);
|
||||
$this->locale = $locale;
|
||||
@@ -149,36 +136,36 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLocale()
|
||||
public function getLocale(): string
|
||||
{
|
||||
return $this->locale;
|
||||
return $this->locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the fallback locales.
|
||||
*
|
||||
* @param array $locales The fallback locales
|
||||
* @param string[] $locales
|
||||
*
|
||||
* @throws InvalidArgumentException If a locale contains invalid characters
|
||||
*/
|
||||
public function setFallbackLocales(array $locales)
|
||||
{
|
||||
// needed as the fallback locales are linked to the already loaded catalogues
|
||||
$this->catalogues = array();
|
||||
$this->catalogues = [];
|
||||
|
||||
foreach ($locales as $locale) {
|
||||
$this->assertValidLocale($locale);
|
||||
}
|
||||
|
||||
$this->fallbackLocales = $locales;
|
||||
$this->fallbackLocales = $this->cacheVary['fallback_locales'] = $locales;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fallback locales.
|
||||
*
|
||||
* @return array $locales The fallback locales
|
||||
* @internal
|
||||
*/
|
||||
public function getFallbackLocales()
|
||||
public function getFallbackLocales(): array
|
||||
{
|
||||
return $this->fallbackLocales;
|
||||
}
|
||||
@@ -186,29 +173,16 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
|
||||
public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string
|
||||
{
|
||||
if (null === $domain) {
|
||||
$domain = 'messages';
|
||||
if (null === $id || '' === $id) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return strtr($this->getCatalogue($locale)->get((string) $id, $domain), $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
|
||||
{
|
||||
$parameters = array_merge(array(
|
||||
'%count%' => $number,
|
||||
), $parameters);
|
||||
|
||||
if (null === $domain) {
|
||||
$domain = 'messages';
|
||||
}
|
||||
|
||||
$id = (string) $id;
|
||||
$catalogue = $this->getCatalogue($locale);
|
||||
$locale = $catalogue->getLocale();
|
||||
while (!$catalogue->defines($id, $domain)) {
|
||||
@@ -220,15 +194,23 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
}
|
||||
}
|
||||
|
||||
return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
|
||||
$len = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX);
|
||||
if ($this->hasIntlFormatter
|
||||
&& ($catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)
|
||||
|| (\strlen($domain) > $len && 0 === substr_compare($domain, MessageCatalogue::INTL_DOMAIN_SUFFIX, -$len, $len)))
|
||||
) {
|
||||
return $this->formatter->formatIntl($catalogue->get($id, $domain), $locale, $parameters);
|
||||
}
|
||||
|
||||
return $this->formatter->format($catalogue->get($id, $domain), $locale, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCatalogue($locale = null)
|
||||
public function getCatalogue(string $locale = null): MessageCatalogueInterface
|
||||
{
|
||||
if (null === $locale) {
|
||||
if (!$locale) {
|
||||
$locale = $this->getLocale();
|
||||
} else {
|
||||
$this->assertValidLocale($locale);
|
||||
@@ -241,20 +223,25 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
return $this->catalogues[$locale];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCatalogues(): array
|
||||
{
|
||||
return array_values($this->catalogues);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the loaders.
|
||||
*
|
||||
* @return array LoaderInterface[]
|
||||
* @return LoaderInterface[]
|
||||
*/
|
||||
protected function getLoaders()
|
||||
protected function getLoaders(): array
|
||||
{
|
||||
return $this->loaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
*/
|
||||
protected function loadCatalogue($locale)
|
||||
protected function loadCatalogue(string $locale)
|
||||
{
|
||||
if (null === $this->cacheDir) {
|
||||
$this->initializeCatalogue($locale);
|
||||
@@ -263,10 +250,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
*/
|
||||
protected function initializeCatalogue($locale)
|
||||
protected function initializeCatalogue(string $locale)
|
||||
{
|
||||
$this->assertValidLocale($locale);
|
||||
|
||||
@@ -280,10 +264,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
$this->loadFallbackCatalogues($locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
*/
|
||||
private function initializeCacheCatalogue($locale)
|
||||
private function initializeCacheCatalogue(string $locale): void
|
||||
{
|
||||
if (isset($this->catalogues[$locale])) {
|
||||
/* Catalogue already initialized. */
|
||||
@@ -306,7 +287,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
$this->catalogues[$locale] = include $cache->getPath();
|
||||
}
|
||||
|
||||
private function dumpCatalogue($locale, ConfigCacheInterface $cache)
|
||||
private function dumpCatalogue(string $locale, ConfigCacheInterface $cache): void
|
||||
{
|
||||
$this->initializeCatalogue($locale);
|
||||
$fallbackContent = $this->getFallbackContent($this->catalogues[$locale]);
|
||||
@@ -324,14 +305,14 @@ return \$catalogue;
|
||||
EOF
|
||||
,
|
||||
$locale,
|
||||
var_export($this->catalogues[$locale]->all(), true),
|
||||
var_export($this->getAllMessages($this->catalogues[$locale]), true),
|
||||
$fallbackContent
|
||||
);
|
||||
|
||||
$cache->write($content, $this->catalogues[$locale]->getResources());
|
||||
}
|
||||
|
||||
private function getFallbackContent(MessageCatalogue $catalogue)
|
||||
private function getFallbackContent(MessageCatalogue $catalogue): string
|
||||
{
|
||||
$fallbackContent = '';
|
||||
$current = '';
|
||||
@@ -350,7 +331,7 @@ EOF
|
||||
,
|
||||
$fallbackSuffix,
|
||||
$fallback,
|
||||
var_export($fallbackCatalogue->all(), true),
|
||||
var_export($this->getAllMessages($fallbackCatalogue), true),
|
||||
$currentSuffix,
|
||||
$fallbackSuffix
|
||||
);
|
||||
@@ -361,26 +342,33 @@ EOF
|
||||
return $fallbackContent;
|
||||
}
|
||||
|
||||
private function getCatalogueCachePath($locale)
|
||||
private function getCatalogueCachePath(string $locale): string
|
||||
{
|
||||
return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php';
|
||||
return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->cacheVary), true)), 0, 7), '/', '_').'.php';
|
||||
}
|
||||
|
||||
private function doLoadCatalogue($locale)
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected function doLoadCatalogue(string $locale): void
|
||||
{
|
||||
$this->catalogues[$locale] = new MessageCatalogue($locale);
|
||||
|
||||
if (isset($this->resources[$locale])) {
|
||||
foreach ($this->resources[$locale] as $resource) {
|
||||
if (!isset($this->loaders[$resource[0]])) {
|
||||
throw new RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
|
||||
if (\is_string($resource[1])) {
|
||||
throw new RuntimeException(sprintf('No loader is registered for the "%s" format when loading the "%s" resource.', $resource[0], $resource[1]));
|
||||
}
|
||||
|
||||
throw new RuntimeException(sprintf('No loader is registered for the "%s" format.', $resource[0]));
|
||||
}
|
||||
$this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function loadFallbackCatalogues($locale)
|
||||
private function loadFallbackCatalogues(string $locale): void
|
||||
{
|
||||
$current = $this->catalogues[$locale];
|
||||
|
||||
@@ -389,7 +377,7 @@ EOF
|
||||
$this->initializeCatalogue($fallback);
|
||||
}
|
||||
|
||||
$fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all());
|
||||
$fallbackCatalogue = new MessageCatalogue($fallback, $this->getAllMessages($this->catalogues[$fallback]));
|
||||
foreach ($this->catalogues[$fallback]->getResources() as $resource) {
|
||||
$fallbackCatalogue->addResource($resource);
|
||||
}
|
||||
@@ -398,34 +386,55 @@ EOF
|
||||
}
|
||||
}
|
||||
|
||||
protected function computeFallbackLocales($locale)
|
||||
protected function computeFallbackLocales(string $locale)
|
||||
{
|
||||
$locales = array();
|
||||
$this->parentLocales ??= json_decode(file_get_contents(__DIR__.'/Resources/data/parents.json'), true);
|
||||
|
||||
$originLocale = $locale;
|
||||
$locales = [];
|
||||
|
||||
while ($locale) {
|
||||
$parent = $this->parentLocales[$locale] ?? null;
|
||||
|
||||
if ($parent) {
|
||||
$locale = 'root' !== $parent ? $parent : null;
|
||||
} elseif (\function_exists('locale_parse')) {
|
||||
$localeSubTags = locale_parse($locale);
|
||||
$locale = null;
|
||||
if (1 < \count($localeSubTags)) {
|
||||
array_pop($localeSubTags);
|
||||
$locale = locale_compose($localeSubTags) ?: null;
|
||||
}
|
||||
} elseif ($i = strrpos($locale, '_') ?: strrpos($locale, '-')) {
|
||||
$locale = substr($locale, 0, $i);
|
||||
} else {
|
||||
$locale = null;
|
||||
}
|
||||
|
||||
if (null !== $locale) {
|
||||
$locales[] = $locale;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->fallbackLocales as $fallback) {
|
||||
if ($fallback === $locale) {
|
||||
if ($fallback === $originLocale) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$locales[] = $fallback;
|
||||
}
|
||||
|
||||
if (strrchr($locale, '_') !== false) {
|
||||
array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
|
||||
}
|
||||
|
||||
return array_unique($locales);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the locale is valid, throws an Exception if not.
|
||||
*
|
||||
* @param string $locale Locale to tests
|
||||
*
|
||||
* @throws InvalidArgumentException If the locale contains invalid characters
|
||||
*/
|
||||
protected function assertValidLocale($locale)
|
||||
protected function assertValidLocale(string $locale)
|
||||
{
|
||||
if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
|
||||
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
|
||||
}
|
||||
}
|
||||
@@ -433,15 +442,28 @@ EOF
|
||||
/**
|
||||
* Provides the ConfigCache factory implementation, falling back to a
|
||||
* default implementation if necessary.
|
||||
*
|
||||
* @return ConfigCacheFactoryInterface $configCacheFactory
|
||||
*/
|
||||
private function getConfigCacheFactory()
|
||||
private function getConfigCacheFactory(): ConfigCacheFactoryInterface
|
||||
{
|
||||
if (!$this->configCacheFactory) {
|
||||
$this->configCacheFactory = new ConfigCacheFactory($this->debug);
|
||||
}
|
||||
$this->configCacheFactory ??= new ConfigCacheFactory($this->debug);
|
||||
|
||||
return $this->configCacheFactory;
|
||||
}
|
||||
|
||||
private function getAllMessages(MessageCatalogueInterface $catalogue): array
|
||||
{
|
||||
$allMessages = [];
|
||||
|
||||
foreach ($catalogue->all() as $domain => $messages) {
|
||||
if ($intlMessages = $catalogue->all($domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
|
||||
$allMessages[$domain.MessageCatalogue::INTL_DOMAIN_SUFFIX] = $intlMessages;
|
||||
$messages = array_diff_key($messages, $intlMessages);
|
||||
}
|
||||
if ($messages) {
|
||||
$allMessages[$domain] = $messages;
|
||||
}
|
||||
}
|
||||
|
||||
return $allMessages;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user