Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | $usageInstructions = <<<END |
| 13 | |
| 14 | Usage instructions |
| 15 | ------------------------------------------------------------------------------- |
| 16 | |
| 17 | $ cd symfony-code-root-directory/ |
| 18 | |
| 19 | # show the translation status of all locales |
| 20 | $ php translation-status.php |
| 21 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 22 | # only show the translation status of incomplete or erroneous locales |
| 23 | $ php translation-status.php --incomplete |
| 24 | |
| 25 | # show the translation status of all locales, all their missing translations and mismatches between trans-unit id and source |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 26 | $ php translation-status.php -v |
| 27 | |
| 28 | # show the status of a single locale |
| 29 | $ php translation-status.php fr |
| 30 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 31 | # show the status of a single locale, missing translations and mismatches between trans-unit id and source |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 32 | $ php translation-status.php fr -v |
| 33 | |
| 34 | END; |
| 35 | |
| 36 | $config = [ |
| 37 | // if TRUE, the full list of missing translations is displayed |
| 38 | 'verbose_output' => false, |
| 39 | // NULL = analyze all locales |
| 40 | 'locale_to_analyze' => null, |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 41 | // append --incomplete to only show incomplete languages |
| 42 | 'include_completed_languages' => true, |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 43 | // the reference files all the other translations are compared to |
| 44 | 'original_files' => [ |
| 45 | 'src/Symfony/Component/Form/Resources/translations/validators.en.xlf', |
| 46 | 'src/Symfony/Component/Security/Core/Resources/translations/security.en.xlf', |
| 47 | 'src/Symfony/Component/Validator/Resources/translations/validators.en.xlf', |
| 48 | ], |
| 49 | ]; |
| 50 | |
| 51 | $argc = $_SERVER['argc']; |
| 52 | $argv = $_SERVER['argv']; |
| 53 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 54 | if ($argc > 4) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 55 | echo str_replace('translation-status.php', $argv[0], $usageInstructions); |
| 56 | exit(1); |
| 57 | } |
| 58 | |
| 59 | foreach (array_slice($argv, 1) as $argumentOrOption) { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 60 | if ('--incomplete' === $argumentOrOption) { |
| 61 | $config['include_completed_languages'] = false; |
| 62 | continue; |
| 63 | } |
| 64 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 65 | if (str_starts_with($argumentOrOption, '-')) { |
| 66 | $config['verbose_output'] = true; |
| 67 | } else { |
| 68 | $config['locale_to_analyze'] = $argumentOrOption; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | foreach ($config['original_files'] as $originalFilePath) { |
| 73 | if (!file_exists($originalFilePath)) { |
| 74 | echo sprintf('The following file does not exist. Make sure that you execute this command at the root dir of the Symfony code repository.%s %s', \PHP_EOL, $originalFilePath); |
| 75 | exit(1); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $totalMissingTranslations = 0; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 80 | $totalTranslationMismatches = 0; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 81 | |
| 82 | foreach ($config['original_files'] as $originalFilePath) { |
| 83 | $translationFilePaths = findTranslationFiles($originalFilePath, $config['locale_to_analyze']); |
| 84 | $translationStatus = calculateTranslationStatus($originalFilePath, $translationFilePaths); |
| 85 | |
| 86 | $totalMissingTranslations += array_sum(array_map(function ($translation) { |
| 87 | return count($translation['missingKeys']); |
| 88 | }, array_values($translationStatus))); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 89 | $totalTranslationMismatches += array_sum(array_map(function ($translation) { |
| 90 | return count($translation['mismatches']); |
| 91 | }, array_values($translationStatus))); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 92 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 93 | printTranslationStatus($originalFilePath, $translationStatus, $config['verbose_output'], $config['include_completed_languages']); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 96 | exit($totalTranslationMismatches > 0 ? 1 : 0); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 97 | |
| 98 | function findTranslationFiles($originalFilePath, $localeToAnalyze) |
| 99 | { |
| 100 | $translations = []; |
| 101 | |
| 102 | $translationsDir = dirname($originalFilePath); |
| 103 | $originalFileName = basename($originalFilePath); |
| 104 | $translationFileNamePattern = str_replace('.en.', '.*.', $originalFileName); |
| 105 | |
| 106 | $translationFiles = glob($translationsDir.'/'.$translationFileNamePattern, \GLOB_NOSORT); |
| 107 | sort($translationFiles); |
| 108 | foreach ($translationFiles as $filePath) { |
| 109 | $locale = extractLocaleFromFilePath($filePath); |
| 110 | |
| 111 | if (null !== $localeToAnalyze && $locale !== $localeToAnalyze) { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | $translations[$locale] = $filePath; |
| 116 | } |
| 117 | |
| 118 | return $translations; |
| 119 | } |
| 120 | |
| 121 | function calculateTranslationStatus($originalFilePath, $translationFilePaths) |
| 122 | { |
| 123 | $translationStatus = []; |
| 124 | $allTranslationKeys = extractTranslationKeys($originalFilePath); |
| 125 | |
| 126 | foreach ($translationFilePaths as $locale => $translationPath) { |
| 127 | $translatedKeys = extractTranslationKeys($translationPath); |
| 128 | $missingKeys = array_diff_key($allTranslationKeys, $translatedKeys); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 129 | $mismatches = findTransUnitMismatches($allTranslationKeys, $translatedKeys); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 130 | |
| 131 | $translationStatus[$locale] = [ |
| 132 | 'total' => count($allTranslationKeys), |
| 133 | 'translated' => count($translatedKeys), |
| 134 | 'missingKeys' => $missingKeys, |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 135 | 'mismatches' => $mismatches, |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 136 | ]; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 137 | $translationStatus[$locale]['is_completed'] = isTranslationCompleted($translationStatus[$locale]); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return $translationStatus; |
| 141 | } |
| 142 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 143 | function isTranslationCompleted(array $translationStatus): bool |
| 144 | { |
| 145 | return $translationStatus['total'] === $translationStatus['translated'] && 0 === count($translationStatus['mismatches']); |
| 146 | } |
| 147 | |
| 148 | function printTranslationStatus($originalFilePath, $translationStatus, $verboseOutput, $includeCompletedLanguages) |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 149 | { |
| 150 | printTitle($originalFilePath); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 151 | printTable($translationStatus, $verboseOutput, $includeCompletedLanguages); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 152 | echo \PHP_EOL.\PHP_EOL; |
| 153 | } |
| 154 | |
| 155 | function extractLocaleFromFilePath($filePath) |
| 156 | { |
| 157 | $parts = explode('.', $filePath); |
| 158 | |
| 159 | return $parts[count($parts) - 2]; |
| 160 | } |
| 161 | |
| 162 | function extractTranslationKeys($filePath) |
| 163 | { |
| 164 | $translationKeys = []; |
| 165 | $contents = new \SimpleXMLElement(file_get_contents($filePath)); |
| 166 | |
| 167 | foreach ($contents->file->body->{'trans-unit'} as $translationKey) { |
| 168 | $translationId = (string) $translationKey['id']; |
| 169 | $translationKey = (string) $translationKey->source; |
| 170 | |
| 171 | $translationKeys[$translationId] = $translationKey; |
| 172 | } |
| 173 | |
| 174 | return $translationKeys; |
| 175 | } |
| 176 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 177 | /** |
| 178 | * Check whether the trans-unit id and source match with the base translation. |
| 179 | */ |
| 180 | function findTransUnitMismatches(array $baseTranslationKeys, array $translatedKeys): array |
| 181 | { |
| 182 | $mismatches = []; |
| 183 | |
| 184 | foreach ($baseTranslationKeys as $translationId => $translationKey) { |
| 185 | if (!isset($translatedKeys[$translationId])) { |
| 186 | continue; |
| 187 | } |
| 188 | if ($translatedKeys[$translationId] !== $translationKey) { |
| 189 | $mismatches[$translationId] = [ |
| 190 | 'found' => $translatedKeys[$translationId], |
| 191 | 'expected' => $translationKey, |
| 192 | ]; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return $mismatches; |
| 197 | } |
| 198 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 199 | function printTitle($title) |
| 200 | { |
| 201 | echo $title.\PHP_EOL; |
| 202 | echo str_repeat('=', strlen($title)).\PHP_EOL.\PHP_EOL; |
| 203 | } |
| 204 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 205 | function printTable($translations, $verboseOutput, bool $includeCompletedLanguages) |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 206 | { |
| 207 | if (0 === count($translations)) { |
| 208 | echo 'No translations found'; |
| 209 | |
| 210 | return; |
| 211 | } |
| 212 | $longestLocaleNameLength = max(array_map('strlen', array_keys($translations))); |
| 213 | |
| 214 | foreach ($translations as $locale => $translation) { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 215 | if (!$includeCompletedLanguages && $translation['is_completed']) { |
| 216 | continue; |
| 217 | } |
| 218 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 219 | if ($translation['translated'] > $translation['total']) { |
| 220 | textColorRed(); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 221 | } elseif (count($translation['mismatches']) > 0) { |
| 222 | textColorRed(); |
| 223 | } elseif ($translation['is_completed']) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 224 | textColorGreen(); |
| 225 | } |
| 226 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 227 | echo sprintf( |
| 228 | '| Locale: %-'.$longestLocaleNameLength.'s | Translated: %2d/%2d | Mismatches: %d |', |
| 229 | $locale, |
| 230 | $translation['translated'], |
| 231 | $translation['total'], |
| 232 | count($translation['mismatches']) |
| 233 | ).\PHP_EOL; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 234 | |
| 235 | textColorNormal(); |
| 236 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 237 | $shouldBeClosed = false; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 238 | if (true === $verboseOutput && count($translation['missingKeys']) > 0) { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 239 | echo '| Missing Translations:'.\PHP_EOL; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 240 | |
| 241 | foreach ($translation['missingKeys'] as $id => $content) { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 242 | echo sprintf('| (id=%s) %s', $id, $content).\PHP_EOL; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 243 | } |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 244 | $shouldBeClosed = true; |
| 245 | } |
| 246 | if (true === $verboseOutput && count($translation['mismatches']) > 0) { |
| 247 | echo '| Mismatches between trans-unit id and source:'.\PHP_EOL; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 248 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 249 | foreach ($translation['mismatches'] as $id => $content) { |
| 250 | echo sprintf('| (id=%s) Expected: %s', $id, $content['expected']).\PHP_EOL; |
| 251 | echo sprintf('| Found: %s', $content['found']).\PHP_EOL; |
| 252 | } |
| 253 | $shouldBeClosed = true; |
| 254 | } |
| 255 | if ($shouldBeClosed) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 256 | echo str_repeat('-', 80).\PHP_EOL; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | function textColorGreen() |
| 262 | { |
| 263 | echo "\033[32m"; |
| 264 | } |
| 265 | |
| 266 | function textColorRed() |
| 267 | { |
| 268 | echo "\033[31m"; |
| 269 | } |
| 270 | |
| 271 | function textColorNormal() |
| 272 | { |
| 273 | echo "\033[0m"; |
| 274 | } |