Laravel-路由和语言代码

Kha*_*mad 1 laravel-4

我需要在网址中添加语言代码,但当它是默认代码时则不需要。

这是routes.php文件中的代码Laravel 4.2

我需要像这样的根结构:

默认语言=> http://website.com/RegistrationStep1
其他语言=> http://website.com/language/RegistrationStep1

Route::group(['prefix' => '{lang?}', 'before' => 'localization'], function()
{

    Route::get('/', function() {
        return View::make('hello');
    });

    Route::get('registration/step1', 'RegistrationController@getRegistrationStep1');

    Route::post('registration/step1', 'RegistrationController@postRegistrationStep1');
});
Run Code Online (Sandbox Code Playgroud)

在没有url语言参数的情况下调用url时出现错误

Hie*_* Le 5

首先,定义您的可用语言:

# in app/config/app.php
'available_locales' => array('de', 'en', 'es', 'fr', 'it'),
Run Code Online (Sandbox Code Playgroud)

在将routes.php前缀注册到路由组之前,请检查当前URI的第一段是否是有效的语言快捷方式。

$locale = Request::segment(1);

if (in_array($locale, Config::get('app.available_locales'))) {
    \App::setLocale($locale);
} else {
    $locale = null;
}

Route::group(array('prefix' => $locale), function()
{

    //your routes here

});
Run Code Online (Sandbox Code Playgroud)

参见链接http://learninglaravel.net/language-switching-in-laravel-4/link

您也可以使用此软件包执行任务:mcamara / laravel-localization