I try to get list of language which provided by languages.php
in my config folder and I get this error
htmlspecialchars() expects parameter 1 to be string, array given (View: C:\laragon\www\newproject\resources\views\layouts\panel.blade.php) (View: C:\laragon\www\newproject\resources\views\layouts\panel.blade.php)
Here is my languages.php
<?php
return [
'en' => [
'name' => 'English',
'flag' => 'images/flags/en.png'
],
'fa' => [
'name' => 'پارسی',
'flag' => 'images/flags/iran.png'
],
];
My middleware Language.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
class Language
{
public function handle($request, Closure $next)
{
if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) {
App::setLocale(Session::get('applocale'));
}
else { // This is optional as Laravel will automatically set the fallback language if there is none specified
App::setLocale(Config::get('app.fallback_locale'));
}
return $next($request);
}
}
LanguageController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Config;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
class LanguageController extends Controller
{
public function index($lang)
{
if (array_key_exists($lang, Config::get('languages'))) {
Session::put('applocale', $lang);
}
return Redirect::back();
}
}
Route
Route::get('lang/{lang}', ['as'=>'lang.switch', 'uses'=>'LanguageController@index']);
and finally my view
<li class="dropdown langs">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
@foreach (Config::get('languages') as $lang => $language)
@if ($lang != App::getLocale())
<li>
<a href=""></a>
</li>
@endif
@endforeach
</ul>
</li>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire