Skip to content

Commit

Permalink
use a compiler pass to register translation files in their new location
Browse files Browse the repository at this point in the history
  • Loading branch information
craue committed Aug 19, 2014
1 parent 36d83b5 commit 28f6e7c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CraueFormFlowBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Craue\FormFlowBundle;

use Craue\FormFlowBundle\DependencyInjection\Compiler\LoadTranslationsCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -10,4 +12,13 @@
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class CraueFormFlowBundle extends Bundle {

/**
* {@inheritDoc}
*/
public function build(ContainerBuilder $container) {
parent::build($container);
$container->addCompilerPass(new LoadTranslationsCompilerPass());
}

}
45 changes: 45 additions & 0 deletions DependencyInjection/Compiler/LoadTranslationsCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Craue\FormFlowBundle\DependencyInjection\Compiler;

use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Finder\Finder;

/**
* Explicitly registers translation files in their uncommon location.
*
* @author Christian Raue <[email protected]>
* @copyright 2011-2014 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class LoadTranslationsCompilerPass implements CompilerPassInterface {

/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container) {
$dir = __DIR__ . '/../../FormFlow/Resources/translations';

// taken roughly from https://github.com/symfony/symfony/blob/ce15db564736d7a0cf02a0db688a0ee101959cb5/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L650
$container->addResource(new DirectoryResource($dir));

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

$translator = $container->findDefinition('translator');

foreach ($finder as $file) {
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3);
$translator->addMethodCall('addResource', array($format, (string) $file, $locale, $domain));
}
}

}

0 comments on commit 28f6e7c

Please sign in to comment.