From 127b7f0a3aad7012055c058e8aba0d27192a8cbc Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Sat, 8 Jan 2022 08:00:26 -0500 Subject: Change i18n process (#4131) Before, the ignore info were stored in a different file which was a bit cumbersome for new comers. Now, this info is stored directly in the translation file as a comment. Before, there was no way of telling translators that a previously translated string was in need of a new translation. Now, the dirty information is there to convey that info. --- cli/i18n/I18nFile.php | 70 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 11 deletions(-) (limited to 'cli/i18n/I18nFile.php') diff --git a/cli/i18n/I18nFile.php b/cli/i18n/I18nFile.php index 222d07692..50b2d5023 100644 --- a/cli/i18n/I18nFile.php +++ b/cli/i18n/I18nFile.php @@ -1,8 +1,8 @@ isFile()) { continue; } - $i18n[$dir->getFilename()][$file->getFilename()] = $this->flatten(include $file->getPathname(), $file->getBasename('.php')); + + $i18n[$dir->getFilename()][$file->getFilename()] = $this->flatten($this->process($file->getPathname()), $file->getBasename('.php')); } } @@ -42,6 +43,35 @@ class I18nFile implements I18nFileInterface{ } } + /** + * Process the content of an i18n file + * + * @param string $filename + * @return array + */ + private function process(string $filename) { + $content = file_get_contents($filename); + $content = str_replace(' todo\',', + ' -> dirty\',', + ' -> ignore\',', + ], $content); + + $content = eval($content); + + if (is_array($content)) { + return $content; + } + + return []; + } + /** * Flatten an array of translation * @@ -49,7 +79,7 @@ class I18nFile implements I18nFileInterface{ * @param string $prefix * @return array */ - private function flatten($translation, $prefix = '') { + private function flatten(array $translation, string $prefix = '') { $a = array(); if ('' !== $prefix) { @@ -60,7 +90,7 @@ class I18nFile implements I18nFileInterface{ if (is_array($value)) { $a += $this->flatten($value, $prefix . $key); } else { - $a[$prefix . $key] = $value; + $a[$prefix . $key] = new I18nValue($value); } } @@ -76,7 +106,7 @@ class I18nFile implements I18nFileInterface{ * @param array $translation * @return array */ - private function unflatten($translation) { + private function unflatten(array $translation) { $a = array(); ksort($translation, SORT_NATURAL); @@ -99,25 +129,43 @@ class I18nFile implements I18nFileInterface{ * @param array $translation * @return string */ - private function format($translation) { + private function format(array $translation) { $translation = var_export($this->unflatten($translation), true); $patterns = array( + '/ -> todo\',/', + '/ -> dirty\',/', + '/ -> ignore\',/', '/array \(/', '/=>\s*array/', '/(\w) {2}/', '/ {2}/', - '/ -> todo\',/', ); $replacements = array( + "',\t// TODO", // Double quoting is mandatory to have a tab instead of the \t string + "',\t// DIRTY", // Double quoting is mandatory to have a tab instead of the \t string + "',\t// IGNORE", // Double quoting is mandatory to have a tab instead of the \t string 'array(', '=> array', '$1 ', "\t", // Double quoting is mandatory to have a tab instead of the \t string - "',\t// TODO - Translation", // 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("