如何在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);
});

解决方法

我想根据所选语言创建具有许多翻译路线的应用程序。我曾经在3种在多语言网站中创建URL的方法中对此进行了描述。

在这种情况下,它应该是 提到的主题的第一个方法, 因此:

  1. 我有一种默认语言
  2. 我可以有许多其他语言
  3. 当前语言应仅通过URL计算(不包含cookie /会话),以使其对搜索引擎也非常友好
  4. 对于默认语言,URL中不应包含前缀,对于其他语言,应在域后添加语言前缀
  5. url的每个部分都应根据当前语言进行翻译。

假设我设置了默认语言,pl并设置了2种其他语言enfr。我只有3页-主页,联系页和关于页。

网站的网址应如下所示:

/
/[about]
/[contact]
/en
/en/[about]
/en/[contact]
/fr
/fr/[about]
/fr/[contact]

[about][contact]应根据所选语言的翻译,例如英语应该留下contact,但波兰应该kontakt等等。

如何做到尽可能简单?