translation-status.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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);
  71. foreach ($translationFiles as $filePath) {
  72. $locale = extractLocaleFromFilePath($filePath);
  73. if (null !== $localeToAnalyze && $locale !== $localeToAnalyze) {
  74. continue;
  75. }
  76. $translations[$locale] = $filePath;
  77. }
  78. return $translations;
  79. }
  80. function calculateTranslationStatus($originalFilePath, $translationFilePaths)
  81. {
  82. $translationStatus = [];
  83. $allTranslationKeys = extractTranslationKeys($originalFilePath);
  84. foreach ($translationFilePaths as $locale => $translationPath) {
  85. $translatedKeys = extractTranslationKeys($translationPath);
  86. $missingKeys = array_diff_key($allTranslationKeys, $translatedKeys);
  87. $translationStatus[$locale] = [
  88. 'total' => count($allTranslationKeys),
  89. 'translated' => count($translatedKeys),
  90. 'missingKeys' => $missingKeys,
  91. ];
  92. }
  93. return $translationStatus;
  94. }
  95. function printTranslationStatus($originalFilePath, $translationStatus, $verboseOutput)
  96. {
  97. printTitle($originalFilePath);
  98. printTable($translationStatus, $verboseOutput);
  99. echo PHP_EOL.PHP_EOL;
  100. }
  101. function extractLocaleFromFilePath($filePath)
  102. {
  103. $parts = explode('.', $filePath);
  104. return $parts[count($parts) - 2];
  105. }
  106. function extractTranslationKeys($filePath)
  107. {
  108. $translationKeys = [];
  109. $contents = new \SimpleXMLElement(file_get_contents($filePath));
  110. foreach ($contents->file->body->{'trans-unit'} as $translationKey) {
  111. $translationId = (string) $translationKey['id'];
  112. $translationKey = (string) $translationKey->source;
  113. $translationKeys[$translationId] = $translationKey;
  114. }
  115. return $translationKeys;
  116. }
  117. function printTitle($title)
  118. {
  119. echo $title.PHP_EOL;
  120. echo str_repeat('=', strlen($title)).PHP_EOL.PHP_EOL;
  121. }
  122. function printTable($translations, $verboseOutput)
  123. {
  124. if (0 === count($translations)) {
  125. echo 'No translations found';
  126. return;
  127. }
  128. $longestLocaleNameLength = max(array_map('strlen', array_keys($translations)));
  129. foreach ($translations as $locale => $translation) {
  130. $isTranslationCompleted = $translation['translated'] === $translation['total'];
  131. if ($isTranslationCompleted) {
  132. textColorGreen();
  133. }
  134. echo sprintf('| Locale: %-'.$longestLocaleNameLength.'s | Translated: %d/%d', $locale, $translation['translated'], $translation['total']).PHP_EOL;
  135. textColorNormal();
  136. if (true === $verboseOutput && count($translation['missingKeys']) > 0) {
  137. echo str_repeat('-', 80).PHP_EOL;
  138. echo '| Missing Translations:'.PHP_EOL;
  139. foreach ($translation['missingKeys'] as $id => $content) {
  140. echo sprintf('| (id=%s) %s', $id, $content).PHP_EOL;
  141. }
  142. echo str_repeat('-', 80).PHP_EOL;
  143. }
  144. }
  145. }
  146. function textColorGreen()
  147. {
  148. echo "\033[32m";
  149. }
  150. function textColorNormal()
  151. {
  152. echo "\033[0m";
  153. }