Skip to content

Commit a83a7dd

Browse files
committed
FIX #13919 added TranslationsCacheWarmer to generate catalogues at warmup
1 parent cb70899 commit a83a7dd

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.7.0
5+
-----
6+
7+
* Added `TranslationsCacheWarmer` to create catalogues at warmup
8+
49
2.6.0
510
-----
611

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Bundle\FrameworkBundle\CacheWarmer;
13+
14+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
15+
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
16+
use Symfony\Component\Translation\TranslatorInterface;
17+
18+
/**
19+
* Generates the catalogues for translations.
20+
*
21+
* @author Xavier Leune <xavier.leune@gmail.com>
22+
*/
23+
class TranslationsCacheWarmer implements CacheWarmerInterface
24+
{
25+
private $translator;
26+
27+
/**
28+
*
29+
* @param TranslatorInterface $translator A Translator instance
30+
*/
31+
public function __construct(TranslatorInterface $translator)
32+
{
33+
$this->translator = $translator;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function warmUp($cacheDir)
40+
{
41+
if ($this->translator instanceof WarmableInterface) {
42+
$this->translator->warmUp($cacheDir);
43+
}
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function isOptional()
50+
{
51+
return true;
52+
}
53+
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/LoggingTranslatorPass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Reference;
1617

1718
/**
1819
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
@@ -38,6 +39,7 @@ public function process(ContainerBuilder $container)
3839
$refClass = new \ReflectionClass($class);
3940
if ($refClass->implementsInterface('Symfony\Component\Translation\TranslatorInterface') && $refClass->implementsInterface('Symfony\Component\Translation\TranslatorBagInterface')) {
4041
$container->getDefinition('translator.logging')->setDecoratedService('translator');
42+
$container->getDefinition('translation.warmer')->replaceArgument(0, new Reference('translator.logging.inner'));
4143
}
4244
}
4345
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,10 @@
152152
<service id="translation.extractor" class="%translation.extractor.class%"/>
153153

154154
<service id="translation.writer" class="%translation.writer.class%"/>
155+
156+
<service id="translation.warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer" public="false">
157+
<argument type="service" id="translator" />
158+
<tag name="kernel.cache_warmer" />
159+
</service>
155160
</services>
156161
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/LoggingTranslatorPassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testProcess()
3535
->method('getAlias')
3636
->will($this->returnValue('translation.default'));
3737

38-
$container->expects($this->exactly(2))
38+
$container->expects($this->exactly(3))
3939
->method('getDefinition')
4040
->will($this->returnValue($definition));
4141

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Translation;
1313

14+
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1415
use Symfony\Component\Translation\Translator as BaseTranslator;
1516
use Symfony\Component\Translation\MessageSelector;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -20,7 +21,7 @@
2021
*
2122
* @author Fabien Potencier <fabien@symfony.com>
2223
*/
23-
class Translator extends BaseTranslator
24+
class Translator extends BaseTranslator implements WarmableInterface
2425
{
2526
protected $container;
2627
protected $loaderIds;
@@ -94,4 +95,22 @@ private function loadResources()
9495
unset($this->resourceFiles[$key]);
9596
}
9697
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function warmUp($cacheDir)
103+
{
104+
if (null !== $this->options['cache_dir']) {
105+
if (null !== $this->locale) {
106+
$this->loadCatalogue($this->locale);
107+
}
108+
109+
foreach ($this->getFallbackLocales() as $locale) {
110+
// We need to reset the catalogues every time, otherwise file won't be generated
111+
$this->catalogues = array();
112+
$this->loadCatalogue($locale);
113+
}
114+
}
115+
}
97116
}

0 commit comments

Comments
 (0)