英文:
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
'locale' => env('APP_LOCALE', 'en'),
'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),
Here's the route in web.php
use App\Http\Controllers\LocaleController;
Route::get('set-locale/{locale}', [LocaleController::class, 'setLocale'])->name('locale.set');
Here's my 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();
}
}
Using these buttons to change the language
<a href="{{ route('locale.set', 'en') }}">English</a>
<a href="{{ route('locale.set', 'fr') }}">Français</a>
<a href="{{ route('locale.set', 'ar') }}">العربية</a>
when I try this code to see what's going on
<div>Current locale: {{ session()->get('locale') }}</div>
<h1>{{ trans('messages.welcome') }}</h1>
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.
<div>Current locale: {{ session()->get('locale') }}</div>
<h1>{{ trans('messages.welcome', [], session()->get('locale')) }}</h1>
There is another helper that is an alias to trans
. __
.
<div>Current locale: {{ session()->get('locale') }}</div>
<h1>{{ __('messages.welcome', [], session()->get('locale')) }}</h1>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论