Merge pull request #174 from caouecs/check_localizations

feature: check localization command
This commit is contained in:
Dmitry Khomutov 2018-05-01 19:27:42 +07:00 committed by GitHub
commit 3cdedcd972
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 177 additions and 4 deletions

View file

@ -0,0 +1,166 @@
<?php
namespace PHPCensor\Command;
use Monolog\Logger;
use PHPCensor\Service\BuildService;
use PHPCensor\Store\Factory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Check localizations.
*/
class CheckLocalizationCommand extends Command
{
/**
* @var array
*/
protected $excluded = ['lang.en.php'];
/**
* Construct.
*/
public function __construct()
{
parent::__construct();
$this->basePath = __DIR__.'/../Languages';
}
/**
* Configure.
*/
protected function configure()
{
$this
->setName('php-censor:check-localizations')
->addOption('same', 0, InputOption::VALUE_OPTIONAL, 'Same than English version (0 = no, 1 = yes)')
->addOption(
'langs',
[],
InputOption::VALUE_OPTIONAL,
'List of languages separated by commas. By default, all languages'
)
->setDescription('Check localizations.');
}
/**
* Loops through running.
*
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('');
$output->writeln('<info>Check localizations!</info>');
$output->writeln('');
$sameThanEnglish = (null !== $input->getOption('same')) ? $input->getOption('same') : false;
$languagesList = (null !== $input->getOption('langs')) ? explode(',', $input->getOption('langs')) : [];
// Get English version
$english = $this->getTranslations($this->basePath.'/lang.en.php');
$othersLanguages = $this->getLanguages($languagesList);
$diffs = $this->compareTranslations($english, $othersLanguages);
foreach ($diffs as $language => $value) {
$output->writeln('');
$output->writeln($language.' : ');
if (!empty($value['not_present'])) {
$output->writeln(' * not present : '.implode(', ', $value['not_present']));
}
if ($sameThanEnglish === '1' && !empty($value['same'])) {
$output->writeln(' * same than English : '.implode(', ', $value['same']));
}
$output->writeln('');
}
}
/**
* Returns array of translations by language.
*
* @param string $language language code
*
* @return array
*/
private function getTranslations($language)
{
return [
include($language)
];
}
/**
* Returns list of languages.
*
* @param array $languagesList
*
* @return array
*/
private function getLanguages(array $languagesList = [])
{
$files = glob($this->basePath.'/*.php');
$languages = array_map(function ($dir) use ($languagesList) {
$name = basename($dir);
// Not exclused
if (in_array($name, $this->excluded, true)) {
return null;
}
// Check if in list of languages.
if (!empty($languagesList)) {
$languageOfName = explode('.', $name);
if (null == $languageOfName[1] || !in_array($languageOfName[1], $languagesList)) {
return null;
}
}
return $name;
}, $files);
return array_filter($languages);
}
/**
* Compare translations.
*
* @param array $default language by default
* @param array $languages others languages
*
* @return array
*/
private function compareTranslations(array $default, array $languages)
{
$diffs = [];
// Return diff language by language
foreach ($languages as $language) {
$current = $this->getTranslations($this->basePath.'/'.$language);
foreach ($default as $key => $values) {
$keyValues = array_keys($values);
foreach ($keyValues as $key2) {
if (!isset($current[$key][$key2])) {
$diffs[$language]['not_present'][] = $key2;
} elseif ($current[$key][$key2] === $default[$key][$key2]) {
$diffs[$language]['same'][] = $key2;
}
}
}
}
return $diffs;
}
}

View file

@ -2,11 +2,10 @@
namespace PHPCensor\Console;
use PHPCensor\Config;
use PHPCensor\Store\Factory;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPCensor\Command\CheckLocalizationCommand;
use PHPCensor\Command\CreateAdminCommand;
use PHPCensor\Command\CreateBuildCommand;
use PHPCensor\Command\InstallCommand;
@ -15,17 +14,19 @@ use PHPCensor\Command\RebuildQueueCommand;
use PHPCensor\Command\RunCommand;
use PHPCensor\Command\ScheduleBuildCommand;
use PHPCensor\Command\WorkerCommand;
use PHPCensor\Config;
use PHPCensor\Logging\Handler;
use PHPCensor\Service\BuildService;
use PHPCensor\Store\BuildStore;
use PHPCensor\Store\Factory;
use PHPCensor\Store\ProjectStore;
use PHPCensor\Store\UserStore;
use Symfony\Component\Console\Application as BaseApplication;
use Phinx\Config\Config as PhinxConfig;
use Phinx\Console\Command\Create;
use Phinx\Console\Command\Migrate;
use Phinx\Console\Command\Rollback;
use Phinx\Console\Command\Status;
use Phinx\Config\Config as PhinxConfig;
use Symfony\Component\Console\Application as BaseApplication;
/**
* Class Application
@ -148,8 +149,14 @@ LOGO;
$this->add(new WorkerCommand($logger));
$this->add(new RebuildQueueCommand($logger));
$this->add(new ScheduleBuildCommand($projectStore, $buildStore, new BuildService($buildStore)));
$this->add(new CheckLocalizationCommand());
}
/**
* Returns help.
*
* @return string
*/
public function getHelp()
{
return self::LOGO . parent::getHelp();