10000 Report mismatches between trans-unit id and source text via status sc… · symfony/symfony@2982eec · GitHub
[go: up one dir, main page]

Skip to content

Commit 2982eec

Browse files
rvanlaakfabpot
authored andcommitted
Report mismatches between trans-unit id and source text via status script
1 parent a0d0c22 commit 2982eec

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed

.github/workflows/integration-tests.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,13 @@ jobs:
117117
# docker run --rm -e COMPOSER_ROOT_VERSION -v $(pwd):/app -v $(which composer):/usr/local/bin/composer -v $(which vulcain):/usr/local/bin/vulcain -w /app php:8.0-alpine ./phpunit src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php --filter testHttp2Push
118118
# sudo rm -rf .phpunit
119119
# [ -d .phpunit.bak ] && mv .phpunit.bak .phpunit
120+
121+
- uses: marceloprado/has-changed-path@v1
122+
id: changed-translation-files
123+
with:
124+
paths: src/**/Resources/translations/*.xlf
125+
126+
- name: Check Translation Status
127+
if: steps.changed-translation-files.outputs.changed == 'true'
128+
run: |
129+
php src/Symfony/Component/Translation/Resources/bin/translation-status.php -v

src/Symfony/Component/Translation/Resources/bin/translation-status.php

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
# show the translation status of all locales
2020
$ php translation-status.php
2121
22-
# show the translation status of all locales and all their missing translations
22+
# 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
2326
$ php translation-status.php -v
2427
2528
# show the status of a single locale
2629
$ php translation-status.php fr
2730
28-
# show the status of a single locale and all its missing translations
31+
# show the status of a single locale, missing translations and mismatches between trans-unit id and source
2932
$ php translation-status.php fr -v
3033
3134
END;
@@ -35,6 +38,8 @@
3538
'verbose_output' => false,
3639
// NULL = analyze all locales
3740
'locale_to_analyze' => null,
41+
// append --incomplete to only show incomplete languages
42+
'include_completed_languages' => true,
3843
// the reference files all the other translations are compared to
3944
'original_files' => [
4045
'src/Symfony/Component/Form/Resources/translations/validators.en.xlf',
@@ -46,12 +51,17 @@
4651
$argc = $_SERVER['argc'];
4752
$argv = $_SERVER['argv'];
4853

49-
if ($argc > 3) {
54+
if ($argc > 4) {
5055
echo str_replace('translation-status.php', $argv[0], $usageInstructions);
5156
exit(1);
5257
}
5358

5459
foreach (array_slice($argv, 1) as $argumentOrOption) {
60+
if ('--incomplete' === $argumentOrOption) {
61+
$config['include_completed_languages'] = false;
62+
continue;
63+
}
64+
5565
if (str_starts_with($argumentOrOption, '-')) {
5666
$config['verbose_output'] = true;
5767
} else {
@@ -67,6 +77,7 @@
6777
}
6878

6979
$totalMissingTranslations = 0;
80+
$totalTranslationMismatches = 0;
7081

7182
foreach ($config['original_files'] as $originalFilePath) {
7283
$translationFilePaths = findTranslationFiles($originalFilePath, $config['locale_to_analyze']);
@@ -75,11 +86,14 @@
7586
$totalMissingTranslations += array_sum(array_map(function ($translation) {
7687
return count($translation['missingKeys']);
7788
}, array_values($translationStatus)));
89+
$totalTranslationMismatches += array_sum(array_map(function ($translation) {
90+
return count($translation['mismatches']);
91+
}, array_values($translationStatus)));
7892

79-
printTranslationStatus($originalFilePath, $translationStatus, $config['verbose_output']);
93+
printTranslationStatus($originalFilePath, $translationStatus, $config['verbose_output'], $config['include_completed_languages']);
8094
}
8195

82-
exit($totalMissingTranslations > 0 ? 1 : 0);
96+
exit($totalTranslationMismatches > 0 ? 1 : 0);
8397

8498
function findTranslationFiles($originalFilePath, $localeToAnalyze)
8599
{
@@ -112,21 +126,29 @@ function calculateTranslationStatus($originalFilePath, $translationFilePaths)
112126
foreach ($translationFilePaths as $locale => $translationPath) {
113127
$translatedKeys = extractTranslationKeys($translationPath);
114128
$missingKeys = array_diff_key($allTranslationKeys, $translatedKeys);
129+
$mismatches = findTransUnitMismatches($allTranslationKeys, $translatedKeys);
115130

116131
$translationStatus[$locale] = [
117132
'total' => count($allTranslationKeys),
118133
'translated' => count($translatedKeys),
119134
'missingKeys' => $missingKeys,
135+
'mismatches' => $mismatches,
120136
];
137+
$translationStatus[$locale]['is_completed'] = isTranslationCompleted($translationStatus[$locale]);
121138
}
122139

123140
return $translationStatus;
124141
}
125142

126-
function printTranslationStatus($originalFilePath, $translationStatus, $verboseOutput)
143+
function isTranslationCompleted(array $translationStatus): bool
144+
{
145+
return $translationStatus['total'] === $translationStatus['translated'] && 0 === count($translationStatus['mismatches']);
146+
}
147+
148+
function printTranslationStatus($originalFilePath, $translationStatus, $verboseOutput, $includeCompletedLanguages)
127149
{
128150
printTitle($originalFilePath);
129-
printTable($translationStatus, $verboseOutput);
151+
printTable($translationStatus, $verboseOutput, $includeCompletedLanguages);
130152
echo \PHP_EOL.\PHP_EOL;
131153
}
132154

@@ -152,13 +174,35 @@ function extractTranslationKeys($filePath)
152174
return $translationKeys;
153175
}
154176

177+
/**
178+
* Check whether the trans-unit id and source match with the base translation.
179+
*/
180+
function 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+
155199
function printTitle($title)
156200
{
157201
echo $title.\PHP_EOL;
158202
echo str_repeat('=', strlen($title)).\PHP_EOL.\PHP_EOL;
159203
}
160204

161-
function printTable($translations, $verboseOutput)
205+
function printTable($translations, $verboseOutput, bool $includeCompletedLanguages)
162206
{
163207
if (0 === count($translations)) {
164208
echo 'No translations found';
@@ -168,24 +212,47 @@ function printTable($translations, $verboseOutput)
168212
$longestLocaleNameLength = max(array_map('strlen', array_keys($translations)));
169213

170214
foreach ($translations as $locale => $translation) {
215+
if (!$includeCompletedLanguages && $translation['is_completed']) {
216+
continue;
217+
}
218+
171219
if ($translation['translated'] > $translation['total']) {
172220
textColorRed();
173-
} elseif ($translation['translated'] === $translation['total']) {
221+
} elseif (count($translation['mismatches']) > 0) {
222+
textColorRed();
223+
} elseif ($translation['is_completed']) {
174224
textColorGreen();
175225
}
176226

177-
echo sprintf('| Locale: %-'.$longestLocaleNameLength.'s | Translated: %d/%d', $locale, $translation['translated'], $translation['total']).\PHP_EOL;
227+
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;
178234

179235
textColorNormal();
180236

237+
$shouldBeClosed = false;
181238
if (true === $verboseOutput && count($translation['missingKeys']) > 0) {
182-
echo str_repeat('-', 80).\PHP_EOL;
183-
echo '| Missing Translations:'.\PHP_EOL;
239+
echo '| Missing Translations:'.\PHP_EOL;
184240

185241
foreach ($translation['missingKeys'] as $id => $content) {
186-
echo sprintf('| (id=%s) %s', $id, $content).\PHP_EOL;
242+
echo sprintf('| (id=%s) %s', $id, $content).\PHP_EOL;
187243
}
244+
$shouldBeClosed = true;
245+
}
246+
if (true === $verboseOutput && count($translation['mismatches']) > 0) {
247+
echo '| Mismatches between trans-unit id and source:'.\PHP_EOL;
188248

249+
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) {
189256
echo str_repeat('-', 80).\PHP_EOL;
190257
}
191258
}

0 commit comments

Comments
 (0)
0