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

Skip to content

Commit 050b8b4

Browse files
committed
FIX symfony#13919 added TranslationsCacheWarmer to generate catalogues at warmup
1 parent 17ad6fd commit 050b8b4

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-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-DEV
5+
---------
6+
7+
* Added `TranslationsCacheWarmer` to create catalogues at warmup
8+
49
2.6.0
510
-----
611

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
protected $translator;
26+
27+
/**
28+
* Constructor.
29+
*
30+
* @param TranslatorInterface $translator A Translator instance
31+
*/
32+
public function __construct(TranslatorInterface $translator)
33+
{
34+
$this->translator = $translator;
35+
}
36+
37+
/**
38+
* Warms up the cache.
39+
*
40+
* @param string $cacheDir The cache directory
41+
*/
42+
public function warmUp($cacheDir)
43+
{
44+
if ($this->translator instanceof WarmableInterface) {
45+
$this->translator->warmUp($cacheDir);
46+
}
47+
}
48+
49+
/**
50+
* Checks whether this warmer is optional or not.
51+
*
52+
* @return bool always true
53+
*/
54+
public function isOptional()
55+
{
56+
return true;
57+
}
58+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<parameter key="translation.loader.class">Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader</parameter>
3434
<parameter key="translation.extractor.class">Symfony\Component\Translation\Extractor\ChainExtractor</parameter>
3535
<parameter key="translation.writer.class">Symfony\Component\Translation\Writer\TranslationWriter</parameter>
36+
<parameter key="translation.warmer.class">Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer</parameter>
3637
</parameters>
3738

3839
<services>
@@ -152,5 +153,10 @@
152153
<service id="translation.extractor" class="%translation.extractor.class%"/>
153154

154155
<service id="translation.writer" class="%translation.writer.class%"/>
156+
157+
<service id="translation.warmer" class="%translation.warmer.class%" public="false">
158+
<argument type="service" id="translator" />
159+
<tag name="kernel.cache_warmer" />
160+
</service>
155161
</services>
156162
</container>

src/Symfony/Component/Translation/Translator.php

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

1212
namespace Symfony\Component\Translation;
1313

14+
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1415
use Symfony\Component\Translation\Loader\LoaderInterface;
1516
use Symfony\Component\Translation\Exception\NotFoundResourceException;
1617
use Symfony\Component\Config\ConfigCache;
@@ -22,7 +23,7 @@
2223
*
2324
* @api
2425
*/
25-
class Translator implements TranslatorInterface, TranslatorBagInterface
26+
class Translator implements TranslatorInterface, TranslatorBagInterface, WarmableInterface
2627
{
2728
/**
2829
* @var MessageCatalogueInterface[]
@@ -491,4 +492,19 @@ protected function assertValidLocale($locale)
491492
throw new \InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
492493
}
493494
}
495+
496+
/**
497+
* @see WarmableInterface
498+
*
499+
* @param string $cacheDir only used for interface compatibility, $this->cacheDir is already defined
500+
*/
501+
public function warmUp($cacheDir)
502+
{
503+
if ($this->locale !== null) {
504+
$this->loadCatalogue($this->locale);
505+
}
506+
foreach ($this->fallbackLocales as $locale) {
507+
$this->loadCatalogue($locale);
508+
}
509+
}
494510
}

0 commit comments

Comments
 (0)
0