Skip to content

Commit

Permalink
Add test for checking localization parameters are not translated
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Nov 20, 2023
1 parent fb4142b commit 677c8a8
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/Language/AbstractTranslationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,57 @@ final public function testAllConfiguredLanguageKeysAreInOrder(string $locale): v
));
}

/**
* @see https://codeigniter4.github.io/CodeIgniter4/outgoing/localization.html#replacing-parameters
*
* @dataProvider localesProvider
*/
final public function testAllLocalizationParametersAreNotTranslated(string $locale): void
{
$diffs = [];

foreach ($this->foundSets($locale) as $file) {
$original = $this->loadFile($file);
$translated = $this->loadFile($file, $locale);

foreach ($original as $key => $translation) {
if (! array_key_exists($key, $translated)) {
continue;
}

preg_match_all('/(\{[^\}]+\})/', $translation, $matches);
array_shift($matches);

if ($matches === []) {
unset($matches);

continue;
}

foreach ($matches as $match) {
foreach ($match as $parameter) {
if (strpos($translated[$key], $parameter) === false) {
$diffs[sprintf('%s.%s', substr($file, 0, -4), $key)] = $parameter;
}
}
}

unset($matches);
}
}

ksort($diffs);

$this->assertEmpty($diffs, sprintf(
"Failed asserting that parameters of translation keys are not translated:\n%s",
implode("\n", array_map(
static fn (string $key, string $value): string => sprintf(' * %s => %s', $key, $value),
array_keys($diffs),
array_values($diffs)
))
));
}

/**
* @return string[][]
*/
Expand Down

0 comments on commit 677c8a8

Please sign in to comment.