我正在嘗試使用配置控制器更改 Laravel 7 中的應用程序區域設置:class ConfigController extends Controller{ /** * * Set the App locale. * * @param \SetLocaleRequest $request * @return mixed */ public function set_locale(SetLocaleRequest $request) { App::setLocale($request->locale); return response()->json([ 'message' => trans('config.set'), ], 200); }}這段代碼實際上有效,因為一個簡單App:getLocale();的返回提供的語言。無論哪種方式,Lang外觀都會繼續使用提供的默認語言環境,config/app.php即西班牙語。所以,這段代碼:Lang::get('auth.failed')正在返回文本:"Estas credenciales no coinciden con nuestros registros."即使en當前已選中。知道為什么嗎?
1 回答

守著星空守著你
TA貢獻1799條經驗 獲得超8個贊
動態更改語言環境是 2 個步驟。我看到你已經完成了第 1 步。第 2 步是在你的刀片文件中做這樣的事情(很可能是基本模板):
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
請注意,一旦用戶導航到另一個頁面,它就會返回到配置中設置的語言環境。但是,如果您需要為當前用戶維護區域設置,請使用會話。例如,除了我之前的觀點之外,將您的控制器方法更改為:
public function set_locale(SetLocaleRequest $request)
{
App::setLocale($request->locale);
Session::put('locale', $request->locale);
return response()->json([
'message' => 'locale.set.success',
], 200);
}
因此,您可以通過以下方式在刀片模板中連續訪問它:<html lang="{{ str_replace('_', '-', Session::get('locale')) }}">
- 1 回答
- 0 關注
- 131 瀏覽
添加回答
舉報
0/150
提交
取消