2010-09-27 08:45:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Translation;
|
|
|
|
|
|
|
|
use Symfony\Component\Translation\Loader\LoaderInterface;
|
|
|
|
|
2010-09-27 08:45:29 +01:00
|
|
|
/**
|
|
|
|
* Translator.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
class Translator implements TranslatorInterface
|
|
|
|
{
|
2011-03-09 09:24:16 +00:00
|
|
|
protected $catalogues;
|
|
|
|
protected $locale;
|
2011-03-08 19:47:32 +00:00
|
|
|
private $fallbackLocale;
|
|
|
|
private $loaders;
|
|
|
|
private $resources;
|
|
|
|
private $selector;
|
2010-09-27 08:45:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2010-09-27 15:53:23 +01:00
|
|
|
* @param string $locale The locale
|
|
|
|
* @param MessageSelector $selector The message selector for pluralization
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
2011-04-25 14:52:15 +01:00
|
|
|
public function __construct($locale, MessageSelector $selector)
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
|
|
|
$this->locale = $locale;
|
2010-09-27 15:53:23 +01:00
|
|
|
$this->selector = $selector;
|
2010-09-27 08:45:29 +01:00
|
|
|
$this->loaders = array();
|
|
|
|
$this->resources = array();
|
|
|
|
$this->catalogues = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Loader.
|
|
|
|
*
|
|
|
|
* @param string $format The name of the loader (@see addResource())
|
|
|
|
* @param LoaderInterface $loader A LoaderInterface instance
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
public function addLoader($format, LoaderInterface $loader)
|
|
|
|
{
|
|
|
|
$this->loaders[$format] = $loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Resource.
|
|
|
|
*
|
|
|
|
* @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
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
public function addResource($format, $resource, $locale, $domain = 'messages')
|
|
|
|
{
|
|
|
|
$this->resources[$locale][] = array($format, $resource, $domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
public function setLocale($locale)
|
|
|
|
{
|
|
|
|
$this->locale = $locale;
|
|
|
|
}
|
|
|
|
|
2010-10-06 13:18:36 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-10-06 13:18:36 +01:00
|
|
|
*/
|
|
|
|
public function getLocale()
|
|
|
|
{
|
|
|
|
return $this->locale;
|
|
|
|
}
|
|
|
|
|
2010-09-27 08:45:29 +01:00
|
|
|
/**
|
|
|
|
* Sets the fallback locale.
|
|
|
|
*
|
|
|
|
* @param string $locale The fallback locale
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
public function setFallbackLocale($locale)
|
|
|
|
{
|
2010-10-31 21:33:08 +00:00
|
|
|
// needed as the fallback locale is used to fill-in non-yet translated messages
|
|
|
|
$this->catalogues = array();
|
2010-09-27 08:45:29 +01:00
|
|
|
|
|
|
|
$this->fallbackLocale = $locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
|
|
|
|
{
|
|
|
|
if (!isset($locale)) {
|
2010-10-06 10:57:38 +01:00
|
|
|
$locale = $this->getLocale();
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->catalogues[$locale])) {
|
|
|
|
$this->loadCatalogue($locale);
|
|
|
|
}
|
|
|
|
|
2011-04-24 08:39:28 +01:00
|
|
|
return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
|
|
|
|
{
|
|
|
|
if (!isset($locale)) {
|
2010-10-06 10:57:38 +01:00
|
|
|
$locale = $this->getLocale();
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->catalogues[$locale])) {
|
|
|
|
$this->loadCatalogue($locale);
|
|
|
|
}
|
|
|
|
|
2011-09-14 21:49:04 +01:00
|
|
|
if (!$this->catalogues[$locale]->defines((string) $id, $domain)) {
|
2011-08-22 15:07:39 +01:00
|
|
|
// we will use the fallback
|
|
|
|
$locale = $this->computeFallbackLocale($locale);
|
|
|
|
}
|
|
|
|
|
2011-04-24 08:39:28 +01:00
|
|
|
return strtr($this->selector->choose($this->catalogues[$locale]->get((string) $id, $domain), (int) $number, $locale), $parameters);
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|
|
|
|
|
2011-03-09 09:24:16 +00:00
|
|
|
protected function loadCatalogue($locale)
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
|
|
|
$this->catalogues[$locale] = new MessageCatalogue($locale);
|
|
|
|
|
2011-02-25 12:56:27 +00:00
|
|
|
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]));
|
|
|
|
}
|
|
|
|
$this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->optimizeCatalogue($locale);
|
|
|
|
}
|
|
|
|
|
2011-03-08 19:47:32 +00:00
|
|
|
private function optimizeCatalogue($locale)
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
2011-08-22 15:07:39 +01:00
|
|
|
if (!$fallback = $this->computeFallbackLocale($locale)) {
|
2010-10-31 21:33:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-27 08:45:29 +01:00
|
|
|
if (!isset($this->catalogues[$fallback])) {
|
|
|
|
$this->loadCatalogue($fallback);
|
|
|
|
}
|
|
|
|
|
2010-10-31 21:33:08 +00:00
|
|
|
$this->catalogues[$locale]->addFallbackCatalogue($this->catalogues[$fallback]);
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|
2011-08-22 15:07:39 +01:00
|
|
|
|
|
|
|
private function computeFallbackLocale($locale)
|
|
|
|
{
|
|
|
|
if (strlen($locale) > 3) {
|
|
|
|
return substr($locale, 0, -strlen(strrchr($locale, '_')));
|
|
|
|
} else {
|
|
|
|
return $this->fallbackLocale;
|
|
|
|
}
|
|
|
|
}
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|