Skip to content

Commit

Permalink
2821 translation migration (#2830)
Browse files Browse the repository at this point in the history
* *

* working  commands
  • Loading branch information
novia713 authored and enzolutions committed Oct 20, 2016
1 parent cda7f34 commit f580bc2
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 65 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "drupal/console",
"description": "The Drupal Console is a CLI tool to generate boilerplate code, interact and debug Drupal 8.",
"keywords": ["Drupal", "Console", "Development", "Symfony"],
"keywords": ["Drupal", "Console", "Developmdrent", "Symfony"],
"homepage": "http://drupalconsole.com/",
"type": "project",
"license": "GPL-2.0+",
Expand Down Expand Up @@ -39,7 +39,7 @@
"php": "^5.5.9 || ^7.0",
"alchemy/zippy": "0.3.5",
"composer/installers": "~1.0",
"drupal/console-core" : "^1.0",
"drupal/console-core" : "*",
"symfony/css-selector": "~2.8",
"symfony/debug": "~2.6|~2.8",
"symfony/dom-crawler": "~2.7|~2.8",
Expand Down
4 changes: 4 additions & 0 deletions config/services/drupal-console/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ services:
- { name: drupal.command }
console.translation_cleanup:
class: Drupal\Console\Command\Develop\TranslationCleanupCommand
arguments: ['@console.root', '@console.configuration_manager']
tags:
- { name: drupal.command }
console.translation_pending:
class: Drupal\Console\Command\Develop\TranslationPendingCommand
arguments: ['@console.root', '@console.configuration_manager', '@console.nested_array']
tags:
- { name: drupal.command }
console.translation_stats:
class: Drupal\Console\Command\Develop\TranslationStatsCommand
arguments: ['@console.root', '@console.configuration_manager', '@console.renderer', '@console.nested_array']
tags:
- { name: drupal.command }
console.translation_sync:
class: Drupal\Console\Command\Develop\TranslationSyncCommand
arguments: ['@console.root', '@console.configuration_manager']
tags:
- { name: drupal.command }
62 changes: 40 additions & 22 deletions src/Command/Develop/TranslationCleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,35 @@
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Style\DrupalStyle;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Utils\ConfigurationManager;

class TranslationCleanupCommand extends Command
{
use CommandTrait;

/**
* @var string
*/
protected $consoleRoot;

/**
* @var ConfigurationManager
*/
protected $configurationManager;

/**
* TranslationCleanupCommand constructor.
*
* @param $consoleRoot
* @param configurationManager $configurationManager
*
*/
public function __construct()
{
public function __construct(
$consoleRoot,
ConfigurationManager $configurationManager
) {
$this->consoleRoot = $consoleRoot;
$this->configurationManager = $configurationManager;
parent::__construct();
}

Expand Down Expand Up @@ -55,10 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$language = $input->getArgument('language');

$application = $this->getApplication();
$appRoot = $application->getDirectoryRoot();

$languages = $application->getConfig()->get('application.languages');
$languages = $this->configurationManager->getConfiguration()->get('application.languages');
unset($languages['en']);

if ($language && !isset($languages[$language])) {
Expand All @@ -75,31 +91,33 @@ protected function execute(InputInterface $input, OutputInterface $output)
$languages = [$language => $languages[$language]];
}

$this->cleanupTranslations($io, $language, $languages, $appRoot);
$this->cleanupTranslations($io, $language, $languages);

$io->success(
$this->trans('commands.translation.cleanup.messages.success')
);
}

protected function cleanupTranslations($io, $language = null, $languages, $appRoot)
protected function cleanupTranslations($io, $language = null, $languages)
{
$finder = new Finder();

foreach ($languages as $langCode => $languageName) {
foreach ($finder->files()->name('*.yml')->in($appRoot . 'config/translations/' . $langCode) as $file) {
$filename = $file->getBasename('.yml');
if (!file_exists($appRoot . 'config/translations/en/' . $filename . '.yml')) {
$io->info(
sprintf(
$this->trans('commands.translation.cleanup.messages.file-deleted'),
$filename,
$languageName
)
);
unlink($appRoot . 'config/translations/' . $langCode. '/' . $filename . '.yml');
}
}
}
if (file_exists($this->consoleRoot . sprintf( DRUPAL_CONSOLE_LANGUAGE, $langCode ))) {
foreach ($finder->files()->name('*.yml')->in($this->consoleRoot . sprintf( DRUPAL_CONSOLE_LANGUAGE, $langCode )) as $file) {
$filename = $file->getBasename('.yml');
if (!file_exists($this->consoleRoot . sprintf( DRUPAL_CONSOLE_LANGUAGE, 'en') . $filename . '.yml')) {
$io->info(
sprintf(
$this->trans('commands.translation.cleanup.messages.file-deleted'),
$filename,
$languageName
)
);
unlink($this->consoleRoot . sprintf( DRUPAL_CONSOLE_LANGUAGE, $langCode ). '/' . $filename . '.yml');
}
}
}
}
}
}
60 changes: 47 additions & 13 deletions src/Command/Develop/TranslationPendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,50 @@
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Style\DrupalStyle;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Utils\ConfigurationManager;
use Drupal\Console\Utils\NestedArray;

class TranslationPendingCommand extends Command
{
use TranslationTrait;
use CommandTrait;

/**
* @var string
*/
protected $consoleRoot;

/**
* @var ConfigurationManager
*/
protected $configurationManager;

/**
* @var NestedArray
*/
protected $nestedArray;


/**
* TranslationPendingCommand constructor.
*
* @param $consoleRoot
* @param $configurationManager
* @param NestedArray $nestedArray
*
*/
public function __construct()
{
public function __construct(
$consoleRoot,
ConfigurationManager $configurationManager,
NestedArray $nestedArray
) {
$this->consoleRoot = $consoleRoot;
$this->configurationManager = $configurationManager;
$this->nestedArray = $nestedArray;
parent::__construct();
}


/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -65,10 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$language = $input->getArgument('language');
$file = $input->getOption('file');

$application = $this->getApplication();
$appRoot = $application->getDirectoryRoot();

$languages = $application->getConfig()->get('application.languages');
$languages = $this->configurationManager->getConfiguration()->get('application.languages');
unset($languages['en']);

if ($language && !isset($languages[$language])) {
Expand All @@ -85,7 +112,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$languages = [$language => $languages[$language]];
}

$pendingTranslations = $this->determinePendingTranslation($io, $language, $languages, $file, $appRoot);
$pendingTranslations = $this->determinePendingTranslation($io, $language, $languages, $file);

if ($file) {
$io->success(
Expand All @@ -107,14 +134,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

protected function determinePendingTranslation($io, $language = null, $languages, $fileFilter, $appRoot)
protected function determinePendingTranslation($io, $language = null, $languages, $fileFilter)
{
$nestedArray = $this->getNestedArrayHelper();
$englishFilesFinder = new Finder();
$yaml = new Parser();
$statistics = [];

$englishDirectory = $appRoot . 'config/translations/en';
$englishDirectory = $this->consoleRoot .
sprintf(
DRUPAL_CONSOLE_LANGUAGE,
'en'
);

$englishFiles = $englishFilesFinder->files()->name('*.yml')->in($englishDirectory);

Expand All @@ -135,7 +165,11 @@ protected function determinePendingTranslation($io, $language = null, $languages
}

foreach ($languages as $langCode => $languageName) {
$languageDir = $appRoot . 'config/translations/' . $langCode;
$languageDir = $this->consoleRoot .
sprintf(
DRUPAL_CONSOLE_LANGUAGE,
$langCode
);
if (isset($language) && $langCode != $language) {
continue;
}
Expand All @@ -159,12 +193,12 @@ protected function determinePendingTranslation($io, $language = null, $languages
}

$diffStatistics = ['total' => 0, 'equal' => 0, 'diff' => 0];
$diff = $nestedArray->arrayDiff($englishFileParsed, $resourceTranslatedParsed, true, $diffStatistics);
$diff = $this->nestedArray->arrayDiff($englishFileParsed, $resourceTranslatedParsed, true, $diffStatistics);

if (!empty($diff)) {
$diffFlatten = array();
$keyFlatten = '';
$nestedArray->yamlFlattenArray($diff, $diffFlatten, $keyFlatten);
$this->nestedArray->yamlFlattenArray($diff, $diffFlatten, $keyFlatten);

$tableHeader = [
$this->trans('commands.yaml.diff.messages.key'),
Expand Down
Loading

0 comments on commit f580bc2

Please sign in to comment.