From 28f6e7cef2e13b1238f5808b9c18c6a3ce58d0d8 Mon Sep 17 00:00:00 2001 From: Christian Raue Date: Thu, 26 Jun 2014 10:43:15 +0200 Subject: [PATCH] use a compiler pass to register translation files in their new location --- CraueFormFlowBundle.php | 11 +++++ .../Compiler/LoadTranslationsCompilerPass.php | 45 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 DependencyInjection/Compiler/LoadTranslationsCompilerPass.php diff --git a/CraueFormFlowBundle.php b/CraueFormFlowBundle.php index 990dd6c4..c16cd47c 100644 --- a/CraueFormFlowBundle.php +++ b/CraueFormFlowBundle.php @@ -2,6 +2,8 @@ namespace Craue\FormFlowBundle; +use Craue\FormFlowBundle\DependencyInjection\Compiler\LoadTranslationsCompilerPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; /** @@ -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()); + } + } diff --git a/DependencyInjection/Compiler/LoadTranslationsCompilerPass.php b/DependencyInjection/Compiler/LoadTranslationsCompilerPass.php new file mode 100644 index 00000000..7e7b0f50 --- /dev/null +++ b/DependencyInjection/Compiler/LoadTranslationsCompilerPass.php @@ -0,0 +1,45 @@ + + * @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)); + } + } + +}