无法在Laravel 4中切换语言
问题描述:
我尝试了路由以切换语言,但没有任何变化.请问您能帮我吗?
I tried routing to switch language but there's no change. Could you help me, pls?
Route::get('lang/{lang}', function($lang)
{
App::setLocale($lang);
return Redirect::to('/');
});
答
App::setLocale()
不是持久性的-也就是说,它不会记住两次请求之间存储的内容.相反,您可以使用会话来记住所选的语言环境,并从会话中读取每个请求的语言环境.如果会话中没有任何设置,我们还可以从配置中读取默认语言环境.
App::setLocale()
is not persistent - that is to say that it will not remember between requests what you have stored. Instead you could use the session to remember the chosen locale, and read from the session the locale on each request. We can also read the default locale (from config) in case there isn't one set in the session.
// app/routes.php
Route::get('lang/{lang}', function($lang)
{
Session::put('my.locale', $lang);
return Redirect::to('/');
});
// app/start/global.php
App::setLocale(Session::get('my.locale', Config::get('app.locale')));