diff options
Diffstat (limited to 'cli/i18n')
| -rw-r--r-- | cli/i18n/I18nData.php | 36 | ||||
| -rw-r--r-- | cli/i18n/I18nFile.php | 4 | ||||
| -rw-r--r-- | cli/i18n/I18nFileInterface.php | 10 | ||||
| -rw-r--r-- | cli/i18n/I18nIgnoreFile.php | 64 |
4 files changed, 109 insertions, 5 deletions
diff --git a/cli/i18n/I18nData.php b/cli/i18n/I18nData.php index b8f958288..2178d330d 100644 --- a/cli/i18n/I18nData.php +++ b/cli/i18n/I18nData.php @@ -49,7 +49,8 @@ class I18nData { * @throws Exception */ public function addKey($key, $value) { - if (array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) { + if (array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) && + array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) { throw new Exception('The selected key already exist.'); } $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key] = $value; @@ -67,7 +68,8 @@ class I18nData { if (!in_array($language, $this->getAvailableLanguages())) { throw new Exception('The selected language does not exist.'); } - if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) { + if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) || + !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) { throw new Exception('The selected key does not exist for the selected language.'); } $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value; @@ -80,7 +82,8 @@ class I18nData { * @throws Exception */ public function duplicateKey($key) { - if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) { + if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) || + !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) { throw new Exception('The selected key does not exist.'); } $value = $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key]; @@ -102,7 +105,8 @@ class I18nData { * @throws Exception */ public function removeKey($key) { - if (!array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) { + if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE]) || + !array_key_exists($key, $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)])) { throw new Exception('The selected key does not exist.'); } foreach ($this->getAvailableLanguages() as $language) { @@ -113,6 +117,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 * * @return bool diff --git a/cli/i18n/I18nFile.php b/cli/i18n/I18nFile.php index a07efdf88..bdcf3c079 100644 --- a/cli/i18n/I18nFile.php +++ b/cli/i18n/I18nFile.php @@ -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()) { diff --git a/cli/i18n/I18nFileInterface.php b/cli/i18n/I18nFileInterface.php new file mode 100644 index 000000000..c5aaf9fcd --- /dev/null +++ b/cli/i18n/I18nFileInterface.php @@ -0,0 +1,10 @@ +<?php + +require_once __DIR__ . '/I18nData.php'; + +interface I18nFileInterface { + + public function load(); + + public function dump(I18nData $i18n); +} diff --git a/cli/i18n/I18nIgnoreFile.php b/cli/i18n/I18nIgnoreFile.php new file mode 100644 index 000000000..714e000ca --- /dev/null +++ b/cli/i18n/I18nIgnoreFile.php @@ -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); + } + +} |
