Skip to content

Commit 28f6e7c

Browse files
committed
use a compiler pass to register translation files in their new location
1 parent 36d83b5 commit 28f6e7c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

CraueFormFlowBundle.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Craue\FormFlowBundle;
44

5+
use Craue\FormFlowBundle\DependencyInjection\Compiler\LoadTranslationsCompilerPass;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
57
use Symfony\Component\HttpKernel\Bundle\Bundle;
68

79
/**
@@ -10,4 +12,13 @@
1012
* @license http://opensource.org/licenses/mit-license.php MIT License
1113
*/
1214
class CraueFormFlowBundle extends Bundle {
15+
16+
/**
17+
* {@inheritDoc}
18+
*/
19+
public function build(ContainerBuilder $container) {
20+
parent::build($container);
21+
$container->addCompilerPass(new LoadTranslationsCompilerPass());
22+
}
23+
1324
}
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-2014 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)