简体   繁体   English

Laravel 本地化抛出 htmlspecialchars() 错误

[英]Laravel localization throws htmlspecialchars() error

When I try to use the localization and retrieve translation strings, Laravel for some reason throws:当我尝试使用本地化并检索翻译字符串时,Laravel 出于某种原因抛出: 在此处输入图像描述 . .

From the controller to the view (products index page), I pass the $products variable.从 controller 到视图(产品索引页),我传递了$products变量。 On that page, I use the translation string {{ __('Products') }} and getting htmlspecialchars() error.在该页面上,我使用翻译字符串{{ __('Products') }}并收到htmlspecialchars()错误。 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.据我了解,出于某种原因,翻译字符串认为我将$products变量传递给{{ __('Products') }}翻译字符串,因为如果我将翻译字符串更改为(例如) {{ __('Products page') }} ,我不再收到此错误。 Can someone explain, why this error is occurring?有人可以解释为什么会发生此错误吗?

Controller 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.修复在 lang 文件夹中添加 en.json 文件时的问题。

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您将这个创建的中间件添加到列表中,然后将 go 添加到 web.php 路由文件并添加

 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 et ensuite vous pouvez recupé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如果您想根据 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抱歉,您必须同时使用这两个答案,因为它们是互补的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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