mySun
mySun

Reputation: 1696

How to set custom validation message in Laravel 5.2 for multi language?

My website has three language ‍['English', 'Persian', 'Arabic'].

For example:

In Request:

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'mobile' => 'required|max:11|min:11',
        'register_id' => 'required|max:7|min:7'
    ];
}

I want After selecting the language by the user, An error message is displayed in the selected language.

What should I do?

Upvotes: 0

Views: 1637

Answers (1)

craig_h
craig_h

Reputation: 32694

If you would like to use different languages in your laravel app then you can use the built in localization:

https://laravel.com/docs/5.2/localization

EDIT:

For your purposes, I would approach it like this.

First, create your language folders in resources/lang, for example, for you Arabic messages you would place a messages.php in resource/lang/ar

I'm not sure how you are storing a users language, so I'll assume it's stored in the session as lang.

Now you can create middleware to check the users chosen language and set the locale:

public function handle($request, Closure $next)
{
    // set language from session
    \App::setLocale(session('lang'));
    return $next($request);
}

Now register that as global middleware in: app\Kernal.php

Now your app should automatically set the locale before each request based on the 'lang' value in the session. Make sure you have fallback_locale set so your app knows what to do if your user doesn't have a locale set.

Upvotes: 1

Related Questions