Skip to content

Commit

Permalink
simple CLI Module to display missing locales keys
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Sep 6, 2016
1 parent 02fc73f commit 09c541e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
72 changes: 72 additions & 0 deletions api/core/Directus/Console/Modules/LanguageModule.php
@@ -0,0 +1,72 @@
<?php

namespace Directus\Console\Modules;

use Directus\Bootstrap;
use Directus\Util\ArrayUtils;


class LanguageModule extends ModuleBase
{

protected $__module_name = 'language';
protected $__module_description = 'command to know the missing/removed language keys';
protected $commands_help;
protected $help;

public function __construct()
{
$this->help = array(
'diff' => __t('The Languages difference keys.')
);

$this->commands_help = array(
'diff' => __t('The Languages difference keys.')
);
}

public function cmdHelp($args, $extra)
{
echo PHP_EOL . __t('Directus Command ') . $this->__module_name . ':' . $extra[0] . __t(' help') . PHP_EOL . PHP_EOL;
echo "\t" . $this->commands_help[$extra[0]] . PHP_EOL;
echo PHP_EOL . PHP_EOL;
}

protected function cmdDiff($args, $extra)
{
// TODO: Accepts a language code to compare it with another. (Default: English)
$languageManager = Bootstrap::get('languagesManager');

$languages = $languageManager->getLanguageList();

if (count($languages) <= 1) {
return [];
}

$languagesPhrases = [];
foreach ($languages as $code => $name) {
$languagesPhrases[] = [
'code' => $code,
'name' => $name,
'phrases_keys' => get_locale_keys($code),
'diff' => []
];
}

// =============================================================================
// A language to compare to the rest of languages keys
// =============================================================================
$mainLanguage = $languagesPhrases[0];
unset($languagesPhrases[0]);

foreach ($languagesPhrases as $language) {
$diff = ArrayUtils::missing($mainLanguage['phrases_keys'], $language['phrases_keys']);
echo "--------------------" . PHP_EOL;
echo $language['name'] . " has " . count($diff) . " missing/removed keys compared against " . $mainLanguage['name'] . PHP_EOL;
foreach ($diff as $key) {
echo "\t - " . $key . PHP_EOL;
}
echo "--------------------" . PHP_EOL . PHP_EOL;
}
}
}
20 changes: 20 additions & 0 deletions api/core/Directus/Util/ArrayUtils.php
Expand Up @@ -135,4 +135,24 @@ public static function flatKey($separator, $array, $prepend = '')
return $results;
}

/**
* Get the missing values from a array in another array
*
* @param array $from
* @param array $target
*
* @return array
*/
public static function missing(array $from, array $target)
{
$missing = [];

foreach($target as $value) {
if (!in_array($value, $from)) {
$missing[] = $value;
}
}

return $missing;
}
}
18 changes: 11 additions & 7 deletions api/core/functions.php
Expand Up @@ -422,12 +422,11 @@ function get_locales_filename()
*/
function get_locale_keys($locale)
{
$content = file_get_contents(BASE_PATH . '/api/locales/' . $locale . '.json');
$json = json_decode($content, true);
$phrases = get_locale_phrases($locale);

$keys = [];
if ($json) {
$keys = array_keys($json);
if ($phrases) {
$keys = array_keys($phrases);
}

return $keys;
Expand All @@ -453,9 +452,14 @@ function is_locale_available($locale)
}

if (!function_exists('get_default_phrases')) {
function get_default_phrases()
{
$phrasesPath = BASE_PATH . '/api/locales/en.json';
function get_default_phrases() {
return get_locale_phrases('en');
}
}

if (!function_exists('get_locale_phrases')) {
function get_locale_phrases($locale) {
$phrasesPath = BASE_PATH . '/api/locales/' . $locale . '.json';

return json_decode(file_get_contents($phrasesPath), true);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/api/Util/ArrayUtilsTest.php
Expand Up @@ -73,4 +73,13 @@ public function testFlatKeys()
$this->assertArrayHasKey('user_name', $result);
$this->assertArrayHasKey('user_country_name', $result);
}

public function testMissing()
{
$array1 = ['one', 'two', 'three', 'five'];
$array2 = ['one', 'four', 'five'];
$result = ArrayUtils::missing($array1, $array2);

$this->assertTrue(in_array('four', $result));
}
}

0 comments on commit 09c541e

Please sign in to comment.