Skip to content

Commit

Permalink
Add Validation::setLanguage and lazy-load Language
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Feb 3, 2022
1 parent 96a4c21 commit bf0aed7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"ext-json": "*",
"aplus/debug": "^2.4",
"aplus/helpers": "^2.3",
"aplus/language": "^1.7"
"aplus/language": "^1.8"
},
"require-dev": {
"ext-xdebug": "*",
Expand Down
17 changes: 14 additions & 3 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,26 @@ public function __construct(array $validators = null, Language $language = null)
$this->validators = empty($validators)
? $defaultValidators
: \array_reverse($validators);
if ($language) {
$this->setLanguage($language);
}
}

public function setLanguage(Language $language = null) : static
{
if ($language === null) {
$language = new Language('en');
$language = new Language();
}
$language->addDirectory(__DIR__ . '/Languages');
$this->language = $language;
return $this;
}

public function getLanguage() : Language
{
if ( ! isset($this->language)) {
$this->setLanguage();
}
return $this->language;
}

Expand Down Expand Up @@ -262,9 +273,9 @@ public function getFilledMessage(string $field, string $rule, array $args = [])
{
$message = $this->getMessage($field, $rule);
if ($message === null) {
return $this->language->render('validation', $rule, $args);
return $this->getLanguage()->render('validation', $rule, $args);
}
return $this->language->formatMessage($message, $args);
return $this->getLanguage()->formatMessage($message, $args);
}

/**
Expand Down

0 comments on commit bf0aed7

Please sign in to comment.