*
* @api
*/
@@ -51,6 +54,11 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
*/
private $resources = array();
+ /**
+ * @var MessageFormatterInterface
+ */
+ private $formatter;
+
/**
* @var MessageSelector
*/
@@ -74,19 +82,31 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
/**
* 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 ?
+ * @param string $locale The locale
+ * @param MessageFormatterInterface|MessageSelector $formatter The message formatter
+ * @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
*
* @api
*/
- public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false)
+ public function __construct($locale, $formatter = null, $cacheDir = null, $debug = false)
{
$this->setLocale($locale);
- $this->selector = $selector ?: new MessageSelector();
+ if ($formatter instanceof MessageSelector) {
+ @trigger_error('Passing a MessageSelector instance into the '.__METHOD__.' as a second argument is deprecated since version 2.8 and will be removed in 3.0. Inject a MessageFormatterInterface instance instead.', E_USER_DEPRECATED);
+ $this->selector = $formatter;
+ $formatter = new LegacyIntlMessageFormatter();
+ } else {
+ $this->selector = new MessageSelector();
+ }
+
+ $this->formatter = $formatter ?: new LegacyIntlMessageFormatter();
+ if (!$this->formatter instanceof MessageFormatterInterface) {
+ throw new \InvalidArgumentException(sprintf('The message formatter "%s" must implement MessageFormatterInterface.', get_class($this->formatter)));
+ }
+
$this->cacheDir = $cacheDir;
$this->debug = $debug;
}
@@ -226,7 +246,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
$domain = 'messages';
}
- return strtr($this->getCatalogue($locale)->get((string) $id, $domain), $parameters);
+ return $this->formatter->format($locale, $this->getCatalogue($locale)->get((string) $id, $domain), $parameters);
}
/**
@@ -236,6 +256,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
*/
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
+ @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Rely on the MessageFormatterInterface and TranslatorInterface::trans() method instead.', E_USER_DEPRECATED);
if (null === $domain) {
$domain = 'messages';
}
@@ -252,7 +273,7 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
}
}
- return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
+ return $this->formatter->format($locale, $this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
}
/**
diff --git a/src/Symfony/Component/Translation/TranslatorInterface.php b/src/Symfony/Component/Translation/TranslatorInterface.php
index fe1a8659e2c93..f76dad6de1f46 100644
--- a/src/Symfony/Component/Translation/TranslatorInterface.php
+++ b/src/Symfony/Component/Translation/TranslatorInterface.php
@@ -39,6 +39,9 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
/**
* Translates the given choice message by choosing a translation according to a number.
*
+ * @deprecated since version 2.8, to be removed in 3.0.
+ * Use the {@link trans} method instead.
+ *
* @param string $id The message id (may also be an object that can be cast to string)
* @param int $number The number to use to find the indice of the message
* @param array $parameters An array of parameters for the message
diff --git a/src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php b/src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php
index 6995d25817988..d281f47d46a63 100644
--- a/src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php
+++ b/src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php
@@ -521,7 +521,6 @@ public function testAddCustomizedViolation()
$context->buildViolation('Message %param%')
->setParameter('%param%', 'value')
->setInvalidValue('Invalid value')
- ->setPlural(2)
->setCode(42)
->addViolation();
};
@@ -538,7 +537,7 @@ public function testAddCustomizedViolation()
$this->assertSame('', $violations[0]->getPropertyPath());
$this->assertSame($entity, $violations[0]->getRoot());
$this->assertSame('Invalid value', $violations[0]->getInvalidValue());
- $this->assertSame(2, $violations[0]->getPlural());
+ $this->assertNull($violations[0]->getPlural());
$this->assertSame(42, $violations[0]->getCode());
}
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy