|
| 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