From 3e49b44839237693ce1a8151325942704917f6c6 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Wed, 4 Dec 2019 08:27:39 +0100 Subject: Update i18n cli tools (#2673) * Update i18n cli tools Few things were bugging me when using the cli tool for i18n. So I've modified the tools to be easier to use. First, the tool automatically adds missing keys to all languages. This way, we always have all keys in all languages. Second, the tool detects all untranslated keys and adds automatically the todo comment after the value. Third, when adding a new key, the key is pushed to all languages at once. There is no need to duplicate it manually. Thus making the duplication process obsolete. Fourth, translation and ignore keys are manipulated at the same time. Thus we don't have obsolete ignored strings anymore. * Add i18n rules I find that having the common rules in the Makefile is easier to use, as long as you know they are here. As it is self documented, people will see the new rules when using make. * Use long parameters in Makefile I find that using long parameters in scripts makes it easier to understand what's going on. So I've switched all short parameters to long one. * Format all i18n files I've used the updated version of the cli tools to have some output that can be consistently formated. This commit is a huge formating commit. Nothing was added but some comments were removed in the process. --- cli/i18n/I18nData.php | 159 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 113 insertions(+), 46 deletions(-) (limited to 'cli/i18n/I18nData.php') diff --git a/cli/i18n/I18nData.php b/cli/i18n/I18nData.php index 2178d330d..a6d260a8f 100644 --- a/cli/i18n/I18nData.php +++ b/cli/i18n/I18nData.php @@ -5,15 +5,95 @@ class I18nData { const REFERENCE_LANGUAGE = 'en'; private $data = array(); - private $originalData = array(); + private $ignore = array(); - public function __construct($data) { + public function __construct($data, $ignore) { $this->data = $data; - $this->originalData = $data; + $this->ignore = $ignore; + + $this->synchonizeKeys(); } public function getData() { - return $this->data; + $output = array(); + $reference = $this->getReferenceLanguage(); + $languages = $this->getNonReferenceLanguages(); + + foreach ($reference as $file => $values) { + foreach ($values as $key => $value) { + $output[static::REFERENCE_LANGUAGE][$file][$key] = $value; + foreach ($languages as $language) { + if ($this->data[$language][$file][$key] !== $value) { + // This value is translated, there is no need to flag it. + $output[$language][$file][$key] = $this->data[$language][$file][$key]; + } elseif (array_key_exists($language, $this->ignore) && in_array($key, $this->ignore[$language])) { + // This value is ignored, there is no need to flag it. + $output[$language][$file][$key] = $this->data[$language][$file][$key]; + } else { + // This value is not translated nor ignored, it must be flagged. + $output[$language][$file][$key] = "{$value} -> todo"; + } + } + } + } + + return $output; + } + + public function getIgnore() { + $ignore = array(); + + foreach ($this->ignore as $language => $keys) { + sort($keys); + $ignore[$language] = $keys; + } + + return $ignore; + } + + private function synchonizeKeys() { + $this->addMissingKeysFromReference(); + $this->removeExtraKeysFromOtherLanguages(); + $this->removeUnknownIgnoreKeys(); + } + + private function addMissingKeysFromReference() { + $reference = $this->getReferenceLanguage(); + $languages = $this->getNonReferenceLanguages(); + + foreach ($reference as $file => $values) { + foreach ($values as $key => $value) { + foreach ($languages as $language) { + if (!array_key_exists($key, $this->data[$language][$file])) { + $this->data[$language][$file][$key] = $value; + } + } + } + } + } + + private function removeExtraKeysFromOtherLanguages() { + $reference = $this->getReferenceLanguage(); + foreach ($this->getNonReferenceLanguages() as $language) { + foreach ($this->getLanguage($language) as $file => $values) { + foreach ($values as $key => $value) { + if (!array_key_exists($key, $reference[$file])) { + unset($this->data[$language][$file][$key]); + } + } + } + } + } + + private function removeUnknownIgnoreKeys() { + $reference = $this->getReferenceLanguage(); + foreach ($this->ignore as $language => $keys) { + foreach ($keys as $index => $key) { + if (!array_key_exists($this->getFilenamePrefix($key), $reference) || !array_key_exists($key, $reference[$this->getFilenamePrefix($key)])) { + unset($this->ignore[$language][$index]); + } + } + } } /** @@ -28,6 +108,17 @@ class I18nData { return $languages; } + /** + * Return all available languages without the reference language + * + * @return array + */ + public function getNonReferenceLanguages() { + return array_filter(array_keys($this->data), function ($value) { + return static::REFERENCE_LANGUAGE !== $value; + }); + } + /** * Add a new language. It's a copy of the reference language. * @@ -42,7 +133,7 @@ class I18nData { } /** - * Add a key in the reference language + * Add a new key to all languages. * * @param string $key * @param string $value @@ -53,7 +144,12 @@ class I18nData { 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; + + foreach ($this->getAvailableLanguages() as $language) { + if (!array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) { + $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value; + } + } } /** @@ -75,29 +171,6 @@ class I18nData { $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value; } - /** - * Duplicate a key from the reference language to all other languages - * - * @param string $key - * @throws Exception - */ - public function duplicateKey($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]; - foreach ($this->getAvailableLanguages() as $language) { - if (static::REFERENCE_LANGUAGE === $language) { - continue; - } - if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) { - continue; - } - $this->data[$language][$this->getFilenamePrefix($key)][$key] = $value; - } - } - /** * Remove a key in all languages * @@ -113,14 +186,13 @@ class I18nData { if (array_key_exists($key, $this->data[$language][$this->getFilenamePrefix($key)])) { unset($this->data[$language][$this->getFilenamePrefix($key)][$key]); } + if (array_key_exists($language, $this->ignore) && $position = array_search($key, $this->ignore[$language])) { + unset($this->ignore[$language][$position]); + } } } /** - * 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 @@ -128,25 +200,20 @@ class I18nData { * @param boolean $reverse */ public function ignore($key, $language, $reverse = false) { - $index = array_search($key, $this->data[$language]); + if (!array_key_exists($language, $this->ignore)) { + $this->ignore[$language] = array(); + } - if ($index && $reverse) { - unset($this->data[$language][$index]); + $index = array_search($key, $this->ignore[$language]); + if (false !== $index && $reverse) { + unset($this->ignore[$language][$index]); return; } - if ($index && !$reverse) { + if (false !== $index && !$reverse) { return; } - $this->data[$language][] = $key; - } - /** - * Check if the data has changed - * - * @return bool - */ - public function hasChanged() { - return $this->data !== $this->originalData; + $this->ignore[$language][] = $key; } public function getLanguage($language) { -- cgit v1.2.3