Ad

Specify Laravel Validation Language

is it at all possible to tell the Laravel validator which language to use for validation?

I have an application which has its text in English, but for some of the forms, I need the validation errors for the fields to be returned in a different language.

I researched a little and found out that I can just use \App::setLocale('ro') to set the language of the app and thus the file under resources/lang/ro/validation.php will be used for the validation, but I do not want to temper with the setLocale. I could, in the worst scenario, temper with it and change the language before the validation and change it back after the validation again, but it doesn't seem like a good solution.

I am looking for something more like this:

$this->validate($request, [
    'title' => 'required',
    'short' => 'required',
], 'lang_that_I_set_in_DB');
Ad

Answer

I did figure this out, thanks for giving me pointers!

This is how it looks in Laravel 5.1:

$validator = \Validator::make($request->all(), [
    'title' => 'required',
    'short' => 'required',
]);

$validator->getTranslator()->setLocale('ro');

if ($validator->fails()) {
    return redirect()
        ->back()
        ->withErrors($validator)
        ->withInput();
}
Ad
source: stackoverflow.com
Ad