From af14f35a55c178d6505a329963a238f5f93f3073 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 23 Dec 2015 11:06:49 +0100 Subject: [PATCH] Made configration easier --- DependencyInjection/Configuration.php | 2 +- .../HappyrTranslationExtension.php | 21 +++++++- README.md | 48 +------------------ 3 files changed, 21 insertions(+), 50 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index dbea0a6..d9b8bc4 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -24,7 +24,7 @@ public function getConfigTreeBuilder() ->booleanNode('auto_add_assets')->defaultFalse()->end() ->booleanNode('allow_edit')->defaultTrue()->end() ->scalarNode('http_adapter')->defaultValue('guzzle5')->end() - ->enumNode('file_extension')->values(array('xlf', 'php'))->defaultValue('xlf')->end() + ->enumNode('file_extension')->values(array('csv', 'ini', 'json', 'mo', 'php', 'po', 'qt', 'yaml', 'xliff'))->defaultValue('xliff')->end() ->arrayNode('locales') ->requiresAtLeastOneElement() ->prototype('scalar')->end() diff --git a/DependencyInjection/HappyrTranslationExtension.php b/DependencyInjection/HappyrTranslationExtension.php index 123af06..c121845 100644 --- a/DependencyInjection/HappyrTranslationExtension.php +++ b/DependencyInjection/HappyrTranslationExtension.php @@ -4,6 +4,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader; @@ -41,12 +42,13 @@ public function load(array $configs, ContainerBuilder $container) $targetDir = rtrim($config['target_dir'], '/'); - $fileExtension = rtrim($config['file_extension']); $container->findDefinition('happyr.translation.filesystem') ->replaceArgument(2, $targetDir) - ->replaceArgument(3, $fileExtension); + ->replaceArgument(3, $config['file_extension']); + $this->configureLoaderAndDumper($container, $config['file_extension']); + /* * Set alias for the translation service */ @@ -74,4 +76,19 @@ private function copyValuesFromParentToProject($key, array &$config) } } } + + /** + * @param ContainerBuilder $container + * @param string $fileExtension + */ + protected function configureLoaderAndDumper(ContainerBuilder $container, $fileExtension) + { + $loader = $container->getDefinition('happyr.translation.loader'); + $loader->setClass(sprintf('Symfony\Component\Translation\Loader\%sFileLoader', ucfirst($fileExtension))); + $loader->addTag('translation.loader', ['alias'=>$fileExtension]); + + $dumper = $container->getDefinition('happyr.translation.dumper'); + $dumper->setClass(sprintf('Symfony\Component\Translation\Dumper\%sFileDumper', ucfirst($fileExtension))); + $dumper->addTag('translation.dumper', ['alias'=>$fileExtension]); + } } diff --git a/README.md b/README.md index b7d2ee0..701beb3 100644 --- a/README.md +++ b/README.md @@ -74,52 +74,6 @@ _happyr_translation: ``` -You can choose local translation storage file type by changing the file_extension value in your config.yml. -As of now, PHP (php) and XLIFF (xlf) are supported (xlf is the default value). -If you choose another file type, you have to choose the appropriate Loader and Dumper in your service.yml. - -Loader and Dumper for XLIFF resources (default) -``` yaml -# /app/config/config.yml -happyr_translation: - file_extension: 'xlf' - -# /app/config/services.yml -services: - # XLIFF Loader - happyr.translation.loader: - class: Symfony\Component\Translation\Loader\XliffFileLoader - tags: - - { name: 'translation.loader', alias: 'xliff' } - - # XLIFF Dump - happyr.translation.dumper: - class: Symfony\Component\Translation\Dumper\XliffFileDumper - tags: - - { name: 'translation.dumper', alias: 'xliff' } -``` - -Loader and Dumper for PHP resources -``` yaml -# /app/config/config.yml -happyr_translation: - file_extension: 'php' - -# /app/config/services.yml -services: - # PHP Loader - happyr.translation.loader: - class: Symfony\Component\Translation\Loader\PhpFileLoader - tags: - - { name: 'translation.loader', alias: 'php' } - - # PHP Dumper - happyr.translation.dumper: - class: Symfony\Component\Translation\Dumper\PhpFileDumper - tags: - - { name: 'translation.dumper', alias: 'php' } -``` - If composer installs guzzle 6+ please mind the following configuration: ``` yaml happyr_translation: @@ -131,7 +85,7 @@ happyr_translation: ``` yaml happyr_translation: - file_extension: 'xlf' + file_extension: 'xliff' # could be 'json', 'mo', 'php', 'po', 'yaml' and many more locales: [] dimensions: [] translation_service: 'loco'