Skip to content

Commit 8cd4149

Browse files
committed
use a compiler pass to register translation files in their new location
1 parent c4fdece commit 8cd4149

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

CraueFormFlowBundle.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Craue\FormFlowBundle;
44

5+
use Craue\FormFlowBundle\DependencyInjection\Compiler\LoadTranslationsCompilerPass;
56
use Craue\FormFlowBundle\FormFlow\Util\TempFileUtil;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
68
use Symfony\Component\HttpKernel\Bundle\Bundle;
79

810
/**
@@ -25,4 +27,12 @@ public function boot() {
2527
});
2628
}
2729

30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public function build(ContainerBuilder $container) {
34+
parent::build($container);
35+
$container->addCompilerPass(new LoadTranslationsCompilerPass());
36+
}
37+
2838
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Craue\FormFlowBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\Config\Resource\DirectoryResource;
6+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\Finder\Finder;
9+
10+
/**
11+
* Explicitly registers translation files in their uncommon location.
12+
*
13+
* @author Christian Raue <christian.raue@gmail.com>
14+
* @copyright 2011-2015 Christian Raue
15+
* @license http://opensource.org/licenses/mit-license.php MIT License
16+
*/
17+
class LoadTranslationsCompilerPass implements CompilerPassInterface {
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function process(ContainerBuilder $container) {
23+
$dir = __DIR__ . '/../../FormFlow/Resources/translations';
24+
25+
// taken roughly from https://github.com/symfony/symfony/blob/ce15db564736d7a0cf02a0db688a0ee101959cb5/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L650
26+
$container->addResource(new DirectoryResource($dir));
27+
28+
$finder = Finder::create()
29+
->files()
30+
->in($dir)
31+
->filter(function (\SplFileInfo $file) {
32+
$basename = $file->getBasename();
33+
return substr_count($basename, '.') === 2 && preg_match('/\.\w+$/', $basename);
34+
})
35+
;
36+
37+
$translator = $container->findDefinition('translator');
38+
39+
foreach ($finder as $file) {
40+
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3);
41+
$translator->addMethodCall('addResource', array($format, (string) $file, $locale, $domain));
42+
}
43+
}
44+
45+
}

0 commit comments

Comments
 (0)