Skip to content

Commit 40f2628

Browse files
committed
[Translation] added message cache + doctrine cache.
1 parent 37c137a commit 40f2628

File tree

14 files changed

+663
-65
lines changed

14 files changed

+663
-65
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
585585
->defaultValue(array('en'))
586586
->end()
587587
->booleanNode('logging')->defaultValue($this->debug)->end()
588+
->scalarNode('cache')->defaultValue('translation.cache.default')->end()
588589
->end()
589590
->end()
590591
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
652652

653653
$container->setParameter('translator.logging', $config['logging']);
654654

655+
if (isset($config['cache'])) {
656+
$container->setAlias('translation.cache', $config['cache']);
657+
}
658+
655659
// Discover translation directories
656660
$dirs = array();
657661
if (class_exists('Symfony\Component\Validator\Validation')) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<argument key="debug">%kernel.debug%</argument>
4646
</argument>
4747
<argument type="collection" /> <!-- translation resources -->
48+
<argument type="service" id="translation.cache" />
4849
</service>
4950

5051
<service id="translator.logging" class="Symfony\Component\Translation\LoggingTranslator" public="false">
@@ -152,5 +153,13 @@
152153
<service id="translation.extractor" class="%translation.extractor.class%"/>
153154

154155
<service id="translation.writer" class="%translation.writer.class%"/>
156+
157+
<!-- Cache -->
158+
<service id="translation.cache.default" class="Symfony\Component\Translation\MessageCache" public="false">
159+
<argument key="cache_dir">%kernel.cache_dir%/translations</argument> <!-- cache_dir -->
160+
<argument>%kernel.debug%</argument> <!-- debug -->
161+
</service>
162+
163+
<service id="translation.cache" alias="translation.cache.default" />
155164
</services>
156165
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ protected static function getBundleDefaultConfig()
146146
'enabled' => false,
147147
'fallbacks' => array('en'),
148148
'logging' => true,
149+
'cache' => 'translation.cache.default'
149150
),
150151
'validation' => array(
151152
'enabled' => false,

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Translation\Translator as BaseTranslator;
1515
use Symfony\Component\Translation\MessageSelector;
1616
use Symfony\Component\DependencyInjection\ContainerInterface;
17+
use Symfony\Component\Translation\MessageCacheInterface;
1718

1819
/**
1920
* Translator.
@@ -47,7 +48,7 @@ class Translator extends BaseTranslator
4748
*
4849
* @throws \InvalidArgumentException
4950
*/
50-
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array())
51+
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array(), MessageCacheInterface $cache = null)
5152
{
5253
$this->container = $container;
5354
$this->loaderIds = $loaderIds;
@@ -63,7 +64,8 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
6364
$this->loadResources();
6465
}
6566

66-
parent::__construct(null, $selector, $this->options['cache_dir'], $this->options['debug']);
67+
$cache = $cache ? :$this->options['cache_dir'];
68+
parent::__construct(null, $selector, $cache, $this->options['debug']);
6769
}
6870

6971
/**
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Catalogue;
13+
14+
use Symfony\Component\Translation\DoctrineMessageCatalogue;
15+
use Doctrine\Common\Cache\Cache;
16+
17+
/**
18+
* @author Abdellatif Ait Boudad <a.aitboudad@gmail.com>
19+
*/
20+
class DoctrineCatalogueFactory implements CatalogueFactoryInterface
21+
{
22+
/**
23+
* @var Cache
24+
*/
25+
private $cache;
26+
27+
/**
28+
* @param Cache $cache
29+
*/
30+
public function __construct(Cache $cache)
31+
{
32+
$this->cache = $cache;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function create($locale, array $messages = array())
39+
{
40+
$catalogue = new DoctrineMessageCatalogue($locale, $this->cache);
41+
foreach ($messages as $domain => $message) {
42+
$catalogue->add($message, $domain);
43+
}
44+
45+
return $catalogue;
46+
}
47+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation;
13+
14+
use Doctrine\Common\Cache\Cache;
15+
16+
/**
17+
* @author Abdellatif Ait Boudad <a.aitboudad@gmail.com>
18+
*/
19+
class DoctrineMessageCache implements MessageCacheInterface
20+
{
21+
const CACHE_RESOURCE_HASH = 'resources_hash';
22+
23+
/**
24+
* @var bool
25+
*/
26+
private $debug;
27+
28+
/**
29+
* @var Cache
30+
*/
31+
private $cacheProvider;
32+
33+
/**
34+
* @param bool $debug
35+
* @param Cache $cacheProvider
36+
*/
37+
public function __construct($debug, Cache $cacheProvider)
38+
{
39+
$this->debug = $debug;
40+
$this->cacheProvider = $cacheProvider;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function isFresh($options = array())
47+
{
48+
$resourcesHash = $this->cacheProvider->fetch(self::CACHE_RESOURCE_HASH.'_'.$options['locale']);
49+
if ($this->debug && $resourcesHash !== $options['resources_hash']) {
50+
return false;
51+
}
52+
53+
return true;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function load($resource, $locale, $domain = null)
60+
{
61+
return new DoctrineMessageCatalogue($locale, $this->cacheProvider);
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function dump(MessageCatalogue $messages, $options = array())
68+
{
69+
$catalogue = new DoctrineMessageCatalogue($messages->getLocale(), $this->cacheProvider);
70+
$catalogue->addCatalogue($messages);
71+
72+
$this->cacheProvider->save(self::CACHE_RESOURCE_HASH.'_'.$messages->getLocale(), $options['resources_hash']);
73+
}
74+
}

0 commit comments

Comments
 (0)
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