使用 Laravel 动态更改 url 和语言环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22546596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
changing url, and locale on the fly with laravel
提问by user3384514
My code in laravel to handle multiple language is:
我在 laravel 中处理多种语言的代码是:
$languages = array('it-IT','en-GB','fr-FR');
$lingua = Request::segment(1);
if(in_array($lingua, $languages)){
\App::setLocale($lingua);
}else{
$lingua = 'it-IT';
}
Route::group(array('prefix' => $lingua), function()
{
Route::get('/', array('as' => 'home', 'uses' => 'ItemController@menu'));
Route::get('/{idcampo}','ItemController@show');
});
How can i:
我怎样才能:
1)Make the page start always with it-IT as default. (i need it because I use $lingua to fetch from a database) so i can't have that null. Should I use a redirect::to / to /it-IT?
1) 使页面始终以 it-IT 为默认值。(我需要它,因为我使用 $lingua 从数据库中获取)所以我不能有那个空值。我应该使用重定向::到 / 到 /it-IT 吗?
2) change url and language(app:locale) on he fly with a link in the upper section of every pages. withouth returning to the home.
2)在每个页面的上半部分使用链接即时更改网址和语言(app:locale)。不回家。
3) to link pages I learn to use:
URL::route('home')
but how to do it when the link change with the entry of a database (for example my link is {{ URL::to($lingua. '/'. $campo[1].'/') }}
) I need to use
URL::action('ItemController@show', ($lingua. '/'. $campo[1].'/'))
3)链接我学习使用的页面:
URL::route('home')
但是当链接随着数据库条目(例如我的链接是{{ URL::to($lingua. '/'. $campo[1].'/') }}
)发生变化时如何做我需要使用
URL::action('ItemController@show', ($lingua. '/'. $campo[1].'/'))
EDIT:
编辑:
OK at the top of my pages there is a link to change language on the fly.
好的,在我的页面顶部有一个即时更改语言的链接。
<a href="{{URL::action('LanguageController@select', 'it-IT')}}"> Italian </a> //
<a href="{{URL::action('LanguageController@select', 'en-GB')}}"> English </a> //
<a href="{{URL::action('LanguageController@select', 'fr-FR')}}"> French </a>
I create a controller clled LanguageController
我创建了一个名为 LanguageController 的控制器
<?php
class LanguageController extends BaseController {
public function select($lingua)
{
// Store the current language in the session
Session::put('lingua', $lingua);
return Redirect::back(); // redirect to the same page, nothing changes, just the language
}
}
I create a route:
我创建了一条路线:
Route::get('lingua/{lingua}', 'LanguageController@select');
Route::get('/', array('as' => 'home', 'uses' => 'ItemController@menu'));
Route::get('/mondo/','ItemController@mondo');
Route::get('/{idcampo}','ItemController@show');
I have my ItemController@menu
我有我的 ItemController@menu
public function menu()
{ $linguadefault='it-IT';
$lingua = Session::get('lingua',$linguadefault);
$data = DB::table('campo')->lists('id');
return View::make('index')->with('campo',$data)->with('lingua',$lingua);
}
1) I don't understand why i need to route at lingua/{lingua} if i never route there but i use a url:action to a controller directly.
1) 我不明白为什么我需要在 lingua/{lingua} 路由,如果我从不路由到那里,但我直接使用 url:action 到控制器。
2) now i need to add
2)现在我需要添加
$linguadefault='it-IT';
$lingua = Session::get('lingua',$linguadefault);
at the beginning of every function to have a lingua variable in my page right?
在每个函数的开头在我的页面中有一个 lingua 变量,对吗?
3) now my language seems stucked to french and i can't change it anymore.
3)现在我的语言似乎坚持法语,我不能再改变它了。
回答by Antonio Carlos Ribeiro
I would not use the language in the URL all the time, you can just switch languages when you need and persist it:
我不会一直使用 URL 中的语言,您可以在需要时切换语言并保留它:
1) Use Session to persist the language chosen:
1) 使用 Session 来持久化选择的语言:
// Set the default language to the current user language
// If user is not logged, defaults to Italian
$linguaDefault = Auth::check()
? Auth::user()->lingua
: 'it-IT';
/// If not stored in Session, current language will be the default one
\App::setLocale(Session::get('lingua', $linguaDefault));
To have the language always set in your application, you can put this code in your file
要始终在您的应用程序中设置语言,您可以将此代码放在您的文件中
app/start/global.php
And you don't need to add this anywhere else. So it will use it in this order:
你不需要在其他任何地方添加它。所以它将按以下顺序使用它:
a) Language stored in Session (selected online)
b) Language user has in database
c) Italian
2) To change the language you create a route:
2) 要更改创建路线的语言:
Route::get('lingua/{lang}', 'LanguageController@select');
Your links
您的链接
URL::action('LanguageController@select', 'it-IT')
URL::action('LanguageController@select', 'en-GB')
URL::action('LanguageController@select', 'fr-FR');
And in your controller you just have to do:
在您的控制器中,您只需要执行以下操作:
public function select($lang)
{
// Store the current language in the session
Session::put('lingua', $lang);
return Redirect::back(); // redirect to the same page, nothing changes, just the language
}
3) This way you don't need your language in all your URLs, you don't have to deal with it in all your routes. If your user changes the language in database, you just:
3) 这样你就不需要在所有 URL 中使用你的语言,你不必在所有路由中处理它。如果您的用户更改了数据库中的语言,您只需:
$user->save();
Session::put('lingua', $user->lingua);
return Redirect::route('home'); // or anything else