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

[11.x] Improved translation for displaying the count of errors in the validation message #50560

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected static function summarize($validator)
if ($count = count($messages)) {
$pluralized = $count === 1 ? 'error' : 'errors';

$message .= ' '.$validator->getTranslator()->get("(and :count more $pluralized)", compact('count'));
$message .= ' '.$validator->getTranslator()->choice("(and :count more $pluralized)", $count, compact('count'));
}

return $message;
Expand Down
85 changes: 81 additions & 4 deletions tests/Validation/ValidationExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,74 @@ public function testExceptionSummarizesThreeOrMoreErrors()
$this->assertSame('validation.required (and 2 more errors)', $exception->getMessage());
}

public function testExceptionTranslatedSummarizesTwoErrors()
{
$translator = $this->getTranslator('uk', [
'*' => [
'*' => [
'uk' => [
'(and :count more error)' => '(та ще :count помилка)',
'(and :count more errors)' => '(та ще :count помилка)|(та ще :count помилки)|(та ще :count помилок)',
],
],
],
]);

$exception = $this->getException([], [
'foo' => 'required',
'bar' => 'required',
], $translator);

$this->assertSame('validation.required (та ще 1 помилка)', $exception->getMessage());
}

public function testExceptionTranslatedSummarizesThreeOrMoreErrors()
{
$translator = $this->getTranslator('uk', [
'*' => [
'*' => [
'uk' => [
'(and :count more error)' => '(та ще :count помилка)',
'(and :count more errors)' => '(та ще :count помилка)|(та ще :count помилки)|(та ще :count помилок)',
],
],
],
]);

$exception = $this->getException([], [
'foo' => 'required',
'bar' => 'required',
'baz' => 'required',
], $translator);

$this->assertSame('validation.required (та ще 2 помилки)', $exception->getMessage());
}

public function testExceptionTranslatedSummarizesFiveOrMoreErrors()
{
$translator = $this->getTranslator('uk', [
'*' => [
'*' => [
'uk' => [
'(and :count more error)' => '(та ще :count помилка)',
'(and :count more errors)' => '(та ще :count помилка)|(та ще :count помилки)|(та ще :count помилок)',
],
],
],
]);

$exception = $this->getException([], [
'foo' => 'required',
'bar' => 'required',
'baz' => 'required',
'baq' => 'required',
'baw' => 'required',
'bae' => 'required',
], $translator);

$this->assertSame('validation.required (та ще 5 помилок)', $exception->getMessage());
}

public function testExceptionErrorZeroErrors()
{
$exception = $this->getException([], []);
Expand Down Expand Up @@ -96,17 +164,26 @@ public function testGetExceptionClassFromValidator()
$this->assertEquals(ValidationException::class, $exception);
}

protected function getException($data = [], $rules = [])
protected function getException($data = [], $rules = [], $translator = null)
{
$validator = $this->getValidator($data, $rules);
$validator = $this->getValidator($data, $rules, $translator);

return new ValidationException($validator);
}

protected function getValidator($data = [], $rules = [])
protected function getValidator($data = [], $rules = [], $translator = null)
{
$translator = new Translator(new ArrayLoader, 'en');
$translator ??= $this->getTranslator();

return new Validator($translator, $data, $rules);
}

protected function getTranslator($locale = 'en', $loaded = [])
{
$translator ??= new Translator(new ArrayLoader, $locale);

$translator->setLoaded($loaded);

return $translator;
}
}
Loading