【发布时间】:2019-01-27 00:36:21
【问题描述】:
我在尝试使用新设置的区域设置为我的路线添加前缀时遇到了一个问题。 当我从控制器或路由闭包返回 app()->getLocale() 时,它会正确返回新的设置语言环境,但是当我将它放在路由前缀中时,我会得到默认的“en”。在我的例子中,新的语言环境是 'ar'。
这是我在 web.php 中的代码
<?php
//Request to put the choosen locale into the session
Route::get('locale/{locale}', function($locale) {
session()->put('userLocale', $locale);
return redirect(app()->getLocale());
});
//Group of routes that are supposed to be prefixed with new set locale 'ar'
Route::group(['prefix' => app()->getLocale(), 'middleware' => 'locale'], function() {
Route::get('/', function() {
return app()->getLocale();
});
});
这是中间件:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
class Locale
{
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->app->setLocale(session('userLocale'));
return $next($request);
}
}
注意 Route 前缀中的语句“app()->getLocale()”,该语句的值始终为“en”。但是当我从闭包中返回它时,它会打印新设置的语言环境“ar”。
我想说的是,在前缀内部,app()->getLocale() 的值始终为“en”,但在闭包内部其值为“ar”。
所以当我在浏览器中运行“localehost:8000/locale/ar”时,输出是“ar”,这是正确的,但 url 变成了“localehost:8000/en”,应该是“localehost:8000/ar” “不是吗?当我在浏览器中运行“localehost:8000/ar”时,我得到一个 404 页面。
我尝试在 RouteServiceProvider 的 mapWebRoutes 方法中设置前缀,但它不起作用,因为我认为服务提供者是在中间件之前执行的,所以它无法识别新的语言环境由中间件设置。
我希望获得帮助或另一种方法来为路线添加新的语言环境前缀,因为也许我做错了什么。
【问题讨论】:
标签: php laravel localization