trojan
trojan

Reputation: 53

laravel 5.6 validation in multiple language

I want to switch the locale into another language when outputting the error message. Right now, the default is always the default language in English.

Inside my route file, I've got:

Route::get('/{locale}/contact', [
    'uses' => 'ContactController@create' ])->name('contact');

Route::post('/contact', [
    'uses' => 'ContactController@store' ]);

Inside the ContactController:

public function store(ContactFormRequest $request)
{
  ... ... ...
}

Inside the ContactFormRequest, if I add "App::getLocale('tc'), it will change the validation language into 'tc', instead of the default english 'en'.

class ContactFormRequest extends FormRequest
{
    public function rules()
    {
        App::getLocale('tc');

        return [
                'name' => 'required',
                'message' => 'required',
                'email' => 'required_without:number',
                'number' => 'required_without:email',
            ];
    }
}

My question is how I can detect the locale and pass the current locale into the ContactFormRequest in order to switch to different validation languages.

Upvotes: 1

Views: 2489

Answers (1)

Tudor
Tudor

Reputation: 1898

Check out this section in the laravel documentation.

You can either use custom inline messages, or even better you can create your own validation files for different languages resources/lang/xx/validation.php

Upvotes: 2

Related Questions