Skip to content

Commit

Permalink
Made configration easier
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Dec 23, 2015
1 parent fa1e4e7 commit af14f35
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 50 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 19 additions & 2 deletions DependencyInjection/HappyrTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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]);
}
}
48 changes: 1 addition & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down

0 comments on commit af14f35

Please sign in to comment.