问题描述
im学习多语言环境旅馆laravel 5.4所以我创建了两个文件资源/lang/es/greeting.php中的第一个
im learning the multi locale inn laravel 5.4 soi created two files first one in resources/lang/es/greeting.php
<?php
return [
'hello' => 'hola',
];
其次是resources/lang/en/greeting.php
and second in resources/lang/en/greeting.php
<?php
return [
'hello' => 'hola',
];
我在web.php内创建了这条路由
and i created this route inside web.php
Route::get('/{locale}', function ($locale) {
App::setLocale($locale);
return view('index');
});
所以当我请求此链接时(localhost:8000/es)有用但是,当我刷新页面时,它会返回到默认的区域设置,即
so when i request this link (localhost:8000/es)it works but when i refresh the page its returns to default locale which is en
,我希望它保留在新的语言环境中所以请帮助我
and i want it to stay in the new locale so help me please
推荐答案
如果您要为该会话永久设置语言环境,请将路由代码更改为:
If you want to set the locale permanently for that session, change the route code to:
Route::get('/{locale}', function ($locale) {
App::setLocale($locale);
Session::put('locale', $locale);
return view('index');
});
然后添加一个中间件,以检查会话是否具有语言环境,如果是这样,则设置语言环境:
Then add a middleware to check if session has locale, and if so set the locale like so:
public function handle($request, Closure $next) {
if(Session::has('locale')) {
app()->setLocale(Session::get('locale'));
}
return $next($request);
}
这篇关于laravel 5.4中的语言环境刷新后将返回到先前的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!