Skip to content

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

@dgvirtual

Description

@dgvirtual

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions