Skip to content

Commit

Permalink
Improved translation for displaying the count of errors in the valida…
Browse files Browse the repository at this point in the history
…tion message (#50560)
  • Loading branch information
andrey-helldar authored Mar 15, 2024
1 parent 726c352 commit c018aa1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
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;
}
}

0 comments on commit c018aa1

Please sign in to comment.