Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dgvirtual opened this issue Sep 27, 2024 · 0 comments · May be fixed by #9201
Open

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

dgvirtual opened this issue Sep 27, 2024 · 0 comments · May be fixed by #9201
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@dgvirtual
Copy link
Contributor

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 dgvirtual added the bug Verified issues on the current code behavior or pull requests that will fix them label Sep 27, 2024
@michalsn michalsn linked a pull request Sep 27, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant