1) создать промежуточное программное обеспечение в App/HTTP/Middleware
localization.php
и написать их в том, что:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Application;
/**
* Class Localization
*
* @author Mahmoud Zalt <[email protected]>
*/
class Localization
{
/**
* Localization constructor.
*
* @param \Illuminate\Foundation\Application $app
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
// read the language from the request header
$locale = $request->header('Content-Language');
// if the header is missed
if(!$locale){
// take the default local language
$locale = $this->app->config->get('app.locale');
}
// check the languages defined is supported
if (!array_key_exists($locale, $this->app->config->get('app.supported_languages'))) {
// respond with error
return abort(403, 'Language not supported.');
}
// set the local language
$this->app->setLocale($locale);
// get the response after the request is done
$response = $next($request);
// set Content Languages header in the response
$response->headers->set('Content-Language', $locale);
// return the response
return $response;
}
}
2) Зарегистрируйте промежуточное программное обеспечение в для промежуточного программного это , перейти в App \ Http \ Kernel.php добавить в этом массиве, что есть в файле ядра:
protected $middleware = []
это одно.
\App\Http\Middleware\Localization::class,
3) добавьте к этому app.php в конфигурации директории
'supported_languages' => [ 'ан' => 'английский', 'фа' => 'персидская'],
4) создайте языковую папку в lang-папке «resources/lang» для вашего языка (в данном случае это [fa] рядом с [en]) и, конечно же, установите там ваши файлы. для этого вопроса только скопируйте файл validation.php в папку fa и измените текст ошибки.
5) установите заголовок «Content-Language» в вашем запросе ([ru] или [fa]).