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
Next Next commit
[translation] load resources per locale.
  • Loading branch information
aitboudad committed Mar 11, 2015
commit 7be239f789fe4720a6fc8dc2ae089453efab5411
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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 Down Expand Up @@ -43,7 +44,28 @@ public function process(ContainerBuilder $container)
$translatorDefinition = $container->findDefinition('translator.default');
$translatorDefinition->replaceArgument(2, $loaders);
if ($container->hasParameter('translator.resource_directories')) {
$translatorDefinition->replaceArgument(4, $container->getParameter('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);
}
Copy link
Member

Choose a reason for hiding this comment

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

What about moving this logic back into the extension instead? There is no need for it to be here and that would remove the need to create a parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ 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')
Expand All @@ -41,15 +52,15 @@ public function testValidCollector()
->will($this->returnValue(array('xliff' => array(array('alias' => 'xliff', 'legacy-alias' => 'xlf')))));
$container->expects($this->once())
->method('findDefinition')
->will($this->returnValue($this->getMock('Symfony\Component\DependencyInjection\Definition')));
->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()));
->will($this->returnValue(array(__DIR__.'/../../Fixtures/Resources/translations')));

$pass = new TranslatorPass();
$pass->process($container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,14 @@ public function testTransWithCachingWithInvalidLocale()
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), array(__DIR__.'/../Fixtures/Resources/translations'), 'yml');
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), $resourceFiles, 'yml');
$translator->setLocale('fr');

$this->assertEquals('répertoire', $translator->trans('folder'));
Expand All @@ -122,7 +127,13 @@ public function testLoadRessourcesWithCaching()
public function testLoadRessourcesWithoutCaching()
{
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$translator = $this->getTranslator($loader, array(), array(__DIR__.'/../Fixtures/Resources/translations'), 'yml');
$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'));
Expand Down
38 changes: 17 additions & 21 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\Translation\Translator as BaseTranslator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Finder\Finder;

/**
* Translator.
Expand All @@ -25,7 +24,7 @@ class Translator extends BaseTranslator
{
protected $container;
protected $loaderIds;
protected $resourceDirs;
protected $resourceFiles;

protected $options = array(
'cache_dir' => null,
Expand All @@ -40,19 +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 array $resourceDirs 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
* @param array $resourceFiles An array of resource directories
*
* @throws \InvalidArgumentException
*/
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceDirs = array())
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array())
{
$this->container = $container;
$this->loaderIds = $loaderIds;
$this->resourceDirs = $resourceDirs;
$this->resourceFiles = $resourceFiles;

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

Expand All @@ -80,21 +80,17 @@ protected function initialize()
$this->addLoader($alias, $this->container->get($id));
}
}
}

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

foreach ($finder as $file) {
private function loadResources($locale)
{
if (isset($this->resourceFiles[$locale])) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you change the if to return early?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added loading Fallback Locales.

foreach ($this->resourceFiles[$locale] as $file) {
// filename is domain.locale.format
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3);
$this->addResource($format, (string) $file, $locale, $domain);
list($domain, $locale, $format) = explode('.', basename($file), 3);
$this->addResource($format, $file, $locale, $domain);
}
unset($this->resourceFiles[$locale]);
}
}
}
0