translation-status.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 (0 === strpos($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(function ($translation) {
  71. return count($translation['missingKeys']);
  72. }, array_values($translationStatus)));
  73. $totalTranslationMismatches += array_sum(array_map(function ($translation) {
  74. return count($translation['mismatches']);
  75. }, array_values($translationStatus)));
  76. printTranslationStatus($originalFilePath, $translationStatus, $config['verbose_output'], $config['include_completed_languages']);
  77. }
  78. exit($totalTranslationMismatches > 0 ? 1 : 0);
  79. function findTranslationFiles($originalFilePath, $localeToAnalyze)
  80. {
  81. $translations = [];
  82. $translationsDir = dirname($originalFilePath);
  83. $originalFileName = basename($originalFilePath);
  84. $translationFileNamePattern = str_replace('.en.', '.*.', $originalFileName);
  85. $translationFiles = glob($translationsDir.'/'.$translationFileNamePattern, \GLOB_NOSORT);
  86. sort($translationFiles);
  87. foreach ($translationFiles as $filePath) {
  88. $locale = extractLocaleFromFilePath($filePath);
  89. if (null !== $localeToAnalyze && $locale !== $localeToAnalyze) {
  90. continue;
  91. }
  92. $translations[$locale] = $filePath;
  93. }
  94. return $translations;
  95. }
  96. function calculateTranslationStatus($originalFilePath, $translationFilePaths)
  97. {
  98. $translationStatus = [];
  99. $allTranslationKeys = extractTranslationKeys($originalFilePath);
  100. foreach ($translationFilePaths as $locale => $translationPath) {
  101. $translatedKeys = extractTranslationKeys($translationPath);
  102. $missingKeys = array_diff_key($allTranslationKeys, $translatedKeys);
  103. $mismatches = findTransUnitMismatches($allTranslationKeys, $translatedKeys);
  104. $translationStatus[$locale] = [
  105. 'total' => count($allTranslationKeys),
  106. 'translated' => count($translatedKeys),
  107. 'missingKeys' => $missingKeys,
  108. 'mismatches' => $mismatches,
  109. ];
  110. $translationStatus[$locale]['is_completed'] = isTranslationCompleted($translationStatus[$locale]);
  111. }
  112. return $translationStatus;
  113. }
  114. function isTranslationCompleted(array $translationStatus): bool
  115. {
  116. return $translationStatus['total'] === $translationStatus['translated'] && 0 === count($translationStatus['mismatches']);
  117. }
  118. function printTranslationStatus($originalFilePath, $translationStatus, $verboseOutput, $includeCompletedLanguages)
  119. {
  120. printTitle($originalFilePath);
  121. printTable($translationStatus, $verboseOutput, $includeCompletedLanguages);
  122. echo \PHP_EOL.\PHP_EOL;
  123. }
  124. function extractLocaleFromFilePath($filePath)
  125. {
  126. $parts = explode('.', $filePath);
  127. return $parts[count($parts) - 2];
  128. }
  129. function extractTranslationKeys($filePath)
  130. {
  131. $translationKeys = [];
  132. $contents = new \SimpleXMLElement(file_get_contents($filePath));
  133. foreach ($contents->file->body->{'trans-unit'} as $translationKey) {
  134. $translationId = (string) $translationKey['id'];
  135. $translationKey = (string) $translationKey->source;
  136. $translationKeys[$translationId] = $translationKey;
  137. }
  138. return $translationKeys;
  139. }
  140. /**
  141. * Check whether the trans-unit id and source match with the base translation.
  142. */
  143. function findTransUnitMismatches(array $baseTranslationKeys, array $translatedKeys): array
  144. {
  145. $mismatches = [];
  146. foreach ($baseTranslationKeys as $translationId => $translationKey) {
  147. if (!isset($translatedKeys[$translationId])) {
  148. continue;
  149. }
  150. if ($translatedKeys[$translationId] !== $translationKey) {
  151. $mismatches[$translationId] = [
  152. 'found' => $translatedKeys[$translationId],
  153. 'expected' => $translationKey,
  154. ];
  155. }
  156. }
  157. return $mismatches;
  158. }
  159. function printTitle($title)
  160. {
  161. echo $title.\PHP_EOL;
  162. echo str_repeat('=', strlen($title)).\PHP_EOL.\PHP_EOL;
  163. }
  164. function printTable($translations, $verboseOutput, bool $includeCompletedLanguages)
  165. {
  166. if (0 === count($translations)) {
  167. echo 'No translations found';
  168. return;
  169. }
  170. $longestLocaleNameLength = max(array_map('strlen', array_keys($translations)));
  171. foreach ($translations as $locale => $translation) {
  172. if (!$includeCompletedLanguages && $translation['is_completed']) {
  173. continue;
  174. }
  175. if ($translation['translated'] > $translation['total']) {
  176. textColorRed();
  177. } elseif (count($translation['mismatches']) > 0) {
  178. textColorRed();
  179. } elseif ($translation['is_completed']) {
  180. textColorGreen();
  181. }
  182. echo sprintf(
  183. '| Locale: %-'.$longestLocaleNameLength.'s | Translated: %2d/%2d | Mismatches: %d |',
  184. $locale,
  185. $translation['translated'],
  186. $translation['total'],
  187. count($translation['mismatches'])
  188. ).\PHP_EOL;
  189. textColorNormal();
  190. $shouldBeClosed = false;
  191. if (true === $verboseOutput && count($translation['missingKeys']) > 0) {
  192. echo '| Missing Translations:'.\PHP_EOL;
  193. foreach ($translation['missingKeys'] as $id => $content) {
  194. echo sprintf('| (id=%s) %s', $id, $content).\PHP_EOL;
  195. }
  196. $shouldBeClosed = true;
  197. }
  198. if (true === $verboseOutput && count($translation['mismatches']) > 0) {
  199. echo '| Mismatches between trans-unit id and source:'.\PHP_EOL;
  200. foreach ($translation['mismatches'] as $id => $content) {
  201. echo sprintf('| (id=%s) Expected: %s', $id, $content['expected']).\PHP_EOL;
  202. echo sprintf('| Found: %s', $content['found']).\PHP_EOL;
  203. }
  204. $shouldBeClosed = true;
  205. }
  206. if ($shouldBeClosed) {
  207. echo str_repeat('-', 80).\PHP_EOL;
  208. }
  209. }
  210. }
  211. function textColorGreen()
  212. {
  213. echo "\033[32m";
  214. }
  215. function textColorRed()
  216. {
  217. echo "\033[31m";
  218. }
  219. function textColorNormal()
  220. {
  221. echo "\033[0m";
  222. }