translation-status.php 8.7 KB

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