diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ebd2036..36d797d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -71,6 +71,10 @@ All notable changes to this project are documented here. The format follows ## [Unreleased] +### Fixed + +- No translation changed. A new strict `TranslationCatalogueTest` fails on a non-en locale missing a key, on a non-en value identical to English (any length, placeholders included; values without letters are skipped) unless allow-listed per locale (brands, units, protocols, loanwords, true cognates), on a non-es value identical to the Spanish one (longer than 12 characters, different from English) unless allow-listed as genuinely shared, and on stale entries in either allow-list. + ## [2.0.1] - 2026-11-22 ### Fixed diff --git a/tests/Unit/Translation/TranslationCatalogueTest.php b/tests/Unit/Translation/TranslationCatalogueTest.php new file mode 100644 index 0000000..591cd46 --- /dev/null +++ b/tests/Unit/Translation/TranslationCatalogueTest.php @@ -0,0 +1,268 @@ +> + */ + private const ALLOWED_IDENTICAL = [ + 'de' => [ + 'move.field.bucket', + 'move.field.status', + 'profile.field.bucket', + 'profile.field.endpoint', + 'profile.field.region', + 'profile.placeholder.endpoint', + 'profile.placeholder.region', + 'provider.aws_s3', + 'settings.field.delivery', + ], + 'es' => [ + 'move.field.bucket', + 'probe.step.endpoint', + 'profile.field.bucket', + 'profile.field.endpoint', + 'profile.placeholder.endpoint', + 'profile.placeholder.region', + 'provider.aws_s3', + ], + 'fr' => [ + 'move.field.bucket', + 'probe.step.endpoint', + 'profile.field.bucket', + 'profile.field.endpoint', + 'profile.placeholder.endpoint', + 'profile.placeholder.region', + 'provider.aws_s3', + 'settings.section.config', + ], + 'it' => [ + 'move.field.bucket', + 'probe.step.endpoint', + 'profile.field.bucket', + 'profile.field.endpoint', + 'profile.field.provider', + 'profile.placeholder.endpoint', + 'profile.placeholder.region', + 'provider.aws_s3', + ], + 'nl' => [ + 'move.field.bucket', + 'move.field.status', + 'probe.step.endpoint', + 'profile.field.bucket', + 'profile.field.endpoint', + 'profile.field.provider', + 'profile.placeholder.endpoint', + 'profile.placeholder.region', + 'provider.aws_s3', + 'settings.field.delivery', + ], + 'pt' => [ + 'move.field.bucket', + 'probe.step.endpoint', + 'profile.field.bucket', + 'profile.field.endpoint', + 'profile.placeholder.endpoint', + 'profile.placeholder.region', + 'provider.aws_s3', + ], + ]; + + /** + * Keys whose non-es value legitimately equals the Spanish one (longer than 12 characters, different from English): + * words and phrases that are genuinely the same in Spanish and the locale, per locale. + * + * @var array> + */ + private const ALLOWED_SAME_AS_SPANISH = [ + 'pt' => [ + 'move.section.retired', + 'profile.field.public_endpoint', + ], + ]; + + /** @return iterable */ + public static function catalogues(): iterable + { + $files = glob(self::DIR . '/*.yaml') ?: []; + sort($files); + + foreach ($files as $file) { + if (preg_match('/^(.+)\.([a-z]{2}(?:_[A-Z]{2})?)\.yaml$/', basename($file), $m) === 1 && $m[2] !== 'en') { + yield basename($file) => [$m[1], $m[2]]; + } + } + } + + #[DataProvider('catalogues')] + public function testLocaleHasEveryEnglishKey(string $domain, string $locale): void + { + $missing = array_values(array_diff(array_keys($this->load($domain, 'en')), array_keys($this->load($domain, $locale)))); + + self::assertSame([], $missing, sprintf('Keys of %s.en.yaml missing in %s.%s.yaml', $domain, $domain, $locale)); + } + + #[DataProvider('catalogues')] + public function testNoUntranslatedEnglishValues(string $domain, string $locale): void + { + $english = $this->load($domain, 'en'); + $target = $this->load($domain, $locale); + $allowed = self::ALLOWED_IDENTICAL[$locale] ?? []; + $gaps = []; + + foreach ($english as $key => $value) { + if (!isset($target[$key]) || $target[$key] !== $value || in_array($key, $allowed, true)) { + continue; + } + + if (preg_match('/[^\W\d_]/u', (string) preg_replace('/%[^%\s]+%|\{[^}]+\}/', '', $value)) === 1) { + $gaps[] = $key; + } + } + + self::assertSame([], $gaps, sprintf('Values identical to English in %s.%s.yaml (translate or allow-list)', $domain, $locale)); + } + + /** @return iterable */ + public static function nonSpanishCatalogues(): iterable + { + foreach (self::catalogues() as $name => [$domain, $locale]) { + if ($locale !== 'es') { + yield $name => [$domain, $locale]; + } + } + } + + #[DataProvider('nonSpanishCatalogues')] + public function testNoSpanishLeftInOtherLocales(string $domain, string $locale): void + { + $english = $this->load($domain, 'en'); + $spanish = $this->load($domain, 'es'); + $target = $this->load($domain, $locale); + $allowed = self::ALLOWED_SAME_AS_SPANISH[$locale] ?? []; + $leaks = []; + + foreach ($spanish as $key => $value) { + if (!isset($target[$key]) || $target[$key] !== $value || $value === ($english[$key] ?? null) || mb_strlen($value) <= 12 || in_array($key, $allowed, true)) { + continue; + } + + $leaks[] = $key; + } + + self::assertSame([], $leaks, sprintf('Values identical to Spanish in %s.%s.yaml (translate or allow-list)', $domain, $locale)); + } + + public function testAllowListHasNoStaleEntries(): void + { + $english = []; + $locales = []; + + foreach (self::catalogues() as [$domain, $locale]) { + $english += $this->load($domain, 'en'); + $locales[$locale] = ($locales[$locale] ?? []) + $this->load($domain, $locale); + } + + $stale = []; + + foreach (self::ALLOWED_IDENTICAL as $locale => $keys) { + foreach ($keys as $key) { + if (!isset($english[$key]) || ($locales[$locale][$key] ?? null) !== $english[$key]) { + $stale[] = $locale . ':' . $key; + } + } + } + + self::assertSame([], $stale, 'Allow-list entries that no longer exist or are no longer identical to English'); + } + + public function testSpanishAllowListHasNoStaleEntries(): void + { + $english = []; + $spanish = []; + $locales = []; + + foreach (self::catalogues() as [$domain, $locale]) { + $english += $this->load($domain, 'en'); + $spanish += $this->load($domain, 'es'); + $locales[$locale] = ($locales[$locale] ?? []) + $this->load($domain, $locale); + } + + $stale = []; + + foreach (self::ALLOWED_SAME_AS_SPANISH as $locale => $keys) { + foreach ($keys as $key) { + $value = $spanish[$key] ?? null; + + if ($value === null || ($locales[$locale][$key] ?? null) !== $value || $value === ($english[$key] ?? null) || mb_strlen($value) <= 12) { + $stale[] = $locale . ':' . $key; + } + } + } + + self::assertSame([], $stale, 'Spanish allow-list entries that no longer exist or no longer equal the Spanish value'); + } + + /** @return array */ + private function load(string $domain, string $locale): array + { + $data = Yaml::parseFile(sprintf('%s/%s.%s.yaml', self::DIR, $domain, $locale)); + + return $this->flatten(is_array($data) ? $data : []); + } + + /** + * @param array $tree + * + * @return array + */ + private function flatten(array $tree, string $prefix = ''): array + { + $out = []; + + foreach ($tree as $key => $value) { + $path = $prefix === '' ? (string) $key : $prefix . '.' . $key; + + if (is_array($value)) { + $out += $this->flatten($value, $path); + } else { + $out[$path] = (string) $value; + } + } + + return $out; + } +}