translation-status.php 5.9 KB

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