diff options
| author | 2022-10-18 17:25:04 -0400 | |
|---|---|---|
| committer | 2022-10-18 23:25:04 +0200 | |
| commit | 6fd063fa58a12558c2a01a989a19d4ea611fce6b (patch) | |
| tree | c9b9c3e649d18b0ed263fe85da3ea25872821722 /cli | |
| parent | 0fd608420e197d24a1fbd37d10e9d2372ac18df6 (diff) | |
Fix i18n tool when adding parent translation (#4742)
See #4737
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/i18n/I18nData.php | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/cli/i18n/I18nData.php b/cli/i18n/I18nData.php index 926df9110..e86c6d51d 100644 --- a/cli/i18n/I18nData.php +++ b/cli/i18n/I18nData.php @@ -25,7 +25,7 @@ class I18nData { foreach ($reference as $file => $refValues) { foreach ($refValues as $key => $refValue) { foreach ($languages as $language) { - if (!array_key_exists($key, $this->data[$language][$file])) { + if (!array_key_exists($file, $this->data[$language]) || !array_key_exists($key, $this->data[$language][$file])) { $this->data[$language][$file][$key] = clone $refValue; } $value = $this->data[$language][$file][$key]; @@ -88,7 +88,7 @@ class I18nData { * * @return array */ - public function getNonReferenceLanguages() { + private function getNonReferenceLanguages() { return array_filter(array_keys($this->data), function ($value) { return static::REFERENCE_LANGUAGE !== $value; }); @@ -178,6 +178,27 @@ class I18nData { } /** + * Check if a key is a parent key. + * To be a parent key, there must be at least one key starting with the key + * under test. Of course, it cannot be itself. + */ + private function isParent($key) { + if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE])) { + return false; + } + + $keys = array_keys($this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)]); + $children = array_values(array_filter($keys, function ($element) use ($key) { + if ($element === $key) { + return false; + } + return false !== strpos($element, $key); + })); + + return count($children) !== 0; + } + + /** * Add a new key to all languages. * * @param string $key @@ -185,6 +206,10 @@ class I18nData { * @throws Exception */ public function addKey($key, $value) { + if ($this->isParent($key)) { + $key = $this->getEmptySibling($key); + } + if ($this->isKnown($key)) { throw new Exception('The selected key already exist.'); } @@ -234,7 +259,8 @@ class I18nData { if (static::REFERENCE_LANGUAGE === $language) { $previousValue = $this->data[static::REFERENCE_LANGUAGE][$this->getFilenamePrefix($key)][$key]; foreach ($this->getAvailableLanguages() as $lang) { - if ($this->data[$lang][$this->getFilenamePrefix($key)][$key] === $previousValue) { + $currentValue = $this->data[$lang][$this->getFilenamePrefix($key)][$key]; + if ($currentValue->equal($previousValue)) { $this->data[$lang][$this->getFilenamePrefix($key)][$key] = $value; } } |
