Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 20f7d11

Browse filesBrowse files
committed
[translation] fixed loading resources performances.
1 parent 6963887 commit 20f7d11
Copy full SHA for 20f7d11

File tree

6 files changed

+34
-16
lines changed
Filter options

6 files changed

+34
-16
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslatorPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslatorPass.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function process(ContainerBuilder $container)
4040
}
4141
}
4242

43-
$container->findDefinition('translator.default')->replaceArgument(2, $loaders);
43+
$translatorDefinition = $container->findDefinition('translator.default');
44+
$translatorDefinition->replaceArgument(2, $loaders);
45+
$translatorDefinition->replaceArgument(3, $container->getParameter('translator.resource.directories'));
4446
}
4547
}

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+2-13Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class FrameworkExtension extends Extension
4040
*
4141
* @param array $configs
4242
* @param ContainerBuilder $container
43+
*
4344
* @throws LogicException
4445
*/
4546
public function load(array $configs, ContainerBuilder $container)
@@ -682,23 +683,11 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
682683
}
683684

684685
// Register translation resources
686+
$container->setParameter('translator.resource.directories', $dirs);
685687
if ($dirs) {
686688
foreach ($dirs as $dir) {
687689
$container->addResource(new DirectoryResource($dir));
688690
}
689-
$finder = Finder::create()
690-
->files()
691-
->filter(function (\SplFileInfo $file) {
692-
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
693-
})
694-
->in($dirs)
695-
;
696-
697-
foreach ($finder as $file) {
698-
// filename is domain.locale.format
699-
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3);
700-
$translator->addMethodCall('addResource', array($format, (string) $file, $locale, $domain));
701-
}
702691
}
703692
}
704693

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<argument type="service" id="service_container" />
4141
<argument type="service" id="translator.selector" />
4242
<argument type="collection" /> <!-- translation loaders -->
43+
<argument type="collection" /> <!-- translation resources -->
4344
<argument type="collection">
4445
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
4546
<argument key="debug">%kernel.debug%</argument>

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/TranslatorPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/TranslatorPassTest.php
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testValidCollector()
2828

2929
$container = $this->getMock(
3030
'Symfony\Component\DependencyInjection\ContainerBuilder',
31-
array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'findDefinition')
31+
array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'findDefinition', 'getParameter')
3232
);
3333
$container->expects($this->any())
3434
->method('hasDefinition')
@@ -42,6 +42,11 @@ public function testValidCollector()
4242
$container->expects($this->once())
4343
->method('findDefinition')
4444
->will($this->returnValue($this->getMock('Symfony\Component\DependencyInjection\Definition')));
45+
$container->expects($this->once())
46+
->method('getParameter')
47+
->with('translator.resource.directories')
48+
->will($this->returnValue(array()));
49+
4550
$pass = new TranslatorPass();
4651
$pass->process($container);
4752
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public function getTranslator($loader, $options = array(), $translatorClass = '\
188188
$this->getContainer($loader),
189189
new MessageSelector(),
190190
array('loader' => array('loader')),
191+
array(),
191192
$options
192193
);
193194

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Translation\Translator as BaseTranslator;
1515
use Symfony\Component\Translation\MessageSelector;
1616
use Symfony\Component\DependencyInjection\ContainerInterface;
17+
use Symfony\Component\Finder\Finder;
1718

1819
/**
1920
* Translator.
@@ -24,6 +25,7 @@ class Translator extends BaseTranslator
2425
{
2526
protected $container;
2627
protected $loaderIds;
28+
protected $resourceDirs;
2729

2830
protected $options = array(
2931
'cache_dir' => null,
@@ -41,14 +43,16 @@ class Translator extends BaseTranslator
4143
* @param ContainerInterface $container A ContainerInterface instance
4244
* @param MessageSelector $selector The message selector for pluralization
4345
* @param array $loaderIds An array of loader Ids
46+
* @param array $resourceDirs An array of resource directories
4447
* @param array $options An array of options
4548
*
4649
* @throws \InvalidArgumentException
4750
*/
48-
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array())
51+
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), $resourceDirs = array(), array $options = array())
4952
{
5053
$this->container = $container;
5154
$this->loaderIds = $loaderIds;
55+
$this->resourceDirs = $resourceDirs;
5256

5357
// check option names
5458
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
@@ -76,5 +80,21 @@ protected function initialize()
7680
$this->addLoader($alias, $this->container->get($id));
7781
}
7882
}
83+
84+
if ($this->resourceDirs) {
85+
$finder = Finder::create()
86+
->files()
87+
->filter(function (\SplFileInfo $file) {
88+
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
89+
})
90+
->in($this->resourceDirs)
91+
;
92+
93+
foreach ($finder as $file) {
94+
// filename is domain.locale.format
95+
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3);
96+
$this->addResource($format, (string) $file, $locale, $domain);
97+
}
98+
}
7999
}
80100
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.