upgraded dependencies
This commit is contained in:
125
vendor/symfony/translation/Translator.php
vendored
125
vendor/symfony/translation/Translator.php
vendored
@@ -15,21 +15,22 @@ 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\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\LocaleAwareInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
// Help opcache.preload discover always-needed symbols
|
||||
class_exists(MessageCatalogue::class);
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class Translator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface
|
||||
class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var MessageCatalogueInterface[]
|
||||
@@ -42,7 +43,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
private $locale;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* @var string[]
|
||||
*/
|
||||
private $fallbackLocales = [];
|
||||
|
||||
@@ -88,13 +89,9 @@ 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, array $cacheVary = [])
|
||||
public function __construct(string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false, array $cacheVary = [])
|
||||
{
|
||||
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);
|
||||
$this->setLocale($locale);
|
||||
|
||||
if (null === $formatter) {
|
||||
$formatter = new MessageFormatter();
|
||||
@@ -117,7 +114,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
@@ -127,21 +124,15 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
*
|
||||
* @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, $resource, string $locale, string $domain = null)
|
||||
{
|
||||
if (null === $domain) {
|
||||
$domain = 'messages';
|
||||
}
|
||||
|
||||
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->assertValidLocale($locale);
|
||||
$locale ?: $locale = class_exists(\Locale::class) ? \Locale::getDefault() : 'en';
|
||||
|
||||
@@ -157,12 +148,8 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setLocale($locale)
|
||||
public function setLocale(string $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;
|
||||
}
|
||||
@@ -178,6 +165,8 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
/**
|
||||
* Sets the fallback locales.
|
||||
*
|
||||
* @param string[] $locales
|
||||
*
|
||||
* @throws InvalidArgumentException If a locale contains invalid characters
|
||||
*/
|
||||
public function setFallbackLocales(array $locales)
|
||||
@@ -186,9 +175,6 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
$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);
|
||||
}
|
||||
|
||||
@@ -198,11 +184,9 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
/**
|
||||
* Gets the fallback locales.
|
||||
*
|
||||
* @internal since Symfony 4.2
|
||||
*
|
||||
* @return array The fallback locales
|
||||
* @internal
|
||||
*/
|
||||
public function getFallbackLocales()
|
||||
public function getFallbackLocales(): array
|
||||
{
|
||||
return $this->fallbackLocales;
|
||||
}
|
||||
@@ -210,9 +194,9 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function trans($id, array $parameters = [], $domain = null, $locale = null)
|
||||
public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null)
|
||||
{
|
||||
if ('' === $id = (string) $id) {
|
||||
if (null === $id || '' === $id) {
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -231,7 +215,11 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->hasIntlFormatter && $catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
|
||||
$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);
|
||||
}
|
||||
|
||||
@@ -240,47 +228,8 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
|
||||
/**
|
||||
* {@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 ('' === $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)));
|
||||
}
|
||||
|
||||
if (null === $domain) {
|
||||
$domain = 'messages';
|
||||
}
|
||||
|
||||
$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, ['%count%' => $number] + $parameters);
|
||||
}
|
||||
|
||||
return $this->formatter->choiceFormat($catalogue->get($id, $domain), $number, $locale, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCatalogue($locale = null)
|
||||
public function getCatalogue(string $locale = null)
|
||||
{
|
||||
if (!$locale) {
|
||||
$locale = $this->getLocale();
|
||||
@@ -295,20 +244,25 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
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()
|
||||
{
|
||||
return $this->loaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
*/
|
||||
protected function loadCatalogue($locale)
|
||||
protected function loadCatalogue(string $locale)
|
||||
{
|
||||
if (null === $this->cacheDir) {
|
||||
$this->initializeCatalogue($locale);
|
||||
@@ -317,10 +271,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
*/
|
||||
protected function initializeCatalogue($locale)
|
||||
protected function initializeCatalogue(string $locale)
|
||||
{
|
||||
$this->assertValidLocale($locale);
|
||||
|
||||
@@ -456,7 +407,7 @@ EOF
|
||||
}
|
||||
}
|
||||
|
||||
protected function computeFallbackLocales($locale)
|
||||
protected function computeFallbackLocales(string $locale)
|
||||
{
|
||||
if (null === $this->parentLocales) {
|
||||
$this->parentLocales = json_decode(file_get_contents(__DIR__.'/Resources/data/parents.json'), true);
|
||||
@@ -502,13 +453,11 @@ EOF
|
||||
/**
|
||||
* 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 (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', (string) $locale)) {
|
||||
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user