2010-09-27 08:46:15 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
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:46:15 +01:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Translation;
|
|
|
|
|
|
|
|
use Symfony\Component\Translation\Translator as BaseTranslator;
|
|
|
|
use Symfony\Component\Translation\Loader\LoaderInterface;
|
2010-09-27 15:53:23 +01:00
|
|
|
use Symfony\Component\Translation\MessageSelector;
|
2010-09-27 08:46:15 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Session;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\ConfigCache;
|
2010-09-27 08:46:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translator.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:46:15 +01:00
|
|
|
*/
|
|
|
|
class Translator extends BaseTranslator
|
|
|
|
{
|
|
|
|
protected $container;
|
|
|
|
protected $options;
|
2010-10-06 10:57:38 +01:00
|
|
|
protected $session;
|
2011-04-23 10:34:17 +01:00
|
|
|
protected $loaderIds;
|
2010-09-27 08:46:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Available options:
|
|
|
|
*
|
|
|
|
* * cache_dir: The cache directory (or null to disable caching)
|
|
|
|
* * debug: Whether to enable debugging or not (false by default)
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container A ContainerInterface instance
|
2010-09-27 15:53:23 +01:00
|
|
|
* @param MessageSelector $selector The message selector for pluralization
|
2010-09-27 08:46:15 +01:00
|
|
|
* @param array $options An array of options
|
|
|
|
* @param Session $session A Session instance
|
|
|
|
*/
|
2011-04-23 10:34:17 +01:00
|
|
|
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), Session $session = null)
|
2010-09-27 08:46:15 +01:00
|
|
|
{
|
2010-10-06 15:33:39 +01:00
|
|
|
parent::__construct(null, $selector);
|
|
|
|
|
2010-10-06 10:57:38 +01:00
|
|
|
$this->session = $session;
|
2010-09-27 08:46:15 +01:00
|
|
|
$this->container = $container;
|
2011-04-23 10:34:17 +01:00
|
|
|
$this->loaderIds = $loaderIds;
|
2010-09-27 08:46:15 +01:00
|
|
|
|
|
|
|
$this->options = array(
|
|
|
|
'cache_dir' => null,
|
|
|
|
'debug' => false,
|
|
|
|
);
|
|
|
|
|
|
|
|
// check option names
|
|
|
|
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The Router does not support the following options: \'%s\'.', implode('\', \'', $diff)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->options = array_merge($this->options, $options);
|
|
|
|
}
|
|
|
|
|
2010-10-06 10:57:38 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getLocale()
|
|
|
|
{
|
|
|
|
if (null === $this->locale && null !== $this->session) {
|
2010-10-06 18:14:25 +01:00
|
|
|
$this->locale = $this->session->getLocale();
|
2010-10-06 10:57:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->locale;
|
|
|
|
}
|
|
|
|
|
2010-09-27 08:46:15 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function loadCatalogue($locale)
|
|
|
|
{
|
|
|
|
if (isset($this->catalogues[$locale])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $this->options['cache_dir']) {
|
|
|
|
$this->initialize();
|
|
|
|
|
|
|
|
return parent::loadCatalogue($locale);
|
|
|
|
}
|
|
|
|
|
2011-03-14 17:37:25 +00:00
|
|
|
$cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']);
|
2011-02-10 15:15:51 +00:00
|
|
|
if (!$cache->isFresh()) {
|
2010-09-27 08:46:15 +01:00
|
|
|
$this->initialize();
|
|
|
|
|
|
|
|
parent::loadCatalogue($locale);
|
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
$content = sprintf(
|
|
|
|
"<?php use Symfony\Component\Translation\MessageCatalogue; return new MessageCatalogue('%s', %s);",
|
|
|
|
$locale,
|
|
|
|
var_export($this->catalogues[$locale]->all(), true)
|
2011-02-14 10:14:04 +00:00
|
|
|
);
|
2011-02-10 15:15:51 +00:00
|
|
|
|
|
|
|
$cache->write($content, $this->catalogues[$locale]->getResources());
|
2010-09-27 08:46:15 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
$this->catalogues[$locale] = include $cache;
|
2010-09-27 08:46:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function initialize()
|
|
|
|
{
|
2011-04-23 10:34:17 +01:00
|
|
|
foreach ($this->loaderIds as $id => $alias) {
|
2011-01-17 10:29:38 +00:00
|
|
|
$this->addLoader($alias, $this->container->get($id));
|
2010-09-27 08:46:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|