|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Commands\Translation; |
| 13 | + |
| 14 | +use CodeIgniter\CLI\BaseCommand; |
| 15 | +use CodeIgniter\CLI\CLI; |
| 16 | +use Config\App; |
| 17 | +use Locale; |
| 18 | +use RecursiveDirectoryIterator; |
| 19 | +use RecursiveIteratorIterator; |
| 20 | +use SplFileInfo; |
| 21 | + |
| 22 | +class LocalizationFinder extends BaseCommand |
| 23 | +{ |
| 24 | + protected $group = 'Translation'; |
| 25 | + protected $name = 'lang:find'; |
| 26 | + protected $description = 'Find and save available phrases to translate'; |
| 27 | + protected $usage = 'lang:find [options]'; |
| 28 | + protected $arguments = []; |
| 29 | + protected $options = [ |
| 30 | + '--locale' => 'Apply changes to the selected locale (en, ru, etc...).', |
| 31 | + '--dir' => 'Directory for searching for translations (in relation to the APPPATH).', |
| 32 | + '--show-new' => 'See only new translations as table.', |
| 33 | + '--verbose' => 'Output detailed information.', |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * Flag for output detailed information |
| 38 | + */ |
| 39 | + private bool $verbose = false; |
| 40 | + |
| 41 | + private string $languagePath; |
| 42 | + |
| 43 | + public function run(array $params) |
| 44 | + { |
| 45 | + $this->verbose = array_key_exists('verbose', $params); |
| 46 | + $cliOptionShowNew = array_key_exists('show-new', $params); |
| 47 | + $cliOptionLocale = ! empty($params['locale']) ? $params['locale'] : null; |
| 48 | + $cliOptionDir = ! empty($params['dir']) ? $params['dir'] : null; |
| 49 | + $currentLocale = Locale::getDefault(); |
| 50 | + $currentDir = APPPATH; |
| 51 | + $this->languagePath = $currentDir . 'Language'; |
| 52 | + $tableRows = []; |
| 53 | + $languageFoundKeys = []; |
| 54 | + $countFiles = 0; |
| 55 | + $countNewKeys = 0; |
| 56 | + |
| 57 | + if (ENVIRONMENT === 'testing') { |
| 58 | + $currentDir = SUPPORTPATH; |
| 59 | + $this->languagePath = $currentDir . 'Language/'; |
| 60 | + } |
| 61 | + |
| 62 | + if (is_string($cliOptionLocale)) { |
| 63 | + if (! in_array($cliOptionLocale, config(App::class)->supportedLocales, true)) { |
| 64 | + CLI::error('Error: Supported locales ' . implode(', ', config(App::class)->supportedLocales)); |
| 65 | + |
| 66 | + return -1; |
| 67 | + } |
| 68 | + |
| 69 | + $currentLocale = $cliOptionLocale; |
| 70 | + } |
| 71 | + |
| 72 | + if (is_string($cliOptionDir)) { |
| 73 | + $tempCurrentDir = realpath($currentDir . $cliOptionDir); |
| 74 | + |
| 75 | + if (false === $tempCurrentDir) { |
| 76 | + CLI::error('Error: Dir must be located in ' . $currentDir); |
| 77 | + |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + if (str_starts_with($tempCurrentDir, $this->languagePath)) { |
| 82 | + CLI::error('Error: Dir ' . $this->languagePath . ' restricted to scan.'); |
| 83 | + |
| 84 | + return -1; |
| 85 | + } |
| 86 | + |
| 87 | + $currentDir = $tempCurrentDir; |
| 88 | + } |
| 89 | + |
| 90 | + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($currentDir)); |
| 91 | + $this->writeIsVerbose('Current locale: ' . $currentLocale); |
| 92 | + $this->writeIsVerbose('Find phrases in ' . $currentDir . ' folder...'); |
| 93 | + |
| 94 | + /** |
| 95 | + * @var SplFileInfo $file |
| 96 | + */ |
| 97 | + foreach ($iterator as $file) { |
| 98 | + if ($this->isIgnoredFile($file)) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + $this->writeIsVerbose('File found: ' . mb_substr($file->getRealPath(), mb_strlen(APPPATH))); |
| 103 | + $countFiles++; |
| 104 | + $languageFoundKeys = array_replace_recursive($this->findTranslationsInFile($file), $languageFoundKeys); |
| 105 | + } |
| 106 | + |
| 107 | + ksort($languageFoundKeys); |
| 108 | + $languageDiff = []; |
| 109 | + $languageFoundGroups = array_unique(array_keys($languageFoundKeys)); |
| 110 | + |
| 111 | + foreach ($languageFoundGroups as $langFileName) { |
| 112 | + $languageStoredKeys = []; |
| 113 | + $languageFilePath = $this->languagePath . DIRECTORY_SEPARATOR . $currentLocale . DIRECTORY_SEPARATOR . $langFileName . '.php'; |
| 114 | + |
| 115 | + if (is_file($languageFilePath)) { |
| 116 | + /** |
| 117 | + * Load old localization |
| 118 | + */ |
| 119 | + $languageStoredKeys = require $languageFilePath; |
| 120 | + } |
| 121 | + |
| 122 | + $languageDiff = $this->arrayDiffRecursive($languageFoundKeys[$langFileName], $languageStoredKeys); |
| 123 | + $countNewKeys += $this->arrayCountRecursive($languageDiff); |
| 124 | + |
| 125 | + if ($cliOptionShowNew) { |
| 126 | + $tableRows = array_merge($this->arrayToTableRows($langFileName, $languageDiff), $tableRows); |
| 127 | + } else { |
| 128 | + $newLanguageKeys = array_replace_recursive($languageFoundKeys[$langFileName], $languageStoredKeys); |
| 129 | + |
| 130 | + /** |
| 131 | + * New translates exists |
| 132 | + */ |
| 133 | + if ($languageDiff !== []) { |
| 134 | + if (false === file_put_contents($languageFilePath, $this->templateFile($newLanguageKeys))) { |
| 135 | + $this->writeIsVerbose('Lang file ' . $langFileName . ' (error write).', 'red'); |
| 136 | + } else { |
| 137 | + /** |
| 138 | + * FIXME: Need help command (slow run) |
| 139 | + */ |
| 140 | + exec('composer cs-fix --quiet ' . $this->languagePath); |
| 141 | + $this->writeIsVerbose('Lang file "' . $langFileName . '" successful updated!', 'green'); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if ($cliOptionShowNew && ! empty($tableRows)) { |
| 148 | + sort($tableRows); |
| 149 | + CLI::table($tableRows, ['File', 'Key']); |
| 150 | + } |
| 151 | + |
| 152 | + $this->writeIsVerbose('Files found: ' . $countFiles); |
| 153 | + $this->writeIsVerbose('New translates found: ' . $countNewKeys); |
| 154 | + CLI::write('All operations done!'); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @param SplFileInfo|string $file |
| 159 | + */ |
| 160 | + private function findTranslationsInFile($file): array |
| 161 | + { |
| 162 | + $languageFoundKeys = []; |
| 163 | + |
| 164 | + if (is_string($file) && is_file($file)) { |
| 165 | + $file = new SplFileInfo($file); |
| 166 | + } |
| 167 | + |
| 168 | + $fileContent = file_get_contents($file->getRealPath()); |
| 169 | + preg_match_all('/lang\(\'([._a-z0-9\-]+)\'\)/ui', $fileContent, $matches); |
| 170 | + |
| 171 | + if (empty($matches[1])) { |
| 172 | + return []; |
| 173 | + } |
| 174 | + |
| 175 | + foreach ($matches[1] as $phraseKey) { |
| 176 | + $phraseKeys = explode('.', $phraseKey); |
| 177 | + |
| 178 | + /** |
| 179 | + * Language key not have Filename or Lang key |
| 180 | + */ |
| 181 | + if (count($phraseKeys) < 2) { |
| 182 | + continue; |
| 183 | + } |
| 184 | + |
| 185 | + $languageFileName = array_shift($phraseKeys); |
| 186 | + $isEmptyNestedArray = (! empty($languageFileName) && empty($phraseKeys[0])) |
| 187 | + || (empty($languageFileName) && ! empty($phraseKeys[0])) |
| 188 | + || (empty($languageFileName) && empty($phraseKeys[0])); |
| 189 | + |
| 190 | + if ($isEmptyNestedArray) { |
| 191 | + continue; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Language key as string |
| 196 | + */ |
| 197 | + if (count($phraseKeys) === 1) { |
| 198 | + /** |
| 199 | + * Add found language keys to temporary array |
| 200 | + */ |
| 201 | + $languageFoundKeys[$languageFileName][$phraseKeys[0]] = $phraseKey; |
| 202 | + } else { |
| 203 | + $childKeys = $this->buildMultiArray($phraseKeys, $phraseKey); |
| 204 | + $languageFoundKeys[$languageFileName] = array_replace_recursive($languageFoundKeys[$languageFileName] ?? [], $childKeys); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return $languageFoundKeys; |
| 209 | + } |
| 210 | + |
| 211 | + private function isIgnoredFile(SplFileInfo $file): bool |
| 212 | + { |
| 213 | + if ($file->isDir() || str_contains($file->getRealPath(), $this->languagePath)) { |
| 214 | + return true; |
| 215 | + } |
| 216 | + |
| 217 | + return 'php' !== $file->getExtension(); |
| 218 | + } |
| 219 | + |
| 220 | + private function templateFile(array $language = []): string |
| 221 | + { |
| 222 | + if (! empty($language)) { |
| 223 | + $languageArrayString = $this->languageKeysDump($language); |
| 224 | + |
| 225 | + return <<<PHP |
| 226 | + <?php |
| 227 | +
|
| 228 | + return {$languageArrayString}; |
| 229 | + PHP; |
| 230 | + } |
| 231 | + |
| 232 | + return <<<'PHP' |
| 233 | + <?php |
| 234 | +
|
| 235 | + return []; |
| 236 | + PHP; |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Create multidimensional array from another keys |
| 241 | + */ |
| 242 | + private function buildMultiArray(array $fromKeys, string $lastArrayValue = ''): array |
| 243 | + { |
| 244 | + $newArray = []; |
| 245 | + $lastIndex = array_pop($fromKeys); |
| 246 | + $current = &$newArray; |
| 247 | + |
| 248 | + foreach ($fromKeys as $value) { |
| 249 | + $current[$value] = []; |
| 250 | + $current = &$current[$value]; |
| 251 | + } |
| 252 | + |
| 253 | + $current[$lastIndex] = $lastArrayValue; |
| 254 | + |
| 255 | + return $newArray; |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * Compare recursive two arrays and return new array (diference) |
| 260 | + */ |
| 261 | + private function arrayDiffRecursive(array $originalArray, array $compareArray): array |
| 262 | + { |
| 263 | + $difference = []; |
| 264 | + |
| 265 | + if (count($compareArray) < 1) { |
| 266 | + return $originalArray; |
| 267 | + } |
| 268 | + |
| 269 | + foreach ($originalArray as $originalKey => $originalValue) { |
| 270 | + if (is_array($originalValue)) { |
| 271 | + $diffArrays = null; |
| 272 | + |
| 273 | + if (isset($compareArray[$originalKey])) { |
| 274 | + $diffArrays = $this->arrayDiffRecursive($originalValue, $compareArray[$originalKey]); |
| 275 | + } else { |
| 276 | + $difference[$originalKey] = $originalValue; |
| 277 | + } |
| 278 | + |
| 279 | + if (! empty($diffArrays)) { |
| 280 | + $difference[$originalKey] = $diffArrays; |
| 281 | + } |
| 282 | + } elseif (is_string($originalValue) && ! array_key_exists($originalKey, $compareArray)) { |
| 283 | + $difference[$originalKey] = $originalValue; |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + return $difference; |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Convert multi arrays to specific CLI table rows (flat array) |
| 292 | + */ |
| 293 | + private function arrayToTableRows(string $langFileName, array $array): array |
| 294 | + { |
| 295 | + $rows = []; |
| 296 | + |
| 297 | + foreach ($array as $value) { |
| 298 | + if (is_array($value)) { |
| 299 | + $rows = array_merge($rows, $this->arrayToTableRows($langFileName, $value)); |
| 300 | + |
| 301 | + continue; |
| 302 | + } |
| 303 | + |
| 304 | + if (is_string($value)) { |
| 305 | + $rows[] = [$langFileName, $value]; |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + return $rows; |
| 310 | + } |
| 311 | + |
| 312 | + private function arrayCountRecursive(array $array, int $counter = 0): int |
| 313 | + { |
| 314 | + foreach ($array as $value) { |
| 315 | + if (! is_array($value)) { |
| 316 | + $counter++; |
| 317 | + } else { |
| 318 | + $counter = $this->arrayCountRecursive($value, $counter); |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + return $counter; |
| 323 | + } |
| 324 | + |
| 325 | + private function languageKeysDump(array $inputArray): string |
| 326 | + { |
| 327 | + return var_export($inputArray, true); |
| 328 | + } |
| 329 | + |
| 330 | + /** |
| 331 | + * Show details in the console if the flag is set |
| 332 | + */ |
| 333 | + private function writeIsVerbose(string $text = '', ?string $foreground = null, ?string $background = null): void |
| 334 | + { |
| 335 | + if ($this->verbose) { |
| 336 | + CLI::write($text, $foreground, $background); |
| 337 | + } |
| 338 | + } |
| 339 | +} |
0 commit comments