blob: a769164273a6acd56a5dd8630434066e743aae82 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12$usageInstructions = <<<END
13
14 Usage instructions
15 -------------------------------------------------------------------------------
16
17 $ cd symfony-code-root-directory/
18
19 # show the translation status of all locales
20 $ php translation-status.php
21
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010022 # only show the translation status of incomplete or erroneous locales
23 $ php translation-status.php --incomplete
24
25 # show the translation status of all locales, all their missing translations and mismatches between trans-unit id and source
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020026 $ php translation-status.php -v
27
28 # show the status of a single locale
29 $ php translation-status.php fr
30
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010031 # show the status of a single locale, missing translations and mismatches between trans-unit id and source
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020032 $ php translation-status.php fr -v
33
34END;
35
36$config = [
37 // if TRUE, the full list of missing translations is displayed
38 'verbose_output' => false,
39 // NULL = analyze all locales
40 'locale_to_analyze' => null,
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010041 // append --incomplete to only show incomplete languages
42 'include_completed_languages' => true,
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020043 // the reference files all the other translations are compared to
44 'original_files' => [
45 'src/Symfony/Component/Form/Resources/translations/validators.en.xlf',
46 'src/Symfony/Component/Security/Core/Resources/translations/security.en.xlf',
47 'src/Symfony/Component/Validator/Resources/translations/validators.en.xlf',
48 ],
49];
50
51$argc = $_SERVER['argc'];
52$argv = $_SERVER['argv'];
53
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010054if ($argc > 4) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020055 echo str_replace('translation-status.php', $argv[0], $usageInstructions);
56 exit(1);
57}
58
59foreach (array_slice($argv, 1) as $argumentOrOption) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010060 if ('--incomplete' === $argumentOrOption) {
61 $config['include_completed_languages'] = false;
62 continue;
63 }
64
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020065 if (str_starts_with($argumentOrOption, '-')) {
66 $config['verbose_output'] = true;
67 } else {
68 $config['locale_to_analyze'] = $argumentOrOption;
69 }
70}
71
72foreach ($config['original_files'] as $originalFilePath) {
73 if (!file_exists($originalFilePath)) {
74 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);
75 exit(1);
76 }
77}
78
79$totalMissingTranslations = 0;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010080$totalTranslationMismatches = 0;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020081
82foreach ($config['original_files'] as $originalFilePath) {
83 $translationFilePaths = findTranslationFiles($originalFilePath, $config['locale_to_analyze']);
84 $translationStatus = calculateTranslationStatus($originalFilePath, $translationFilePaths);
85
86 $totalMissingTranslations += array_sum(array_map(function ($translation) {
87 return count($translation['missingKeys']);
88 }, array_values($translationStatus)));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010089 $totalTranslationMismatches += array_sum(array_map(function ($translation) {
90 return count($translation['mismatches']);
91 }, array_values($translationStatus)));
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020092
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010093 printTranslationStatus($originalFilePath, $translationStatus, $config['verbose_output'], $config['include_completed_languages']);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020094}
95
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010096exit($totalTranslationMismatches > 0 ? 1 : 0);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020097
98function findTranslationFiles($originalFilePath, $localeToAnalyze)
99{
100 $translations = [];
101
102 $translationsDir = dirname($originalFilePath);
103 $originalFileName = basename($originalFilePath);
104 $translationFileNamePattern = str_replace('.en.', '.*.', $originalFileName);
105
106 $translationFiles = glob($translationsDir.'/'.$translationFileNamePattern, \GLOB_NOSORT);
107 sort($translationFiles);
108 foreach ($translationFiles as $filePath) {
109 $locale = extractLocaleFromFilePath($filePath);
110
111 if (null !== $localeToAnalyze && $locale !== $localeToAnalyze) {
112 continue;
113 }
114
115 $translations[$locale] = $filePath;
116 }
117
118 return $translations;
119}
120
121function calculateTranslationStatus($originalFilePath, $translationFilePaths)
122{
123 $translationStatus = [];
124 $allTranslationKeys = extractTranslationKeys($originalFilePath);
125
126 foreach ($translationFilePaths as $locale => $translationPath) {
127 $translatedKeys = extractTranslationKeys($translationPath);
128 $missingKeys = array_diff_key($allTranslationKeys, $translatedKeys);
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100129 $mismatches = findTransUnitMismatches($allTranslationKeys, $translatedKeys);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200130
131 $translationStatus[$locale] = [
132 'total' => count($allTranslationKeys),
133 'translated' => count($translatedKeys),
134 'missingKeys' => $missingKeys,
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100135 'mismatches' => $mismatches,
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200136 ];
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100137 $translationStatus[$locale]['is_completed'] = isTranslationCompleted($translationStatus[$locale]);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200138 }
139
140 return $translationStatus;
141}
142
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100143function isTranslationCompleted(array $translationStatus): bool
144{
145 return $translationStatus['total'] === $translationStatus['translated'] && 0 === count($translationStatus['mismatches']);
146}
147
148function printTranslationStatus($originalFilePath, $translationStatus, $verboseOutput, $includeCompletedLanguages)
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200149{
150 printTitle($originalFilePath);
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100151 printTable($translationStatus, $verboseOutput, $includeCompletedLanguages);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200152 echo \PHP_EOL.\PHP_EOL;
153}
154
155function extractLocaleFromFilePath($filePath)
156{
157 $parts = explode('.', $filePath);
158
159 return $parts[count($parts) - 2];
160}
161
162function extractTranslationKeys($filePath)
163{
164 $translationKeys = [];
165 $contents = new \SimpleXMLElement(file_get_contents($filePath));
166
167 foreach ($contents->file->body->{'trans-unit'} as $translationKey) {
168 $translationId = (string) $translationKey['id'];
169 $translationKey = (string) $translationKey->source;
170
171 $translationKeys[$translationId] = $translationKey;
172 }
173
174 return $translationKeys;
175}
176
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100177/**
178 * Check whether the trans-unit id and source match with the base translation.
179 */
180function findTransUnitMismatches(array $baseTranslationKeys, array $translatedKeys): array
181{
182 $mismatches = [];
183
184 foreach ($baseTranslationKeys as $translationId => $translationKey) {
185 if (!isset($translatedKeys[$translationId])) {
186 continue;
187 }
188 if ($translatedKeys[$translationId] !== $translationKey) {
189 $mismatches[$translationId] = [
190 'found' => $translatedKeys[$translationId],
191 'expected' => $translationKey,
192 ];
193 }
194 }
195
196 return $mismatches;
197}
198
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200199function printTitle($title)
200{
201 echo $title.\PHP_EOL;
202 echo str_repeat('=', strlen($title)).\PHP_EOL.\PHP_EOL;
203}
204
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100205function printTable($translations, $verboseOutput, bool $includeCompletedLanguages)
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200206{
207 if (0 === count($translations)) {
208 echo 'No translations found';
209
210 return;
211 }
212 $longestLocaleNameLength = max(array_map('strlen', array_keys($translations)));
213
214 foreach ($translations as $locale => $translation) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100215 if (!$includeCompletedLanguages && $translation['is_completed']) {
216 continue;
217 }
218
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200219 if ($translation['translated'] > $translation['total']) {
220 textColorRed();
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100221 } elseif (count($translation['mismatches']) > 0) {
222 textColorRed();
223 } elseif ($translation['is_completed']) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200224 textColorGreen();
225 }
226
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100227 echo sprintf(
228 '| Locale: %-'.$longestLocaleNameLength.'s | Translated: %2d/%2d | Mismatches: %d |',
229 $locale,
230 $translation['translated'],
231 $translation['total'],
232 count($translation['mismatches'])
233 ).\PHP_EOL;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200234
235 textColorNormal();
236
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100237 $shouldBeClosed = false;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200238 if (true === $verboseOutput && count($translation['missingKeys']) > 0) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100239 echo '| Missing Translations:'.\PHP_EOL;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200240
241 foreach ($translation['missingKeys'] as $id => $content) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100242 echo sprintf('| (id=%s) %s', $id, $content).\PHP_EOL;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200243 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100244 $shouldBeClosed = true;
245 }
246 if (true === $verboseOutput && count($translation['mismatches']) > 0) {
247 echo '| Mismatches between trans-unit id and source:'.\PHP_EOL;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200248
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100249 foreach ($translation['mismatches'] as $id => $content) {
250 echo sprintf('| (id=%s) Expected: %s', $id, $content['expected']).\PHP_EOL;
251 echo sprintf('| Found: %s', $content['found']).\PHP_EOL;
252 }
253 $shouldBeClosed = true;
254 }
255 if ($shouldBeClosed) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200256 echo str_repeat('-', 80).\PHP_EOL;
257 }
258 }
259}
260
261function textColorGreen()
262{
263 echo "\033[32m";
264}
265
266function textColorRed()
267{
268 echo "\033[31m";
269}
270
271function textColorNormal()
272{
273 echo "\033[0m";
274}