您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Laravel中创建多语言翻译的路线

如何在Laravel中创建多语言翻译的路线

转到app/lang目录并在此处为每种语言的路线创建翻译。您需要创建3个routes.PHP文件-每个文件都位于单独的语言目录(pl / en / fr)中,因为您要使用3种语言

波兰语:

<?PHP

// app/lang/pl/routes.PHP

return array(

    'contact' => 'kontakt',
    'about'   => 'o-nas'
);

对于英语:

<?PHP

// app/lang/en/routes.PHP

return array(
    'contact' => 'contact',
    'about'   => 'about-us'
);

对于法语:

<?PHP

// app/lang/fr/routes.PHP

return array(
    'contact' => 'contact-fr',
    'about'   => 'about-fr'
);

转到app/config/app.PHP文件

您应该找到以下行:

'locale' => 'en',

并将其更改为应该是您的主要站点语言的语言(在您的情况下为波兰语):

'locale' => 'pl',

您还需要将以下行放入此文件

/**
 * List of alternative languages (not including the one specified as 'locale')
 */
'alt_langs' => array ('en', 'fr'),

/**
 *  Prefix of selected locale  - leave empty (set in runtime)
 */
'locale_prefix' => '',

alt_langs配置设置可选的语言(在你的情况enfr) -他们应该是一样的,从第一步骤中你的翻译创建的文件文件名。

并且locale_prefix是您的语言环境的前缀。您不需要认语言环境的前缀,因此将其设置为空字符串。如果选择认语言以外的其他语言,则会在运行时修改此配置。

转到您的app/routes.PHP文件并放入其内容(即app/routes.PHP文件的全部内容):

<?PHP

// app/routes.PHP

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/


/*
 *  Set up locale and locale_prefix if other language is selected
 */
if (in_array(Request::segment(1), Config::get('app.alt_langs'))) {

    App::setLocale(Request::segment(1));
    Config::set('app.locale_prefix', Request::segment(1));
}


/*
 * Set up route patterns - patterns will have to be the same as in translated route for current language
 */
foreach(Lang::get('routes') as $k => $v) {
    Route::pattern($k, $v);
}


Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
    Route::get(
        '/',
        function () {
            return "main page - ".App::getLocale();
        }
    );


    Route::get(
        '/{contact}/',
        function () {
            return "contact page ".App::getLocale();
        }
    );



    Route::get(
        '/{about}/',
        function () {
            return "about page ".App::getLocale();

        }
    );

});

如您所见,首先检查url的第一段是否与您的语言名称匹配-如果是,则更改语言环境和当前语言前缀。

然后在小循环中,设置所有路由名称的要求(您提到了要拥有的名称aboutcontact转换为URL),因此在此处将其设置为与routes.PHP文件中为当前语言定义的名称相同。

最后,您创建将有前缀相同的语言(认语言它将是空的),你只需创建路径,但这些参数内组路由组aboutcontact你当作variables让你用{about}{contact}语法他们。

您需要记住,在这种情况下{contact},将检查所有路径是否与您在第一步中为当前语言定义的路径相同。如果你不想要这个效果,并希望使用手动设置路线每条路线的,有替代app\routes.PHP不循环,您可以设置文件contactabout各条路线分别:

<?PHP

// app/routes.PHP

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

/*
 *  Set up locale and locale_prefix if other language is selected
 */
if (in_array(Request::segment(1), Config::get('app.alt_langs'))) {

    App::setLocale(Request::segment(1));
    Config::set('app.locale_prefix', Request::segment(1));
}


Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
    Route::get(
        '/',
        function () {
            return "main page - ".App::getLocale();
        }
    );


    Route::get(
        '/{contact}/',
        function () {
            return "contact page ".App::getLocale();
        }
    )->where('contact', Lang::get('routes.contact'));



    Route::get(
        '/{about}/',
        function () {
            return "about page ".App::getLocale();

        }
    )->where('about', Lang::get('routes.about'));


});

您没有提到它,但是您可以考虑另外一件事。如果有人/en/somethingsomething不正确的路由处使用url ,我认为是进行重定向的最佳解决方案。但是,您不应该重定向到,/因为它是认语言,而是重定向/en

因此,现在您可以打开app/start/global.PHP文件并在此处为未知网址创建301重定向

// app/start/global.PHP

App::missing(function()
{
   return Redirect::to(Config::get('app.locale_prefix'),301);
});
其他 2022/1/1 18:20:02 有390人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶