8000 FIX #13919 added TranslationsCacheWarmer to generate catalogues at wa… · xavierleune/symfony@ebc84cb · GitHub
[go: up one dir, main page]

Skip to content

Commit ebc84cb

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

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
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/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/Translation/Translator.php

Lines changed: 21 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,23 @@ 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+
106+
if (null !== $this->locale) {
107+
$this->loadCatalogue($this->locale);
108+
}
109+
110+
foreach ($this->getFallbackLocales() as $locale) {
111+
// We need to reset the catalogues every time, otherwise file won't be generated
112+
$this->catalogues = array();
113+
$this->loadCatalogue($locale);
114+
}
115+
}
116+
}
97117
}

0 commit comments

Comments
 (0)
0