'Use from specific language in a request validation - Laravel
My Laravel project is a multi language.
I use from Laravel validator in api. Now errors will appear with the language set in the application.
But I want all the errors in api section to be returned in English.
Use \App::setLocale('en'); in the ApiContrpller, Exactly, not what I want!
How can I set an specific language only in the request validator?
Solution 1:[1]
If you just want to change the language in that request, you should create a middleware for your API that change the app locale:
app()->setLocale('es');
Solution 2:[2]
If you really want to use different locale in FormRequest only, you could do something like this:
class OrderRequest extends FormRequest
{
protected string $appLocale;
protected function prepareForValidation(): void
{
$this->appLocale = app()->getLocale();
app()->setLocale($this->input('locale', 'en'));
}
protected function passedValidation(): void
{
app()->setLocale($this->appLocale);
}
// your code...
}
NOTE: There is a setLocale and setDefaultLocale method on the FormRequest but it didn't work for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zrkb |
| Solution 2 | Dharman |
