CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Bug: Language strings with plural forms do not work in within validations

Open dgvirtual opened this issue 1 year ago • 0 comments

PHP Version

8.1

CodeIgniter4 Version

4.5.4

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

SQLite3

What happened?

I am using Lithuanian locale, so I have in app/Config/App.php: public string $defaultLocale = 'lt'; and default translation package. It has a string for min_length in lt/Validation.php language strings file this:

'min_length' => '{param, plural,
    =0 {Lauke „{field}" negali būti mažiau nei nulis ženklų}
    =1 {Lauke „{field}" negali būti mažiau nei vienas ženklas}
    one {Lauke „{field}" negali būti mažiau nei # ženklas}
    few {Lauke „{field}" negali būti mažiau nei # ženklai}
    other {Lauke „{field}" negali būti mažiau nei # ženklų}
}',

When I use that validation rule and it fails, instead of one string I get all that code block as error string.

Steps to Reproduce

Set locale to Lithuanian, install default codeigniter4 translations and run this:

$validation = \Config\Services::validation();

$rules = [
    'test_string' => 'min_length[5]',
];

// Set test data; make string too short
$data = [
    'test_string' => 'abc',
];

echo '<pre>';
echo 'When used as simple language string, it echoes correctly: ' . PHP_EOL;
echo lang('Validation.min_length', ['field' => 'Lauko pavadinimas', 'param' => 5]);

echo 'But fails when used within validation: ' . PHP_EOL;

// Run the validation
if (!$validation->setRules($rules)->run($data)) {
    // If validation fails, get the error messages
    $errors = $validation->getErrors();
    echo $errors['test_string'] . PHP_EOL;
} else {
    echo "Validation passed!" . PHP_EOL;
}

Expected Output

I would expect the validation string to be: Lauke „test_string" negali būti mažiau nei 5 ženklai

So, whole output of the script should be:

When used as simple language string, it echoes correctly: Lauke „Lauko pavadinimas" negali būti mažiau nei 10 ženklų But fails when used within validation: Lauke „test_string" negali būti mažiau nei 5 ženklai

Anything else?

@michalsn asked to be pinged about it!

dgvirtual avatar Sep 27 '24 14:09 dgvirtual