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