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 | |
| 22 | # show the translation status of all locales and all their missing translations |
| 23 | $ php translation-status.php -v |
| 24 | |
| 25 | # show the status of a single locale |
| 26 | $ php translation-status.php fr |
| 27 | |
| 28 | # show the status of a single locale and all its missing translations |
| 29 | $ php translation-status.php fr -v |
| 30 | |
| 31 | END; |
| 32 | |
| 33 | $config = [ |
| 34 | // if TRUE, the full list of missing translations is displayed |
| 35 | 'verbose_output' => false, |
| 36 | // NULL = analyze all locales |
| 37 | 'locale_to_analyze' => null, |
| 38 | // the reference files all the other translations are compared to |
| 39 | 'original_files' => [ |
| 40 | 'src/Symfony/Component/Form/Resources/translations/validators.en.xlf', |
| 41 | 'src/Symfony/Component/Security/Core/Resources/translations/security.en.xlf', |
| 42 | 'src/Symfony/Component/Validator/Resources/translations/validators.en.xlf', |
| 43 | ], |
| 44 | ]; |
| 45 | |
| 46 | $argc = $_SERVER['argc']; |
| 47 | $argv = $_SERVER['argv']; |
| 48 | |
| 49 | if ($argc > 3) { |
| 50 | echo str_replace('translation-status.php', $argv[0], $usageInstructions); |
| 51 | exit(1); |
| 52 | } |
| 53 | |
| 54 | foreach (array_slice($argv, 1) as $argumentOrOption) { |
| 55 | if (str_starts_with($argumentOrOption, '-')) { |
| 56 | $config['verbose_output'] = true; |
| 57 | } else { |
| 58 | $config['locale_to_analyze'] = $argumentOrOption; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | foreach ($config['original_files'] as $originalFilePath) { |
| 63 | if (!file_exists($originalFilePath)) { |
| 64 | 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); |
| 65 | exit(1); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | $totalMissingTranslations = 0; |
| 70 | |
| 71 | foreach ($config['original_files'] as $originalFilePath) { |
| 72 | $translationFilePaths = findTranslationFiles($originalFilePath, $config['locale_to_analyze']); |
| 73 | $translationStatus = calculateTranslationStatus($originalFilePath, $translationFilePaths); |
| 74 | |
| 75 | $totalMissingTranslations += array_sum(array_map(function ($translation) { |
| 76 | return count($translation['missingKeys']); |
| 77 | }, array_values($translationStatus))); |
| 78 | |
| 79 | printTranslationStatus($originalFilePath, $translationStatus, $config['verbose_output']); |
| 80 | } |
| 81 | |
| 82 | exit($totalMissingTranslations > 0 ? 1 : 0); |
| 83 | |
| 84 | function findTranslationFiles($originalFilePath, $localeToAnalyze) |
| 85 | { |
| 86 | $translations = []; |
| 87 | |
| 88 | $translationsDir = dirname($originalFilePath); |
| 89 | $originalFileName = basename($originalFilePath); |
| 90 | $translationFileNamePattern = str_replace('.en.', '.*.', $originalFileName); |
| 91 | |
| 92 | $translationFiles = glob($translationsDir.'/'.$translationFileNamePattern, \GLOB_NOSORT); |
| 93 | sort($translationFiles); |
| 94 | foreach ($translationFiles as $filePath) { |
| 95 | $locale = extractLocaleFromFilePath($filePath); |
| 96 | |
| 97 | if (null !== $localeToAnalyze && $locale !== $localeToAnalyze) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | $translations[$locale] = $filePath; |
| 102 | } |
| 103 | |
| 104 | return $translations; |
| 105 | } |
| 106 | |
| 107 | function calculateTranslationStatus($originalFilePath, $translationFilePaths) |
| 108 | { |
| 109 | $translationStatus = []; |
| 110 | $allTranslationKeys = extractTranslationKeys($originalFilePath); |
| 111 | |
| 112 | foreach ($translationFilePaths as $locale => $translationPath) { |
| 113 | $translatedKeys = extractTranslationKeys($translationPath); |
| 114 | $missingKeys = array_diff_key($allTranslationKeys, $translatedKeys); |
| 115 | |
| 116 | $translationStatus[$locale] = [ |
| 117 | 'total' => count($allTranslationKeys), |
| 118 | 'translated' => count($translatedKeys), |
| 119 | 'missingKeys' => $missingKeys, |
| 120 | ]; |
| 121 | } |
| 122 | |
| 123 | return $translationStatus; |
| 124 | } |
| 125 | |
| 126 | function printTranslationStatus($originalFilePath, $translationStatus, $verboseOutput) |
| 127 | { |
| 128 | printTitle($originalFilePath); |
| 129 | printTable($translationStatus, $verboseOutput); |
| 130 | echo \PHP_EOL.\PHP_EOL; |
| 131 | } |
| 132 | |
| 133 | function extractLocaleFromFilePath($filePath) |
| 134 | { |
| 135 | $parts = explode('.', $filePath); |
| 136 | |
| 137 | return $parts[count($parts) - 2]; |
| 138 | } |
| 139 | |
| 140 | function extractTranslationKeys($filePath) |
| 141 | { |
| 142 | $translationKeys = []; |
| 143 | $contents = new \SimpleXMLElement(file_get_contents($filePath)); |
| 144 | |
| 145 | foreach ($contents->file->body->{'trans-unit'} as $translationKey) { |
| 146 | $translationId = (string) $translationKey['id']; |
| 147 | $translationKey = (string) $translationKey->source; |
| 148 | |
| 149 | $translationKeys[$translationId] = $translationKey; |
| 150 | } |
| 151 | |
| 152 | return $translationKeys; |
| 153 | } |
| 154 | |
| 155 | function printTitle($title) |
| 156 | { |
| 157 | echo $title.\PHP_EOL; |
| 158 | echo str_repeat('=', strlen($title)).\PHP_EOL.\PHP_EOL; |
| 159 | } |
| 160 | |
| 161 | function printTable($translations, $verboseOutput) |
| 162 | { |
| 163 | if (0 === count($translations)) { |
| 164 | echo 'No translations found'; |
| 165 | |
| 166 | return; |
| 167 | } |
| 168 | $longestLocaleNameLength = max(array_map('strlen', array_keys($translations))); |
| 169 | |
| 170 | foreach ($translations as $locale => $translation) { |
| 171 | if ($translation['translated'] > $translation['total']) { |
| 172 | textColorRed(); |
| 173 | } elseif ($translation['translated'] === $translation['total']) { |
| 174 | textColorGreen(); |
| 175 | } |
| 176 | |
| 177 | echo sprintf('| Locale: %-'.$longestLocaleNameLength.'s | Translated: %d/%d', $locale, $translation['translated'], $translation['total']).\PHP_EOL; |
| 178 | |
| 179 | textColorNormal(); |
| 180 | |
| 181 | if (true === $verboseOutput && count($translation['missingKeys']) > 0) { |
| 182 | echo str_repeat('-', 80).\PHP_EOL; |
| 183 | echo '| Missing Translations:'.\PHP_EOL; |
| 184 | |
| 185 | foreach ($translation['missingKeys'] as $id => $content) { |
| 186 | echo sprintf('| (id=%s) %s', $id, $content).\PHP_EOL; |
| 187 | } |
| 188 | |
| 189 | echo str_repeat('-', 80).\PHP_EOL; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | function textColorGreen() |
| 195 | { |
| 196 | echo "\033[32m"; |
| 197 | } |
| 198 | |
| 199 | function textColorRed() |
| 200 | { |
| 201 | echo "\033[31m"; |
| 202 | } |
| 203 | |
| 204 | function textColorNormal() |
| 205 | { |
| 206 | echo "\033[0m"; |
| 207 | } |