diff --git a/src/Illuminate/Validation/ValidationException.php b/src/Illuminate/Validation/ValidationException.php index 31f6f1fa9924..1418874333b8 100644 --- a/src/Illuminate/Validation/ValidationException.php +++ b/src/Illuminate/Validation/ValidationException.php @@ -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; diff --git a/tests/Validation/ValidationExceptionTest.php b/tests/Validation/ValidationExceptionTest.php index 2fbec79f5a53..5061f9e07d11 100755 --- a/tests/Validation/ValidationExceptionTest.php +++ b/tests/Validation/ValidationExceptionTest.php @@ -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([], []); @@ -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; + } }