updated-packages
This commit is contained in:
130
vendor/symfony/translation/Translator.php
vendored
130
vendor/symfony/translation/Translator.php
vendored
@@ -34,7 +34,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
/**
|
||||
* @var MessageCatalogueInterface[]
|
||||
*/
|
||||
protected $catalogues = array();
|
||||
protected $catalogues = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
@@ -44,17 +44,17 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $fallbackLocales = array();
|
||||
private $fallbackLocales = [];
|
||||
|
||||
/**
|
||||
* @var LoaderInterface[]
|
||||
*/
|
||||
private $loaders = array();
|
||||
private $loaders = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $resources = array();
|
||||
private $resources = [];
|
||||
|
||||
/**
|
||||
* @var MessageFormatterInterface
|
||||
@@ -71,6 +71,8 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
*/
|
||||
private $debug;
|
||||
|
||||
private $cacheVary;
|
||||
|
||||
/**
|
||||
* @var ConfigCacheFactoryInterface|null
|
||||
*/
|
||||
@@ -86,9 +88,13 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
/**
|
||||
* @throws InvalidArgumentException If a locale contains invalid characters
|
||||
*/
|
||||
public function __construct(?string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false)
|
||||
public function __construct(?string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false, array $cacheVary = [])
|
||||
{
|
||||
$this->setLocale($locale);
|
||||
if (null === $locale) {
|
||||
@trigger_error(sprintf('Passing "null" as the $locale argument to %s() is deprecated since Symfony 4.4.', __METHOD__), \E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$this->setLocale($locale, false);
|
||||
|
||||
if (null === $formatter) {
|
||||
$formatter = new MessageFormatter();
|
||||
@@ -97,6 +103,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
$this->formatter = $formatter;
|
||||
$this->cacheDir = $cacheDir;
|
||||
$this->debug = $debug;
|
||||
$this->cacheVary = $cacheVary;
|
||||
$this->hasIntlFormatter = $formatter instanceof IntlFormatterInterface;
|
||||
}
|
||||
|
||||
@@ -108,8 +115,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
@@ -132,12 +138,17 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
$domain = 'messages';
|
||||
}
|
||||
|
||||
$this->assertValidLocale($locale);
|
||||
if (null === $locale) {
|
||||
@trigger_error(sprintf('Passing "null" to the third argument of the "%s" method has been deprecated since Symfony 4.4 and will throw an error in 5.0.', __METHOD__), \E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$this->resources[$locale][] = array($format, $resource, $domain);
|
||||
$this->assertValidLocale($locale);
|
||||
$locale ?: $locale = class_exists(\Locale::class) ? \Locale::getDefault() : 'en';
|
||||
|
||||
$this->resources[$locale][] = [$format, $resource, $domain];
|
||||
|
||||
if (\in_array($locale, $this->fallbackLocales)) {
|
||||
$this->catalogues = array();
|
||||
$this->catalogues = [];
|
||||
} else {
|
||||
unset($this->catalogues[$locale]);
|
||||
}
|
||||
@@ -148,6 +159,10 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
*/
|
||||
public function setLocale($locale)
|
||||
{
|
||||
if (null === $locale && (2 > \func_num_args() || func_get_arg(1))) {
|
||||
@trigger_error(sprintf('Passing "null" as the $locale argument to %s() is deprecated since Symfony 4.4.', __METHOD__), \E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$this->assertValidLocale($locale);
|
||||
$this->locale = $locale;
|
||||
}
|
||||
@@ -157,26 +172,27 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
*/
|
||||
public function getLocale()
|
||||
{
|
||||
return $this->locale;
|
||||
return $this->locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the fallback locales.
|
||||
*
|
||||
* @param array $locales The fallback 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) {
|
||||
if (null === $locale) {
|
||||
@trigger_error(sprintf('Passing "null" as the $locale argument to %s() is deprecated since Symfony 4.4.', __METHOD__), \E_USER_DEPRECATED);
|
||||
}
|
||||
$this->assertValidLocale($locale);
|
||||
}
|
||||
|
||||
$this->fallbackLocales = $locales;
|
||||
$this->fallbackLocales = $this->cacheVary['fallback_locales'] = $locales;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,13 +210,16 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
|
||||
public function trans($id, array $parameters = [], $domain = null, $locale = null)
|
||||
{
|
||||
if ('' === $id = (string) $id) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (null === $domain) {
|
||||
$domain = 'messages';
|
||||
}
|
||||
|
||||
$id = (string) $id;
|
||||
$catalogue = $this->getCatalogue($locale);
|
||||
$locale = $catalogue->getLocale();
|
||||
while (!$catalogue->defines($id, $domain)) {
|
||||
@@ -224,9 +243,13 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
*
|
||||
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
|
||||
*/
|
||||
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
|
||||
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);
|
||||
@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 ('' === $id = (string) $id) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!$this->formatter instanceof ChoiceMessageFormatterInterface) {
|
||||
throw new LogicException(sprintf('The formatter "%s" does not support plural translations.', \get_class($this->formatter)));
|
||||
@@ -236,7 +259,6 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
$domain = 'messages';
|
||||
}
|
||||
|
||||
$id = (string) $id;
|
||||
$catalogue = $this->getCatalogue($locale);
|
||||
$locale = $catalogue->getLocale();
|
||||
while (!$catalogue->defines($id, $domain)) {
|
||||
@@ -249,7 +271,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
}
|
||||
|
||||
if ($this->hasIntlFormatter && $catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
|
||||
return $this->formatter->formatIntl($catalogue->get($id, $domain), $locale, array('%count%' => $number) + $parameters);
|
||||
return $this->formatter->formatIntl($catalogue->get($id, $domain), $locale, ['%count%' => $number] + $parameters);
|
||||
}
|
||||
|
||||
return $this->formatter->choiceFormat($catalogue->get($id, $domain), $number, $locale, $parameters);
|
||||
@@ -260,7 +282,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
*/
|
||||
public function getCatalogue($locale = null)
|
||||
{
|
||||
if (null === $locale) {
|
||||
if (!$locale) {
|
||||
$locale = $this->getLocale();
|
||||
} else {
|
||||
$this->assertValidLocale($locale);
|
||||
@@ -335,7 +357,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
$this->catalogues[$locale] = include $cache->getPath();
|
||||
}
|
||||
|
||||
private function dumpCatalogue($locale, ConfigCacheInterface $cache): void
|
||||
private function dumpCatalogue(string $locale, ConfigCacheInterface $cache): void
|
||||
{
|
||||
$this->initializeCatalogue($locale);
|
||||
$fallbackContent = $this->getFallbackContent($this->catalogues[$locale]);
|
||||
@@ -390,26 +412,33 @@ EOF
|
||||
return $fallbackContent;
|
||||
}
|
||||
|
||||
private function getCatalogueCachePath($locale)
|
||||
private function getCatalogueCachePath(string $locale): string
|
||||
{
|
||||
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): void
|
||||
/**
|
||||
* @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): void
|
||||
private function loadFallbackCatalogues(string $locale): void
|
||||
{
|
||||
$current = $this->catalogues[$locale];
|
||||
|
||||
@@ -430,34 +459,43 @@ EOF
|
||||
protected function computeFallbackLocales($locale)
|
||||
{
|
||||
if (null === $this->parentLocales) {
|
||||
$parentLocales = \json_decode(\file_get_contents(__DIR__.'/Resources/data/parents.json'), true);
|
||||
$this->parentLocales = json_decode(file_get_contents(__DIR__.'/Resources/data/parents.json'), true);
|
||||
}
|
||||
|
||||
$locales = array();
|
||||
foreach ($this->fallbackLocales as $fallback) {
|
||||
if ($fallback === $locale) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$locales[] = $fallback;
|
||||
}
|
||||
$originLocale = $locale;
|
||||
$locales = [];
|
||||
|
||||
while ($locale) {
|
||||
$parent = $parentLocales[$locale] ?? null;
|
||||
$parent = $this->parentLocales[$locale] ?? null;
|
||||
|
||||
if (!$parent && false !== strrchr($locale, '_')) {
|
||||
$locale = substr($locale, 0, -\strlen(strrchr($locale, '_')));
|
||||
} elseif ('root' !== $parent) {
|
||||
$locale = $parent;
|
||||
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);
|
||||
$locales[] = $locale;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->fallbackLocales as $fallback) {
|
||||
if ($fallback === $originLocale) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$locales[] = $fallback;
|
||||
}
|
||||
|
||||
return array_unique($locales);
|
||||
}
|
||||
|
||||
@@ -470,7 +508,7 @@ EOF
|
||||
*/
|
||||
protected function assertValidLocale($locale)
|
||||
{
|
||||
if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
|
||||
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', (string) $locale)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
|
||||
}
|
||||
}
|
||||
@@ -490,7 +528,7 @@ EOF
|
||||
|
||||
private function getAllMessages(MessageCatalogueInterface $catalogue): array
|
||||
{
|
||||
$allMessages = array();
|
||||
$allMessages = [];
|
||||
|
||||
foreach ($catalogue->all() as $domain => $messages) {
|
||||
if ($intlMessages = $catalogue->all($domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
|
||||
|
Reference in New Issue
Block a user