无法在Laravel 4中切换语言 [英] Cannot switch language in Laravel 4

查看:59
本文介绍了无法在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')));

这篇关于无法在Laravel 4中切换语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆