use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
+use Symfony\Component\Translation\Exception\LogicException;
use Symfony\Component\Translation\Exception\RuntimeException;
use Symfony\Component\Config\ConfigCacheInterface;
use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Config\ConfigCacheFactory;
+use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
+use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface;
+use Symfony\Component\Translation\Formatter\MessageFormatter;
/**
- * Translator.
- *
* @author Fabien Potencier <fabien@symfony.com>
*/
class Translator implements TranslatorInterface, TranslatorBagInterface
private $resources = array();
/**
- * @var MessageSelector
+ * @var MessageFormatterInterface
*/
- private $selector;
+ private $formatter;
/**
* @var string
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 ?
+ * @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 ?
*
* @throws InvalidArgumentException If a locale contains invalid characters
*/
- 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) {
+ $formatter = new MessageFormatter($formatter);
+ @trigger_error(sprintf('Passing a "%s" instance into the "%s" 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) {
+ $formatter = new MessageFormatter();
+ }
+
+ $this->formatter = $formatter;
$this->cacheDir = $cacheDir;
$this->debug = $debug;
}
- /**
- * Sets the ConfigCache factory to use.
- *
- * @param ConfigCacheFactoryInterface $configCacheFactory
- */
public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
{
$this->configCacheFactory = $configCacheFactory;
$domain = 'messages';
}
- return strtr($this->getCatalogue($locale)->get((string) $id, $domain), $parameters);
+ return $this->formatter->format($this->getCatalogue($locale)->get((string) $id, $domain), $locale, $parameters);
}
/**
*/
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
- $parameters = array_merge(array(
- '%count%' => $number,
- ), $parameters);
+ 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';
}
}
- return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
+ return $this->formatter->choiceFormat($catalogue->get($id, $domain), $number, $locale, $parameters);
}
/**
private function getCatalogueCachePath($locale)
{
- return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php';
+ return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->fallbackLocales), true)), 0, 7), '/', '_').'.php';
}
private function doLoadCatalogue($locale)
$locales[] = $fallback;
}
- if (strrchr($locale, '_') !== false) {
+ if (false !== strrchr($locale, '_')) {
array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
}