8000 [translation][performances] move loading resources into Translator initialize. by aitboudad · Pull Request #13897 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[translation][performances] move loading resources into Translator initialize. #13897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class FrameworkExtension extends Extension
*
* @param array $configs
* @param ContainerBuilder $container
*
* @throws LogicException
*/
public function load(array $configs, ContainerBuilder $container)
Expand Down Expand Up @@ -686,6 +687,8 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
foreach ($dirs as $dir) {
$container->addResource(new DirectoryResource($dir));
}

$files = array();
$finder = Finder::create()
->files()
->filter(function (\SplFileInfo $file) {
Expand All @@ -695,10 +698,15 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
;

foreach ($finder as $file) {
// filename is domain.locale.format
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3);
$translator->addMethodCall('addResource', array($format, (string) $file, $locale, $domain));
if (!isset($files[$locale])) {
$files[$locale] = array();
}

$files[$locale][] = (string) $file;
}

$translator->replaceArgument(4, $files);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
<argument key="debug">%kernel.debug%</argument>
</argument>
<argument type="collection" /> <!-- translation resources -->
</service>

<service id="translator.logging" class="Symfony\Component\Translation\LoggingTranslator" public="false">
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,11 @@ public function testAssets()
public function testTranslator()
{
$container = $this->createContainerFromFile('full');

$this->assertTrue($container->hasDefinition('translator.default'), '->registerTranslatorConfiguration() loads translation.xml');
$this->assertEquals('translator.default', (string) $container->getAlias('translator'), '->registerTranslatorConfiguration() redefines translator service from identity to real translator');
$resources = $container->getDefinition('translator.default')->getArgument(4);

$resources = array();
foreach ($container->getDefinition('translator.default')->getMethodCalls() as $call) {
if ('addResource' == $call[0]) {
$resources[] = $call[1];
}
}

$files = array_map(function ($resource) { return realpath($resource[1]); }, $resources);
$files = array_map(function ($resource) { return realpath($resource); }, $resources['en']);
$ref = new \ReflectionClass('Symfony\Component\Validator\Validation');
$this->assertContains(
strtr(dirname($ref->getFileName()).'/Resources/translations/validators.en.xlf', '/', DIRECTORY_SEPARATOR),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
folder: répertoire
8000
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,50 @@ public function testTransWithCaching()
public function testTransWithCachingWithInvalidLocale()
{
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), array(), 'loader', '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
$translator->setLocale('invalid locale');

$this->setExpectedException('\InvalidArgumentException');
$translator->trans('foo');
}

public function testLoadRessourcesWithCaching()
{
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$resourceFiles = array(
'fr' => array(
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
),
);

// prime the cache
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), $resourceFiles, 'yml');
$translator->setLocale('fr');

$this->assertEquals('répertoire', $translator->trans('folder'));

// do it another time as the cache is primed now
$translator = $this->getTranslator($loader, array(' 8000 cache_dir' => $this->tmpDir), array(), 'yml');
$translator->setLocale('fr');

$this->assertEquals('répertoire', $translator->trans('folder'));
}

public function testLoadRessourcesWithoutCaching()
{
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$resourceFiles = array(
'fr' => array(
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
),
);

$translator = $this->getTranslator($loader, array(), $resourceFiles, 'yml');
$translator->setLocale('fr');

$this->assertEquals('répertoire', $translator->trans('folder'));
}

protected function getCatalogue($locale, $messages)
{
$catalogue = new MessageCatalogue($locale);
Expand Down Expand Up @@ -182,22 +219,25 @@ protected function getContainer($loader)
return $container;
}

public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
public function getTranslator($loader, $options = array(), $resources = array(), $loaderFomat = 'loader', $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
{
$translator = new $translatorClass(
$this->getContainer($loader),
new MessageSelector(),
array('loader' => array('loader')),
$options
array($loaderFomat => array($loaderFomat)),
$options,
$resources
);

$translator->addResource('loader', 'foo', 'fr');
$translator->addResource('loader', 'foo', 'en');
$translator->addResource('loader', 'foo', 'es');
$translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese
$translator->addResource('loader', 'foo', 'fr.UTF-8');
$translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian
if ('loader' === $loaderFomat) {
$translator->addResource('loader', 'foo', 'fr');
$translator->addResource('loader', 'foo', 'en');
$translator->addResource('loader', 'foo', 'es');
$translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese
$translator->addResource('loader', 'foo', 'fr.UTF-8');
$translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian
}

return $translator;
}
Expand Down
29 changes: 24 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Translator extends BaseTranslator
{
protected $container;
protected $loaderIds;
protected $resourceFiles;

protected $options = array(
'cache_dir' => null,
Expand All @@ -38,17 +39,19 @@ class Translator extends BaseTranslator
* * cache_dir: The cache directory (or null to disable caching)
* * debug: Whether to enable debugging or not (false by default)
*
* @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 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
*
* @throws \InvalidArgumentException
*/
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array())
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array())
{
$this->container = $container;
$this->loaderIds = $loaderIds;
$this->resourceFiles = $resourceFiles;

// check option names
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
Expand All @@ -66,6 +69,7 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
protected function initializeCatalogue($locale)
{
$this->initialize();
$this->loadResources($locale);
parent::initializeCatalogue($locale);
}

Expand All @@ -77,4 +81,19 @@ protected function initialize()
}
}
}

private function loadResources($locale)
{
$locales = array_merge(array($locale), $this->computeFallbackLocales($locale));
foreach ($locales as $locale) {
if (isset($this->resourceFiles[$locale])) {
foreach ($this->resourceFiles[$locale] as $file) {
// filename is domain.locale.format
list($domain, $locale, $format) = explode('.', basename($file), 3);
$this->addResource($format, $file, $locale, $domain);
}
unset($this->resourceFiles[$locale]);
}
}
}
}
0