Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions Command/ExportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

namespace Lexik\Bundle\TranslationBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Lexik\Bundle\TranslationBundle\Storage\StorageInterface;
use Lexik\Bundle\TranslationBundle\Translation\Exporter\ExporterCollector;
use Symfony\Bundle\FrameworkBundle\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Lexik\Bundle\TranslationBundle\Manager\FileInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Export translations from the database in to files.
*
* @author Cédric Girard <c.girard@lexik.fr>
*/
class ExportTranslationsCommand extends ContainerAwareCommand
class ExportTranslationsCommand extends Command
{
/**
* @var \Symfony\Component\Console\Input\InputInterface
Expand All @@ -26,6 +29,41 @@ class ExportTranslationsCommand extends ContainerAwareCommand
*/
private $output;

/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var StorageInterface
*/
private $storage;

/**
* @var StorageInterface
*/
private $kernelRootDir;

/**
* @var Filesystem
*/
private $filesystem;

/**
* @var ExporterCollector
*/
private $exporterCollector;

public function __construct(TranslatorInterface $translator, StorageInterface $storage, string $kernelRootDir, Filesystem $filesystem, ExporterCollector $exporterCollector)
{
parent::__construct();
$this->translator = $translator;
$this->storage = $storage;
$this->kernelRootDir = $kernelRootDir;
$this->filesystem = $filesystem;
$this->exporterCollector = $exporterCollector;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -70,9 +108,7 @@ protected function getFilesToExport()
$locales = $this->input->getOption('locales') ? explode(',', $this->input->getOption('locales')) : array();
$domains = $this->input->getOption('domains') ? explode(',', $this->input->getOption('domains')) : array();

return $this->getContainer()
->get('lexik_translation.translation_storage')
->getFilesByLocalesAndDomains($locales, $domains);
return $this->storage->getFilesByLocalesAndDomains($locales, $domains);
}

/**
Expand All @@ -82,7 +118,7 @@ protected function getFilesToExport()
*/
protected function exportFile(FileInterface $file)
{
$rootDir = $this->input->getOption('export-path') ? $this->input->getOption('export-path') . '/' : $this->getContainer()->getParameter('kernel.root_dir');
$rootDir = $this->input->getOption('export-path') ? $this->input->getOption('export-path') . '/' : $this->kernelRootDir;

$this->output->writeln(sprintf('<info># Exporting "%s/%s":</info>', $file->getPath(), $file->getName()));
$override = $this->input->getOption('override');
Expand All @@ -98,9 +134,7 @@ protected function exportFile(FileInterface $file)
$onlyUpdated = !$override;
}

$translations = $this->getContainer()
->get('lexik_translation.translation_storage')
->getTranslationsFromFile($file, $onlyUpdated);
$translations = $this->storage->getTranslationsFromFile($file, $onlyUpdated);

if (count($translations) < 1) {
$this->output->writeln('<comment>No translations to export.</comment>');
Expand All @@ -122,7 +156,7 @@ protected function exportFile(FileInterface $file)
// ensure the path exists
if ($this->input->getOption('export-path')) {
/** @var Filesystem $fs */
$fs = $this->getContainer()->get('filesystem');
$fs = $this->filesystem;
if (!$fs->exists($outputPath)) {
$fs->mkdir($outputPath);
}
Expand All @@ -147,7 +181,7 @@ protected function mergeExistingTranslations($file, $outputFile, $translations)
{
if (file_exists($outputFile)) {
$extension = pathinfo($outputFile, PATHINFO_EXTENSION);
$loader = $this->getContainer()->get('lexik_translation.translator')->getLoader($extension);
$loader = $this->translator->getLoader($extension);
$messageCatalogue = $loader->load($outputFile, $file->getLocale(), $file->getDomain());

$translations = array_merge($messageCatalogue->all($file->getDomain()), $translations);
Expand All @@ -169,7 +203,7 @@ protected function doExport($outputFile, $translations, $format)
$this->output->write(sprintf('<comment>%d translations to export: </comment>', count($translations)));

try {
$exported = $this->getContainer()->get('lexik_translation.exporter_collector')->export(
$exported = $this->exporterCollector->export(
$format,
$outputFile,
$translations
Expand Down
26 changes: 20 additions & 6 deletions Command/ImportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Lexik\Bundle\TranslationBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Lexik\Bundle\TranslationBundle\Manager\LocaleManagerInterface;
use Lexik\Bundle\TranslationBundle\Translation\Importer\FileImporter;
use Symfony\Bundle\FrameworkBundle\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -19,21 +21,33 @@
* @author Cédric Girard <c.girard@lexik.fr>
* @author Nikola Petkanski <nikola@petkanski.com>
*/
class ImportTranslationsCommand extends ContainerAwareCommand
class ImportTranslationsCommand extends Command
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var LocaleManagerInterface
*/
private $localeManager;

/**
* @var FileImporter
*/
private $fileImporter;

/**
* @param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator)
public function __construct(TranslatorInterface $translator, LocaleManagerInterface $localeManager, FileImporter $fileImporter)
{
parent::__construct();

$this->translator = $translator;
$this->localeManager = $localeManager;
$this->fileImporter = $fileImporter;
}

/**
Expand Down Expand Up @@ -79,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$locales = $this->input->getOption('locales');
if (empty($locales)) {
$locales = $this->getContainer()->get('lexik_translation.locale.manager')->getLocales();
$locales = $this->localeManager->getLocales();
}

$domains = $input->getOption('domains') ? explode(',', $input->getOption('domains')) : array();
Expand Down Expand Up @@ -258,7 +272,7 @@ protected function importTranslationFiles($finder)
return;
}

$importer = $this->getContainer()->get('lexik_translation.importer.file');
$importer = $this->fileImporter;
$importer->setCaseInsensitiveInsert($this->input->getOption('case-insensitive'));

foreach ($finder as $file) {
Expand Down Expand Up @@ -314,7 +328,7 @@ protected function findTranslationsFiles($path, array $locales, array $domains,
*/
protected function getFileNamePattern(array $locales, array $domains)
{
$formats = $this->getContainer()->get('lexik_translation.translator')->getFormats();
$formats = $this->translator->getFormats();

if (count($domains)) {
$regex = sprintf('/((%s)\.(%s)\.(%s))/', implode('|', $domains), implode('|', $locales), implode('|', $formats));
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('lexik_translation');
$treeBuilder = new TreeBuilder('lexik_translation');
$rootNode = $treeBuilder->getRootNode();

$storages = array(
StorageInterface::STORAGE_ORM,
Expand Down
7 changes: 7 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,17 @@
<!-- Command -->
<service id="Lexik\Bundle\TranslationBundle\Command\ImportTranslationsCommand" class="%lexik_translation.command.import_translations.class%">
<argument type="service" id="translator" />
<argument type="service" id="lexik_translation.locale.manager" />
<argument type="service" id="lexik_translation.importer.file" />
<tag name="console.command" />
</service>

<service id="Lexik\Bundle\TranslationBundle\Command\ExportTranslationsCommand" class="%lexik_translation.command.export_translations.class%">
<argument type="service" id="translator" />
<argument type="service" id="lexik_translation.translation_storage" />
<argument>%kernel.root_dir%</argument>
<argument type="service" id="filesystem" />
<argument type="service" id="lexik_translation.exporter_collector" />
<tag name="console.command" />
</service>

Expand Down