Skip to content

Commit

Permalink
updaded code, addded missing ouput message
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Girard committed Aug 6, 2015
1 parent 759be73 commit 83228db
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 64 deletions.
48 changes: 25 additions & 23 deletions Command/ExportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,37 @@ protected function exportFile(FileInterface $file)
->get('lexik_translation.translation_storage')
->getTranslationsFromFile($file, $onlyUpdated);

if (count($translations) > 0) {
$format = $this->input->getOption('format') ? $this->input->getOption('format') : $file->getExtention();
if (count($translations) < 1) {
$this->output->writeln('<comment>No translations to export.</comment>');

// we don't write vendors file, translations will be exported in %kernel.root_dir%/Resources/translations
if (false !== strpos($file->getPath(), 'vendor/') || $override) {
$outputPath = sprintf('%s/Resources/translations', $rootDir);
} else {
$outputPath = sprintf('%s/%s', $rootDir, $file->getPath());
}
return;
}

$this->output->writeln(sprintf('<info># OutputPath "%s":</info>', $outputPath));
$format = $this->input->getOption('format') ? $this->input->getOption('format') : $file->getExtention();

// ensure the path exists
if ($this->input->getOption('export-path')) {
/** @var Filesystem $fs */
$fs = $this->getContainer()->get('filesystem');
if (!$fs->exists($outputPath)) {
$fs->mkdir($outputPath);
}
}
// we don't write vendors file, translations will be exported in %kernel.root_dir%/Resources/translations
if (false !== strpos($file->getPath(), 'vendor/') || $override) {
$outputPath = sprintf('%s/Resources/translations', $rootDir);
} else {
$outputPath = sprintf('%s/%s', $rootDir, $file->getPath());
}

$outputFile = sprintf('%s/%s.%s.%s', $outputPath, $file->getDomain(), $file->getLocale(), $format);
$this->output->writeln(sprintf('<info># OutputFile "%s":</info>', $outputFile));
$this->output->writeln(sprintf('<info># OutputPath "%s":</info>', $outputPath));

$translations = $this->mergeExistingTranslations($file, $outputFile, $translations);
$this->doExport($outputFile, $translations, $format);
} else {
$this->output->writeln('<comment>No translations to export.</comment>');
// ensure the path exists
if ($this->input->getOption('export-path')) {
/** @var Filesystem $fs */
$fs = $this->getContainer()->get('filesystem');
if (!$fs->exists($outputPath)) {
$fs->mkdir($outputPath);
}
}

$outputFile = sprintf('%s/%s.%s.%s', $outputPath, $file->getDomain(), $file->getLocale(), $format);
$this->output->writeln(sprintf('<info># OutputFile "%s":</info>', $outputFile));

$translations = $this->mergeExistingTranslations($file, $outputFile, $translations);
$this->doExport($outputFile, $translations, $format);
}

/**
Expand Down
98 changes: 57 additions & 41 deletions Command/ImportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->input = $input;
$this->output = $output;

$this->checkOptions();

$locales = $this->input->getOption('locales');
if (empty($locales)) {
$locales = $this->getContainer()->getParameter('lexik_translation.managed_locales');
}

if ($this->input->getOption('import-path')
&& ($this->input->getOption('globals')
|| $this->input->getOption('merge')
|| $this->input->getOption('only-vendors'))) {
throw new \LogicException('You cannot use "globals", "merge" or "only-vendors" and "import-path" at the same time.');
}

if ($this->input->getOption('only-vendors') && $this->input->getOption('globals')) {
throw new \LogicException('You cannot use "globals" and "only-vendors" at the same time.');
}

$domains = $input->getOption('domains') ? explode(',', $input->getOption('domains')) : array();

$bundleName = $this->input->getArgument('bundle');
Expand All @@ -83,11 +74,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (null !== $bundle->getParent()) {
// due to symfony's bundle inheritance if a bundle has a parent it is fetched first.
// so we tell getBundle to NOT fetch the first if a parent is present
$bundle = $this->getApplication()->getKernel()->getBundle($bundle->getParent(), false)[1];
$bundles = $this->getApplication()->getKernel()->getBundle($bundle->getParent(), false);
$bundle = $bundles[1];
$this->output->writeln('<info>Using: ' . $bundle->getName() . ' as bundle to lookup translations files for.');
}

$this->importBundleTranslationFiles($bundle, $locales, $domains, (bool) $this->input->getOption('globals'));

} else if(!$this->input->getOption('import-path')) {

if (!$this->input->getOption('merge') && !$this->input->getOption('only-vendors')) {
Expand All @@ -113,17 +106,42 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if ($this->input->getOption('import-path')) {
$this->importTranslationFilesFromPath($this->input->getOption('import-path'), $locales, $domains);
$importPath = $this->input->getOption('import-path');
if (!empty($importPath)) {
$this->output->writeln(sprintf('<info>*** Importing translations from path "%s" ***</info>', $importPath));
$this->importTranslationFilesFromPath($importPath, $locales, $domains);
}

if ($this->input->getOption('cache-clear')) {
$this->output->writeln('<info>Removing translations cache files ...</info>');
$this->removeTranslationCache();
$this->getContainer()->get('translator')->removeLocalesCacheFiles($locales);
}
}

/**
* Checks if given options are compatible.
*/
protected function checkOptions()
{
if ($this->input->getOption('only-vendors') && $this->input->getOption('globals')) {
throw new \LogicException('You cannot use "globals" and "only-vendors" at the same time.');
}

if ($this->input->getOption('import-path')
&& ($this->input->getOption('globals')
|| $this->input->getOption('merge')
|| $this->input->getOption('only-vendors'))) {
throw new \LogicException('You cannot use "globals", "merge" or "only-vendors" and "import-path" at the same time.');
}
}

protected function importTranslationFilesFromPath($path, array $locales, array $domains) {
/**
* @param string $path
* @param array $locales
* @param array $domains
*/
protected function importTranslationFilesFromPath($path, array $locales, array $domains)
{
$finder = $this->findTranslationsFiles($path, $locales, $domains, false);
$this->importTranslationFiles($finder);
}
Expand Down Expand Up @@ -212,22 +230,24 @@ protected function importBundleTranslationFiles(BundleInterface $bundle, $locale
*/
protected function importTranslationFiles($finder)
{
if ($finder instanceof Finder) {
$importer = $this->getContainer()->get('lexik_translation.importer.file');
$importer->setCaseInsensitiveInsert($this->input->getOption('case-insensitive'));

foreach ($finder as $file) {
$this->output->write(sprintf('Importing <comment>"%s"</comment> ... ', $file->getPathname()));
$number = $importer->import($file, $this->input->getOption('force'), $this->input->getOption('merge'));
$this->output->writeln(sprintf('%d translations', $number));

$skipped = $importer->getSkippedKeys();
if (count($skipped) > 0) {
$this->output->writeln(sprintf(' <error>[!]</error> The following keys have been skipped: "%s".', implode('", "', $skipped)));
}
}
} else {
if (!$finder instanceof Finder) {
$this->output->writeln('No file to import');

return;
}

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

foreach ($finder as $file) {
$this->output->write(sprintf('Importing <comment>"%s"</comment> ... ', $file->getPathname()));
$number = $importer->import($file, $this->input->getOption('force'), $this->input->getOption('merge'));
$this->output->writeln(sprintf('%d translations', $number));

$skipped = $importer->getSkippedKeys();
if (count($skipped) > 0) {
$this->output->writeln(sprintf(' <error>[!]</error> The following keys has been skipped: "%s".', implode('", "', $skipped)));
}
}
}

Expand All @@ -247,7 +267,12 @@ protected function findTranslationsFiles($path, array $locales, array $domains,
$path = preg_replace('#'. preg_quote(DIRECTORY_SEPARATOR, '#') .'#', '/', $path);
}

$dir = true == $autocompletePath ? (0 === strpos($path, $this->getApplication()->getKernel()->getRootDir() . '/Resources') ? $path : $path . '/Resources/translations') : $path;
if (true === $autocompletePath) {
$dir = (0 === strpos($path, $this->getApplication()->getKernel()->getRootDir() . '/Resources')) ? $path : $path . '/Resources/translations';
} else {
$dir = $path;
}

$this->output->writeln('<info>*** Using dir ' . $dir . ' to lookup translation files. ***</info>');

if (is_dir($dir)) {
Expand Down Expand Up @@ -277,13 +302,4 @@ protected function getFileNamePattern(array $locales, array $domains)

return $regex;
}

/**
* Remove translation cache files managed locales.
*/
protected function removeTranslationCache()
{
$locales = $this->getContainer()->getParameter('lexik_translation.managed_locales');
$this->getContainer()->get('translator')->removeLocalesCacheFiles($locales);
}
}

0 comments on commit 83228db

Please sign in to comment.