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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed t 8000 o load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[translation][loading resources] added Fallback Locales.
  • Loading branch information
aitboudad committed Mar 11, 2015
commit 9a1d6d9876d0a2c673fec549e22bc53c99a6ea8e
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Finder\Finder;

class TranslatorPass implements CompilerPassInterface
{
Expand All @@ -41,31 +40,6 @@ public function process(ContainerBuilder $container)
}
}

$translatorDefinition = $container->findDefinition('translator.default');
$translatorDefinition->replaceArgument(2, $loaders);
if ($container->hasParameter('translator.resource_directories')) {
$resourceDirs = $container->getParameter('translator.resource_directories');
$files = array();
if ($resourceDirs) {
$finder = Finder::create()
->files()
->filter(function (\SplFileInfo $file) {
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
})
->in($resourceDirs)
;

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

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

$translatorDefinition->replaceArgument(4, $files);
}
$container->findDefinition('translator.default')->replaceArgument(2, $loaders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,30 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
}

// Register translation resources
$container->setParameter('translator.resource_directories', $dirs);
if ($dirs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the if is not needed anymore.

foreach ($dirs as $dir) {
$container->addResource(new DirectoryResource($dir));
}

$files = array();
$finder = Finder::create()
->files()
->filter(function (\SplFileInfo $file) {
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
})
->in($dirs)
;

foreach ($finder as $file) {
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3);
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 @@ -26,20 +26,9 @@ public function testValidCollector()
->method('addMethodCall')
->with('addLoader', array('xlf', new Reference('xliff')));

$translatorDefinition = $this->getMock('Symfony\Component\DependencyInjection\Definition');

$expectedResourceFiles = array(
'fr' => array(
(__DIR__.'/../../Fixtures/Resources/translations/messages.fr.yml'),
),
);
$translatorDefinition->expects($this->at(1))
->method('replaceArgument')
->with(4, $expectedResourceFiles);

$container = $this->getMock(
'Symfony\Component\DependencyInjection\ContainerBuilder',
array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'findDefinition', 'hasParameter', 'getParameter')
array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'findDefinition')
);
$container->expects($this->any())
->method('hasDefinition')
Expand All @@ -52,16 +41,7 @@ public function testValidCollector()
->will($this->returnValue(array('xliff' => array(array('alias' => 'xliff', 'legacy-alias' => 'xlf')))));
$container->expects($this->once())
->method('findDefinition')
->will($this->returnValue($translatorDefinition));
$container->expects($this->once())
->method('hasParameter')
->with('translator.resource_directories')
->will($this->returnValue(true));
$container->expects($this->once())
->method('getParameter')
->with('translator.resource_directories')
->will($this->returnValue(array(__DIR__.'/../../Fixtures/Resources/translations')));

->will($this->returnValue($this->getMock('Symfony\Component\DependencyInjection\Definition')));
$pass = new TranslatorPass();
$pass->process($container);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,28 @@ 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);

$dirs = $container->getParameter('translator.resource_directories');

$files = array_map(function ($resource) { return realpath($resource); }, $resources['en']);
$ref = new \ReflectionClass('Symfony\Component\Validator\Validation');
$this->assertContains(
dirname($ref->getFileName()).'/Resources/translations',
$dirs
strtr(dirname($ref->getFileName()).'/Resources/translations/validators.en.xlf', '/', DIRECTORY_SEPARATOR),
$files,
'->registerTranslatorConfiguration() finds Validator translation resources'
);
$ref = new \ReflectionClass('Symfony\Component\Form\Form');
$this->assertContains(
dirname($ref->getFileName()).'/Resources/translations',
$dirs
strtr(dirname($ref->getFileName()).'/Resources/translations/validators.en.xlf', '/', DIRECTORY_SEPARATOR),
$files,
'->registerTranslatorConfiguration() finds Form translation resources'
);
$ref = new \ReflectionClass('Symfony\Component\Security\Core\Exception\AuthenticationException');
$ref = new \ReflectionClass('Symfony\Component\Security\Core\Security');
$this->assertContains(
dirname($ref->getFileName()).'/../Resources/translations',
$dirs
strtr(dirname($ref->getFileName()).'/Resources/translations/security.en.xlf', '/', DIRECTORY_SEPARATOR),
$files,
'->registerTranslatorConfiguration() finds Security translation resources'
);

$calls = $container->getDefinition('translator.default')->getMethodCalls();
Expand Down
15 changes: 9 additions & 6 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ protected function initialize()

private function loadResources($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);
$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]);
}
unset($this->resourceFiles[$locale]);
}
}
}
0