Laravel内置的多语言功能无法正常工作。

huangapple go评论59阅读模式
英文:

Laravel built-in multilang not working correctly

问题

感谢您阅读此内容
我正在使用Laravel 10.7.1
我已经在Resources/lang/中正确设置了语言文件夹 - ar/fr/en / messages.php

这是app.php中的代码

'locale' => env('APP_LOCALE', 'en'),
'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),

这是web.php中的路由

use App\Http\Controllers\LocaleController;
Route::get('set-locale/{locale}', [LocaleController::class, 'setLocale'])->name('locale.set');

这是我的LocaleController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class LocaleController extends Controller
{
    public function setLocale($locale)
    {
        session()->put('locale', $locale);
        App::setLocale($locale);
        return redirect()->back();
    }
}

使用以下按钮来更改语言

<a href="{{ route('locale.set', 'en') }}">English</a>
<a href="{{ route('locale.set', 'fr') }}">Français</a>
<a href="{{ route('locale.set', 'ar') }}">العربية</a>

当我尝试运行此代码查看发生了什么时

<div>Current locale: {{ session()->get('locale') }}</div>
<h1>{{ trans('messages.welcome') }}</h1>

结果是:

Current locale: fr
Welcome

尽管消息被正确翻译。

英文:

thanks for taking the time to read this
I'm using Laravel 10.7.1
I have the languages folders setup correctly (I hope) in Resources/lang/ - ar/fr/en / messages.php

Here's the code in app.php

&#39;locale&#39; =&gt; env(&#39;APP_LOCALE&#39;, &#39;en&#39;),
&#39;fallback_locale&#39; =&gt; env(&#39;APP_FALLBACK_LOCALE&#39;, &#39;en&#39;),

Here's the route in web.php

use App\Http\Controllers\LocaleController;  
Route::get(&#39;set-locale/{locale}&#39;, [LocaleController::class, &#39;setLocale&#39;])-&gt;name(&#39;locale.set&#39;);

Here's my LocaleController

&lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;


class LocaleController extends Controller
{
    public function setLocale($locale)
    {
        session()-&gt;put(&#39;locale&#39;, $locale);
        App::setLocale($locale);
        return redirect()-&gt;back();
    }
}

Using these buttons to change the language

    &lt;a href=&quot;{{ route(&#39;locale.set&#39;, &#39;en&#39;) }}&quot;&gt;English&lt;/a&gt;
    &lt;a href=&quot;{{ route(&#39;locale.set&#39;, &#39;fr&#39;) }}&quot;&gt;Fran&#231;ais&lt;/a&gt;
    &lt;a href=&quot;{{ route(&#39;locale.set&#39;, &#39;ar&#39;) }}&quot;&gt;العربية&lt;/a&gt;

when I try this code to see what's going on

&lt;div&gt;Current locale: {{ session()-&gt;get(&#39;locale&#39;) }}&lt;/div&gt;
&lt;h1&gt;{{ trans(&#39;messages.welcome&#39;) }}&lt;/h1&gt;

The result is :

Current locale: fr
Welcome

Despite the message being correctly translated.

答案1

得分: 2

The signature for the trans helper is as follows:

function trans($key = null, $replace = [], $locale = null)

You could pass the locale as the third parameter.

<div>当前区域:{{ session()->get('locale') }}</div>
<h1>{{ trans('messages.welcome', [], session()->get('locale')) }}</h1>

There is another helper that is an alias to trans. __.

<div>当前区域:{{ session()->get('locale') }}</div>
<h1>{{ __('messages.welcome', [], session()->get('locale')) }}</h1>
英文:

The signature for the trans helper is the following:

function trans($key = null, $replace = [], $locale = null)

You could pass the locale as the third parameter.

&lt;div&gt;Current locale: {{ session()-&gt;get(&#39;locale&#39;) }}&lt;/div&gt;
&lt;h1&gt;{{ trans(&#39;messages.welcome&#39;, [], session()-&gt;get(&#39;locale&#39;)) }}&lt;/h1&gt;

There is another helper that is an alias to trans. __.

&lt;div&gt;Current locale: {{ session()-&gt;get(&#39;locale&#39;) }}&lt;/div&gt;
&lt;h1&gt;{{ __(&#39;messages.welcome&#39;, [], session()-&gt;get(&#39;locale&#39;)) }}&lt;/h1&gt;

huangapple
  • 本文由 发表于 2023年4月17日 10:33:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定