In my model I defined a few validation rules for date fields using before
and after
:
'birth_date' => 'required|date|before:today|after:01-jan-1920',
'another_date' => 'required|date|before:tomorrow|after:01-jan-1990',
The validation works fine, however I can't figure out how to translate the strings today
and tomorrow
on the validation message.
In the validation.php
language file the after
and before
messages are localizable, however the :date
part of the message is still displaying the English version for today
and tomorrow
.
"after" => "The :attribute must be a date after :date.",
"before" => "The :attribute must be a date before :date.",
How could I localize those two words - today
and tomorrow
- in the validation message?
In short, add following code into resources/lang/whichever/validation.php
'values' => [
// or whatever fields you wanna translate
'birth_date' => [
// or tomorrow
'today' => '今天'
]
]
Explained:
/**
* Get the displayable name of the value.
*
* @param string $attribute
* @param mixed $value
* @return string
*/
public function getDisplayableValue($attribute, $value)
{
if (isset($this->customValues[$attribute][$value])) {
return $this->customValues[$attribute][$value];
}
// the key we want
$key = "validation.values.{$attribute}.{$value}";
// if the translate found, then use it
if (($line = $this->translator->get($key)) !== $key) {
return $line;
}
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
return $value;
}