Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX #13919 added TranslationsCacheWarmer to generate catalogues at wa…
…rmup
- Loading branch information
1 parent
75c8a2b
commit 94d3876
Showing
9 changed files
with
133 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TranslationsCacheWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer; | ||
|
||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
/** | ||
* Generates the catalogues for translations. | ||
* | ||
* @author Xavier Leune <xavier.leune@gmail.com> | ||
*/ | ||
class TranslationsCacheWarmer implements CacheWarmerInterface | ||
{ | ||
private $translator; | ||
|
||
public function __construct(TranslatorInterface $translator) | ||
{ | ||
$this->translator = $translator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function warmUp($cacheDir) | ||
{ | ||
if ($this->translator instanceof WarmableInterface) { | ||
$this->translator->warmUp($cacheDir); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isOptional() | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
namespace Symfony\Bundle\FrameworkBundle\Translation; | ||
|
||
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; | ||
use Symfony\Component\Translation\Translator as BaseTranslator; | ||
use Symfony\Component\Translation\MessageSelector; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
@@ -20,52 +21,67 @@ | |
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class Translator extends BaseTranslator | ||
class Translator extends BaseTranslator implements WarmableInterface | ||
{ | ||
protected $container; | ||
protected $loaderIds; | ||
protected $resourceFiles; | ||
|
||
protected $options = array( | ||
'cache_dir' => null, | ||
'debug' => false, | ||
'resource_files' => array(), | ||
); | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $resourceLocales; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* Available options: | ||
* | ||
* * cache_dir: The cache directory (or null to disable caching) | ||
* * debug: Whether to enable debugging or not (false by default) | ||
* * resource_files: List of translation resources available grouped by locale. | ||
* | ||
* @param ContainerInterface $container A ContainerInterface instance | ||
* @param MessageSelector $selector The message selector for pluralization | ||
* @param array $loaderIds An array of loader Ids | ||
* @param array $options An array of options | ||
* @param array $resourceFiles An array of resource directories | ||
* @param ContainerInterface $container A ContainerInterface instance | ||
* @param MessageSelector $selector The message selector for pluralization | ||
* @param array $loaderIds An array of loader Ids | ||
* @param array $options An array of options | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array()) | ||
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array()) | ||
{ | ||
$this->container = $container; | ||
$this->loaderIds = $loaderIds; | ||
$this->resourceFiles = $resourceFiles; | ||
|
||
// check option names | ||
if ($diff = array_diff(array_keys($options), array_keys($this->options))) { | ||
throw new \InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.', implode('\', \'', $diff))); | ||
} | ||
|
||
$this->options = array_merge($this->options, $options); | ||
$this->resourceLocales = array_keys($this->options['resource_files']); | ||
if (null !== $this->options['cache_dir'] && $this->options['debug']) { | ||
$this->loadResources(); | ||
} | ||
|
||
parent::__construct(null, $selector, $this->options['cache_dir'], $this->options['debug']); | ||
} | ||
|
||
/** | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mpdude
Contributor
|
||
* {@inheritdoc} | ||
*/ | ||
public function warmUp($cacheDir) | ||
{ | ||
foreach ($this->resourceLocales as $locale) { | ||
$this->loadCatalogue($locale); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -87,11 +103,13 @@ protected function initialize() | |
|
||
private function loadResources() | ||
{ | ||
foreach ($this->resourceFiles as $key => $file) { | ||
// filename is domain.locale.format | ||
list($domain, $locale, $format) = explode('.', basename($file), 3); | ||
$this->addResource($format, $file, $locale, $domain); | ||
unset($this->resourceFiles[$key]); | ||
foreach ($this->options['resource_files'] as $locale => $files) { | ||
foreach ($files as $key => $file) { | ||
// filename is domain.locale.format | ||
list($domain, $locale, $format) = explode('.', basename($file), 3); | ||
$this->addResource($format, $file, $locale, $domain); | ||
unset($this->options['resource_files'][$locale][$key]); | ||
} | ||
} | ||
} | ||
} |
The
$cacheDir
argument is not used – does that matter?