Add option to ignore keys in cli tool

This commit is contained in:
Alexis Degrugillier
2018-03-11 10:53:32 +01:00
parent 2f98978f6d
commit f4da012940
5 changed files with 122 additions and 6 deletions

View File

@@ -116,6 +116,30 @@ class I18nData {
}
}
/**
* WARNING! This is valid only for ignore files. It's not the best way to
* handle that but as it's meant to be used only for the cli tool, there
* is no point of spending time on making it better than that.
*
* Ignore a key from a language, or reverse it.
*
* @param string $key
* @param string $language
* @param boolean $reverse
*/
public function ignore($key, $language, $reverse = false) {
$index = array_search($key, $this->data[$language]);
if ($index && $reverse) {
unset($this->data[$language][$index]);
return;
}
if ($index && !$reverse) {
return;
}
$this->data[$language][] = $key;
}
/**
* Check if the data has changed
*

View File

@@ -1,8 +1,9 @@
<?php
require_once __DIR__ . '/I18nData.php';
require_once __DIR__ . '/I18nFileInterface.php';
class i18nFile {
class I18nFile implements I18nFileInterface{
private $i18nPath;
@@ -11,6 +12,7 @@ class i18nFile {
}
public function load() {
$i18n = array();
$dirs = new DirectoryIterator($this->i18nPath);
foreach ($dirs as $dir) {
if ($dir->isDot()) {

View File

@@ -0,0 +1,10 @@
<?php
require_once __DIR__ . '/I18nData.php';
interface I18nFileInterface {
public function load();
public function dump(I18nData $i18n);
}

View File

@@ -0,0 +1,64 @@
<?php
require_once __DIR__ . '/I18nData.php';
require_once __DIR__ . '/I18nFileInterface.php';
class I18nIgnoreFile implements I18nFileInterface {
private $i18nPath;
public function __construct() {
$this->i18nPath = __DIR__ . '/ignore';
}
public function dump(I18nData $i18n) {
foreach ($i18n->getData() as $language => $content) {
$filename = $this->i18nPath . DIRECTORY_SEPARATOR . $language . '.php';
file_put_contents($filename, $this->format($content));
}
}
public function load() {
$i18n = array();
$files = new DirectoryIterator($this->i18nPath);
foreach ($files as $file) {
if (!$file->isFile()) {
continue;
}
$i18n[$file->getBasename('.php')] = (include $file->getPathname());
}
return new I18nData($i18n);
}
/**
* Format an array of translation
*
* It takes an array of translation and format it to be dumped in a
* translation file. The array is first converted to a string then some
* formatting regexes are applied to match the original content.
*
* @param array $translation
* @return string
*/
private function format($translation) {
$translation = var_export(($translation), true);
$patterns = array(
'/array \(/',
'/=>\s*array/',
'/ {2}/',
'/\d+ => /',
);
$replacements = array(
'array(',
'=> array',
"\t", // Double quoting is mandatory to have a tab instead of the \t string
'',
);
$translation = preg_replace($patterns, $replacements, $translation);
// Double quoting is mandatory to have new lines instead of \n strings
return sprintf("<?php\n\nreturn %s;\n", $translation);
}
}

View File

@@ -1,6 +1,6 @@
<?php
$options = getopt("a:hk:l:v:");
$options = getopt("a:hk:l:rv:");
if (array_key_exists('h', $options)) {
help();
@@ -10,9 +10,13 @@ if (!array_key_exists('a', $options)) {
error('You need to specify the action to perform.');
}
require_once __DIR__ . '/i18n/I18nFile.php';
$i18nFile = new I18nFile();
if ('ignore' === $options['a']) {
require_once __DIR__ . '/i18n/I18nIgnoreFile.php';
$i18nFile = new I18nIgnoreFile();
} else {
require_once __DIR__ . '/i18n/I18nFile.php';
$i18nFile = new I18nFile();
}
$i18nData = $i18nFile->load();
switch ($options['a']) {
@@ -44,6 +48,13 @@ switch ($options['a']) {
case 'format' :
$i18nFile->dump($i18nData);
break;
case 'ignore' :
if (array_key_exists('l', $options) && array_key_exists('k', $options)) {
$i18nData->ignore($options['k'], $options['l'], array_key_exists('r', $options));
} else {
error('You need to specify a valid set of options.');
}
break;
default :
help();
}
@@ -80,7 +91,7 @@ DESCRIPTION
-a=ACTION
select the action to perform. Available actions are add, delete,
duplicate, and format. This option is mandatory.
duplicate, format, and ignore. This option is mandatory.
-k=KEY select the key to work on.
-v=VAL select the value to set.
-l=LANG select the language to work on.
@@ -105,6 +116,11 @@ Exemple 5: duplicate a key. It duplicates the key from the referential in every
Exemple 6: format i18n files.
php %1\$s -a format
Exemple 7: ignore a key. It adds the key in the ignore file to mark it as translated.
php %1\$s -a ignore -k my_key -l my_lang
Exemple 8: revert ignore a key. It removes the key from the ignore file.
php %1\$s -a ignore -r -k my_key -l my_lang\n\n
HELP;
$file = str_replace(__DIR__ . '/', '', __FILE__);
echo sprintf($help, $file);