Upgrade 1-11.38

This commit is contained in:
xesmyd
2026-03-30 14:10:30 +02:00
parent f2a7e6d1fc
commit ac648ef29d
24665 changed files with 69682 additions and 2205004 deletions
+99 -44
View File
@@ -19,14 +19,17 @@ use Symfony\Component\Translation\Exception\LogicException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Translation\Exception\RuntimeException;
use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface;
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\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Translator implements TranslatorInterface, TranslatorBagInterface
class Translator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface
{
/**
* @var MessageCatalogueInterface[]
@@ -68,33 +71,36 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
*/
private $debug;
private $cacheVary;
/**
* @var ConfigCacheFactoryInterface|null
*/
private $configCacheFactory;
/**
* @param string $locale The locale
* @param MessageFormatterInterface|null $formatter The message formatter
* @param string|null $cacheDir The directory to use for the cache
* @param bool $debug Use cache in debug mode ?
*
* @var array|null
*/
private $parentLocales;
private $hasIntlFormatter;
/**
* @throws InvalidArgumentException If a locale contains invalid characters
*/
public function __construct($locale, $formatter = null, $cacheDir = null, $debug = false)
public function __construct(?string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false, array $cacheVary = [])
{
$this->setLocale($locale);
if ($formatter instanceof MessageSelector) {
$formatter = new MessageFormatter($formatter);
@trigger_error(sprintf('Passing a "%s" instance into the "%s()" method as a second argument is deprecated since Symfony 3.4 and will be removed in 4.0. Inject a "%s" implementation instead.', MessageSelector::class, __METHOD__, MessageFormatterInterface::class), \E_USER_DEPRECATED);
} elseif (null === $formatter) {
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)
@@ -173,12 +179,14 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
$this->assertValidLocale($locale);
}
$this->fallbackLocales = $locales;
$this->fallbackLocales = $this->cacheVary['fallback_locales'] = $locales;
}
/**
* Gets the fallback locales.
*
* @internal since Symfony 4.2
*
* @return array The fallback locales
*/
public function getFallbackLocales()
@@ -195,14 +203,34 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
$domain = 'messages';
}
return $this->formatter->format($this->getCatalogue($locale)->get((string) $id, $domain), $locale, $parameters);
$id = (string) $id;
$catalogue = $this->getCatalogue($locale);
$locale = $catalogue->getLocale();
while (!$catalogue->defines($id, $domain)) {
if ($cat = $catalogue->getFallbackCatalogue()) {
$catalogue = $cat;
$locale = $catalogue->getLocale();
} else {
break;
}
}
if ($this->hasIntlFormatter && $catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
return $this->formatter->formatIntl($catalogue->get($id, $domain), $locale, $parameters);
}
return $this->formatter->format($catalogue->get($id, $domain), $locale, $parameters);
}
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
*/
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.', __METHOD__), E_USER_DEPRECATED);
if (!$this->formatter instanceof ChoiceMessageFormatterInterface) {
throw new LogicException(sprintf('The formatter "%s" does not support plural translations.', \get_class($this->formatter)));
}
@@ -223,6 +251,10 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
}
}
if ($this->hasIntlFormatter && $catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
return $this->formatter->formatIntl($catalogue->get($id, $domain), $locale, ['%count%' => $number] + $parameters);
}
return $this->formatter->choiceFormat($catalogue->get($id, $domain), $number, $locale, $parameters);
}
@@ -283,10 +315,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. */
@@ -309,7 +338,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
$this->catalogues[$locale] = include $cache->getPath();
}
private function dumpCatalogue($locale, ConfigCacheInterface $cache)
private function dumpCatalogue($locale, ConfigCacheInterface $cache): void
{
$this->initializeCatalogue($locale);
$fallbackContent = $this->getFallbackContent($this->catalogues[$locale]);
@@ -327,14 +356,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 = '';
@@ -353,7 +382,7 @@ EOF
,
$fallbackSuffix,
$fallback,
var_export($fallbackCatalogue->all(), true),
var_export($this->getAllMessages($fallbackCatalogue), true),
$currentSuffix,
$fallbackSuffix
);
@@ -366,28 +395,27 @@ EOF
private function getCatalogueCachePath($locale)
{
return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->fallbackLocales), true)), 0, 7), '/', '_').'.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($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]])) {
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]));
throw new RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
}
$this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
}
}
}
private function loadFallbackCatalogues($locale)
private function loadFallbackCatalogues($locale): void
{
$current = $this->catalogues[$locale];
@@ -396,7 +424,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);
}
@@ -407,6 +435,10 @@ EOF
protected function computeFallbackLocales($locale)
{
if (null === $this->parentLocales) {
$parentLocales = json_decode(file_get_contents(__DIR__.'/Resources/data/parents.json'), true);
}
$locales = [];
foreach ($this->fallbackLocales as $fallback) {
if ($fallback === $locale) {
@@ -416,19 +448,27 @@ EOF
$locales[] = $fallback;
}
if (\function_exists('locale_parse')) {
$localeSubTags = locale_parse($locale);
if (1 < \count($localeSubTags)) {
array_pop($localeSubTags);
$fallback = locale_compose($localeSubTags);
if (false !== $fallback) {
array_unshift($locales, $fallback);
while ($locale) {
$parent = $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) {
array_unshift($locales, $locale);
}
} elseif (false !== strrchr($locale, '_')) {
array_unshift($locales, substr($locale, 0, -\strlen(strrchr($locale, '_'))));
} elseif (false !== strrchr($locale, '-')) {
array_unshift($locales, substr($locale, 0, -\strlen(strrchr($locale, '-'))));
}
return array_unique($locales);
@@ -451,10 +491,8 @@ 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);
@@ -462,4 +500,21 @@ EOF
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;
}
}