简体   繁体   中英

Laravel localization throws htmlspecialchars() error

When I try to use the localization and retrieve translation strings, Laravel for some reason throws: 在此处输入图像描述 .

From the controller to the view (products index page), I pass the $products variable. On that page, I use the translation string {{ __('Products') }} and getting htmlspecialchars() error. As I understood it, translation string for some reason thinks that I passing $products variable to {{ __('Products') }} translation string, because if I change translation string to (for example) {{ __('Products page') }} , I not getting this error anymore. Can someone explain, why this error is occurring?

Controller

在此处输入图像描述

Code when an error is occurring

在此处输入图像描述

Code when an error no more occurring

在此处输入图像描述

UPDATE

Fixed problem when added en.json file in the lang folder.

in truth to translate i don't do it that way. in fact i create a midleware and i create a fair route. let's take the case of french and english. let's create a midleware Location

php artisan make:middleware Localisation

then fill in the middleware

<?php

namespace App\Http\Middleware;

use Closure;

use Illuminate\Http\Request;


class Localization
{
    public function handle(Request $request, Closure $next)
    {
        if(\Session::has('locale'))
        {
            \App::setlocale(\Session::get('locale'));
        }
 
        return $next($request);
    }
}
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\Localization::class,
    ],

you add this created middleware to the list and then you go to the web.php routes file and add

 Route::get('/locale/{locale}', function ($locale){
\Session::put('locale', $locale);
return redirect()->back();
})->name('traduction');

et ensuite vous pouvez récupérer la session de cette facon

<a href="{{route('traduction',['locale'=>'en'])}}" class="menu-link d-flex px-5 active"> 
<a href="{{route('traduction',['locale'=>'fr'])}}" class="menu-link d-flex px-5 active">

in case you want to display an image according to the session

<a href="#" class="menu-link px-5">
                            <span class="menu-title position-relative">Langue {{ Session::get('locale') }}
                             @if (Session::get('locale') == "fr")
                            <span class="fs-8 rounded bg-light px-3 py-2 position-absolute translate-middle-y top-50 end-0">Francais 
                            <img class="w-15px h-15px rounded-1 ms-2" src="assets/media/flags/france.svg" alt="" /></span></span>
                            @else
                            <span class="fs-8 rounded bg-light px-3 py-2 position-absolute translate-middle-y top-50 end-0">English 
                            <img class="w-15px h-15px rounded-1 ms-2" src="assets/media/flags/united-states.svg" alt="" /></span></span>
                            @endif
                        </a>

sorry but you have to use both answers as they are complementary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM