diff options
author | Imbasaur <yarrusg@gmail.com> | 2016-04-13 17:05:59 +0200 |
---|---|---|
committer | Imbasaur <yarrusg@gmail.com> | 2016-04-13 17:05:59 +0200 |
commit | 99f275e5bb033cca33eee87b0e914645730f13d1 (patch) | |
tree | ad845419d56304f2bf014744f0878186f7155a3c /app/Console/LocaleComparatorCommand.php | |
parent | 13d5bd8e48bd6c0109d1272da58a8879bf9a6737 (diff) | |
parent | cd5bf9d4d214ec9282b706c26bb27cabf150ee63 (diff) |
Merge pull request #1 from fguillot/master
Diffstat (limited to 'app/Console/LocaleComparatorCommand.php')
-rw-r--r-- | app/Console/LocaleComparatorCommand.php | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/app/Console/LocaleComparatorCommand.php b/app/Console/LocaleComparatorCommand.php new file mode 100644 index 00000000..de83714f --- /dev/null +++ b/app/Console/LocaleComparatorCommand.php @@ -0,0 +1,81 @@ +<?php + +namespace Kanboard\Console; + +use RecursiveIteratorIterator; +use RecursiveDirectoryIterator; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class LocaleComparatorCommand extends BaseCommand +{ + const REF_LOCALE = 'fr_FR'; + + protected function configure() + { + $this + ->setName('locale:compare') + ->setDescription('Compare application translations with the '.self::REF_LOCALE.' locale'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $strings = array(); + $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('app')); + $it->rewind(); + + while ($it->valid()) { + if (! $it->isDot() && substr($it->key(), -4) === '.php') { + $strings = array_merge($strings, $this->search($it->key())); + } + + $it->next(); + } + + $this->compare(array_unique($strings)); + } + + public function show(array $strings) + { + foreach ($strings as $string) { + echo " '".str_replace("'", "\'", $string)."' => '',".PHP_EOL; + } + } + + public function compare(array $strings) + { + $reference_file = 'app/Locale/'.self::REF_LOCALE.'/translations.php'; + $reference = include $reference_file; + + echo str_repeat('#', 70).PHP_EOL; + echo 'MISSING STRINGS'.PHP_EOL; + echo str_repeat('#', 70).PHP_EOL; + $this->show(array_diff($strings, array_keys($reference))); + + echo str_repeat('#', 70).PHP_EOL; + echo 'USELESS STRINGS'.PHP_EOL; + echo str_repeat('#', 70).PHP_EOL; + $this->show(array_diff(array_keys($reference), $strings)); + } + + public function search($filename) + { + $content = file_get_contents($filename); + $strings = array(); + + if (preg_match_all('/\b[et]\((\'\K.*?\') *[\)\,]/', $content, $matches) && isset($matches[1])) { + $strings = $matches[1]; + } + + if (preg_match_all('/\bdt\((\'\K.*?\') *[\)\,]/', $content, $matches) && isset($matches[1])) { + $strings = array_merge($strings, $matches[1]); + } + + array_walk($strings, function (&$value) { + $value = trim($value, "'"); + $value = str_replace("\'", "'", $value); + }); + + return $strings; + } +} |