Skip to content

Commit 677c8a8

Browse files
committed
Add test for checking localization parameters are not translated
1 parent fb4142b commit 677c8a8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/Language/AbstractTranslationTestCase.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,57 @@ final public function testAllConfiguredLanguageKeysAreInOrder(string $locale): v
304304
));
305305
}
306306

307+
/**
308+
* @see https://codeigniter4.github.io/CodeIgniter4/outgoing/localization.html#replacing-parameters
309+
*
310+
* @dataProvider localesProvider
311+
*/
312+
final public function testAllLocalizationParametersAreNotTranslated(string $locale): void
313+
{
314+
$diffs = [];
315+
316+
foreach ($this->foundSets($locale) as $file) {
317+
$original = $this->loadFile($file);
318+
$translated = $this->loadFile($file, $locale);
319+
320+
foreach ($original as $key => $translation) {
321+
if (! array_key_exists($key, $translated)) {
322+
continue;
323+
}
324+
325+
preg_match_all('/(\{[^\}]+\})/', $translation, $matches);
326+
array_shift($matches);
327+
328+
if ($matches === []) {
329+
unset($matches);
330+
331+
continue;
332+
}
333+
334+
foreach ($matches as $match) {
335+
foreach ($match as $parameter) {
336+
if (strpos($translated[$key], $parameter) === false) {
337+
$diffs[sprintf('%s.%s', substr($file, 0, -4), $key)] = $parameter;
338+
}
339+
}
340+
}
341+
342+
unset($matches);
343+
}
344+
}
345+
346+
ksort($diffs);
347+
348+
$this->assertEmpty($diffs, sprintf(
349+
"Failed asserting that parameters of translation keys are not translated:\n%s",
350+
implode("\n", array_map(
351+
static fn (string $key, string $value): string => sprintf(' * %s => %s', $key, $value),
352+
array_keys($diffs),
353+
array_values($diffs)
354+
))
355+
));
356+
}
357+
307358
/**
308359
* @return string[][]
309360
*/

0 commit comments

Comments
 (0)