Skip to content

Commit dfd828f

Browse files
committed
[Translation] added message cache + doctrine cache.
1 parent 007386c commit dfd828f

File tree

14 files changed

+853
-71
lines changed

14 files changed

+853
-71
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: 6 additions & 2 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')) {
@@ -865,9 +869,9 @@ private function registerSecurityCsrfConfiguration(array $config, ContainerBuild
865869
/**
866870
* Loads the serializer configuration.
867871
*
868-
* @param array $config A serializer configuration array
872+
* @param array $config A serializer configuration array
869873
* @param ContainerBuilder $container A ContainerBuilder instance
870-
* @param XmlFileLoader $loader An XmlFileLoader instance
874+
* @param XmlFileLoader $loader An XmlFileLoader instance
871875
*/
872876
private function registerSerializerConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
873877
{

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
<xsd:attribute name="enabled" type="xsd:boolean" />
188188
<xsd:attribute name="fallback" type="xsd:string" />
189189
<xsd:attribute name="logging" type="xsd:boolean" />
190+
<xsd:attribute name="cache" type="xsd:string" />
190191
</xsd:complexType>
191192

192193
<xsd:complexType name="validation">

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

Lines changed: 7 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" on-invalid="null" />
4849
</service>
4950

5051
<service id="translator.logging" class="Symfony\Component\Translation\LoggingTranslator" public="false">
@@ -152,5 +153,11 @@
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>
155162
</services>
156163
</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: 10 additions & 7 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.
@@ -39,15 +40,16 @@ class Translator extends BaseTranslator
3940
* * cache_dir: The cache directory (or null to disable caching)
4041
* * debug: Whether to enable debugging or not (false by default)
4142
*
42-
* @param ContainerInterface $container A ContainerInterface instance
43-
* @param MessageSelector $selector The message selector for pluralization
44-
* @param array $loaderIds An array of loader Ids
45-
* @param array $options An array of options
46-
* @param array $resourceFiles An array of resource directories
43+
* @param ContainerInterface $container A ContainerInterface instance
44+
* @param MessageSelector $selector The message selector for pluralization
45+
* @param array $loaderIds An array of loader Ids
46+
* @param array $options An array of options
47+
* @param array $resourceFiles An array of resource directories
48+
* @param MessageCacheInterface $cache The message cache
4749
*
4850
* @throws \InvalidArgumentException
4951
*/
50-
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array())
52+
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array(), MessageCacheInterface $cache = null)
5153
{
5254
$this->container = $container;
5355
$this->loaderIds = $loaderIds;
@@ -63,7 +65,8 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
6365
$this->loadResources();
6466
}
6567

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

6972
/**
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
const CATALOGUE_FALLBACK_LOCALE = 'fallback_locale';
23+
24+
/**
25+
* @var bool
26+
*/
27+
private $debug;
28+
29+
/**
30+
* @var Cache
31+
*/
32+
private $cacheProvider;
33+
34+
/**
35+
* @param Cache $cacheProvider
36+
* @param bool $debug
37+
*/
38+
public function __construct(Cache $cacheProvider, $debug = false)
39+
{
40+
$this->debug = $debug;
41+
$this->cacheProvider = $cacheProvider;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function isFresh($locale, $options = array())
48+
{
49+
if (!isset($options['resources_hash'])) {
50+
$options['resources_hash'] = '';
51+
}
52+
53+
$resourcesHash = $this->cacheProvider->fetch($this->getResourceHashKey($locale));
54+
if (false === $resourcesHash || ($this->debug && $resourcesHash !== $options['resources_hash'])) {
55+
return false;
56+
}
57+
58+
return true;
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public function load($locale)
65+
{
66+
$messages = new DoctrineMessageCatalogue($locale, $this->cacheProvider);
67+
$catalogue = $messages;
68+
while ($fallbackLocale = $this->cacheProvider->fetch($this->getFallbackLocaleKey($catalogue->getLocale()))) {
69+
$fallback = new DoctrineMessageCatalogue($fallbackLocale, $this->cacheProvider);
70+
$catalogue->addFallbackCatalogue($fallback);
71+
$catalogue = $fallback;
72+
}
73+
74+
return $messages;
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function dump(MessageCatalogueInterface $messages, $options = array())
81+
{
82+
while ($messages) {
83+
$catalogue = new DoctrineMessageCatalogue($messages->getLocale(), $this->cacheProvider);
84+
$catalogue->addCatalogue($messages);
85+
86+
$this->cacheProvider->save($this->getResourceHashKey($messages->getLocale()), $options['resources_hash']);
87+
if ($fallback = $messages->getFallbackCatalogue()) {
88+
$this->cacheProvider->save($this->getFallbackLocaleKey($messages->getLocale()), $fallback->getLocale());
89+
}
90+
91+
$messages = $messages->getFallbackCatalogue();
92+
}
93+
}
94+
95+
private function getResourceHashKey($locale)
96+
{
97+
return self::CACHE_RESOURCE_HASH.'_'.$locale;
98+
}
99+
100+
private function getFallbackLocaleKey($locale)
101+
{
102+
return self::CATALOGUE_FALLBACK_LOCALE.'_'.$locale;
103+
}
104+
}

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