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

问题描述 投票:42回答:3

我想根据所选语言创建具有许多翻译路线的应用程序。我曾经在3 methods of creating URLs in multilingual websites上描述过它。

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

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

让我们假设我已经设置了默认语言pl和其他两种语言enfr。我只有3页 - 主页,联系页面和关于页面。

网站的网址应该是这样的:

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

[about][contact]应根据选定的语言进行翻译,例如在英语中应该留下contact但是对于波兰语应该是kontakt等等。

如何尽可能简单地完成?

php laravel laravel-4 localization routing
3个回答
67
投票

第一步:

转到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的第一段是否与您的语言名称匹配 - 如果是,则更改语言环境和当前语言前缀。

然后在微小的循环中,为所有路径名称设置要求(您提到您希望在URL中翻译aboutcontact),因此在此处将它们设置为与当前语言的routes.php文件中定义的相同。

最后你创建路由组,其前缀与你的语言相同(默认语言为空)和内部组你只需创建路径,但那些参数aboutcontact你视为variables所以你使用{about}{contact}语法他们。

您需要记住,在这种情况下,将检查所有路径中的{contact}是否与您在当前语言的第一步中定义的相同。如果您不想要这种效果,并且想要使用where手动为每条路线设置路线,那么可以选择没有循环的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'));


});

第四步:

你没有提到它,但你可以考虑另外一件事。如果有人会使用url /en/something,其中something不正确Route,我认为是重定向的最佳解决方案。但你应该重定向不到/,因为它是默认语言,但对/en

所以现在你可以打开app/start/global.php文件并在这里​​为未知网址创建301重定向:

// app/start/global.php

App::missing(function()
{
   return Redirect::to(Config::get('app.locale_prefix'),301);
});

22
投票

Marcin Nabiałek在他的初步答案中为我们提供的是路线定位问题的可靠解决方案。

小Bugbear:

他的解决方案唯一真正的缺点是我们不能使用缓存路由,根据Laravel's docs,这有时会有很大的好处:

如果您的应用程序专门使用基于控制器的路由,则应该利用Laravel的路由缓存。使用路由缓存将大大减少注册所有应用程序路由所需的时间。在某些情况下,您的路线注册甚至可能会快100倍。要生成路由缓存,只需执行route:cache Artisan命令即可。


为什么我们不能缓存我们的路线?

因为Marcin Nabiałek's方法动态地基于locale_prefix生成新路由,所以在访问缓存时访问未存储在404变量中的任何前缀时缓存它们将导致locale_prefix错误。


我们保留什么?

基础似乎非常坚实,我们可以保留它的大部分!

我们当然可以保留各种特定于本地化的路径文件:

<?php

// app/lang/pl/routes.php

return array(

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

我们还可以保留所有app/config/app.php变量:

/**
* Default locale 
*/
'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' => '',

 /**
 * Let's also add a all_langs array
 */
'all_langs' => array ('en', 'fr', 'pl'),

我们还需要一些检查路由段的代码。但由于这一点是利用缓存我们需要将它移到routes.php文件之外。一旦我们缓存路由,那个将不再使用。我们暂时可以将其移至app/Providers/AppServiceProver.php,例如:

public function boot(){
  /*
   *  Set up locale and locale_prefix if other language is selected
   */
   if (in_array(Request::segment(1), config('app.alt_langs'))) {
       App::setLocale(Request::segment(1));
       config([ 'app.locale_prefix' => Request::segment(1) ]);
   }
}

别忘了:

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\App;

设置我们的路线:

我们的app/Http/routes.php文件中会发生一些变化。

首先,我们必须创建一个包含所有alt_langs以及默认locale_prefix的新数组,这很可能是''

$all_langs = config('app.all_langs');

为了能够使用已转换的路由参数缓存所有各种lang前缀,我们需要将它们全部注册。我们怎么做?

*** Laravel aside 1: ***

我们来看看Lang::get(..)的定义:

public static function get($key, $replace = array(), $locale = null, $fallback = true){
      return \Illuminate\Translation\Translator::get($key, $replace, $locale, $fallback);
}

该函数的第三个参数是$locale变量!太棒了 - 我们当然可以利用它!这个函数实际上让我们选择我们想要从哪个语言环境中获取翻译!

接下来我们要做的是迭代$all_langs数组并为每个语言前缀创建一个新的Route组。不仅如此,我们还将摆脱我们以前需要的where链和patterns,并且只注册具有正确翻译的路线(其他人将抛出404而不必再检查它):

/**
* Iterate over each language prefix 
*/
foreach( $all_langs as $prefix ){

   if ($prefix == 'pl') $prefix = '';

   /**
   * Register new route group with current prefix
   */
   Route::group(['prefix' => $prefix], function() use ($prefix) {

         // Now we need to make sure the default prefix points to default  lang folder.
         if ($prefix == '') $prefix = 'pl';

         /**
         * The following line will register:
         *
         * example.com/
         * example.com/en/
         */
         Route::get('/', 'MainController@getHome')->name('home');

         /**
         * The following line will register:
         *
         * example.com/kontakt
         * example.com/en/contact
         */
         Route::get(Lang::get('routes.contact',[], $prefix) , 'MainController@getContact')->name('contact');

         /**
         * “In another moment down went Alice after it, never once 
         * considering how in the world she was to get out again.”
         */
         Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function () use ($prefix){

            /**
            * The following line will register:
            *
            * example.com/admin/uzivatelia
            * example.com/en/admin/users
            */
            Route::get(Lang::get('routes.admin.users',[], $prefix), 'AdminController@getUsers')
            ->name('admin-users');

         });
   });
}

/**
* There might be routes that we want to exclude from our language setup.
* For example these pesky ajax routes! Well let's just move them out of the `foreach` loop.
* I will get back to this later.
*/
Route::group(['middleware' => 'ajax', 'prefix' => 'api'], function () {
    /**
    * This will only register example.com/api/login
    */
    Route::post('login', 'AjaxController@login')->name('ajax-login');
});

休斯顿,我们有一个问题!

正如你所看到的,我更喜欢使用命名路线(大多数人可能会这样做):

Route::get('/', 'MainController@getHome')->name('home');

它们可以非常容易地在您的刀片模板中使用:

{{route('home')}}

但到目前为止我的解决方案存在问题:路由名称相互覆盖。上面的foreach循环只会注册带有名称的最后一个前缀路由。

换句话说,只有example.com/将被绑定到home路线,因为locale_perfix$all_langs阵列中的最后一项。

我们可以通过使用语言$prefix为路由名称添加前缀来解决这个问题。例如:

Route::get('/', 'MainController@getHome')->name($prefix.'_home');

我们必须为循环中的每个路由执行此操作。这造成了另一个小障碍。


但是我的大项目差不多完成了!

好吧,你可能已经猜到了你现在必须回到你的所有文件,并使用从route配置加载的当前locale_prefix为每个app辅助函数调用添加前缀。

除了你没有!

*** Laravel aside 2: ***

让我们来看看Laravel如何实现它的route辅助方法。

if (! function_exists('route')) {
    /**
     * Generate a URL to a named route.
     *
     * @param  string  $name
     * @param  array   $parameters
     * @param  bool    $absolute
     * @return string
     */
    function route($name, $parameters = [], $absolute = true)
    {
        return app('url')->route($name, $parameters, $absolute);
    }
}

正如您所看到的,Laravel将首先检查是否存在route函数。只有当另一个功能尚不存在时,它才会注册其route功能!

这意味着我们可以非常轻松地解决我们的问题,而无需重写目前在我们的route模板中进行的每个Blade调用。

让我们快速制作一个app/helpers.php文件。

让我们确保Laravel在加载helpers.php之前通过在bootstrap/autoload.php中添加以下行来加载文件

//Put this line here
require __DIR__ . '/../app/helpers.php';
//Right before this original line
require __DIR__.'/../vendor/autoload.php';

我们现在要做的就是在我们的route文件中制作我们自己的app/helpers.php函数。我们将使用原始实现作为基础:

<?php
//Same parameters and a new $lang parameter
use Illuminate\Support\Str;

function route($name, $parameters = [], $absolute = true, $lang = null)
{
    /*
    * Remember the ajax routes we wanted to exclude from our lang system?
    * Check if the name provided to the function is the one you want to
    * exclude. If it is we will just use the original implementation.
    **/
    if (Str::contains($name, ['ajax', 'autocomplete'])){
        return app('url')->route($name, $parameters, $absolute);
    }

   //Check if $lang is valid and make a route to chosen lang
   if ( $lang && in_array($lang, config('app.alt_langs')) ){
       return app('url')->route($lang . '_' . $name, $parameters, $absolute);
   }

    /**
    * For all other routes get the current locale_prefix and prefix the name.
    */
    $locale_prefix = config('app.locale_prefix');
    if ($locale_prefix == '') $locale_prefix = 'pl';
    return app('url')->route($locale_prefix . '_' . $name, $parameters, $absolute);
}

而已!

所以我们所做的基本上是注册所有可用的前缀组。创建了每个翻译的路线,并且其名称也带有前缀。然后重写Laravel route函数,将所有路径名称(除了一些)添加到当前locale_prefix前面,以便在我们的刀片模板中创建适当的URL,而不必每次都键入config('app.locale_prefix')

哦耶:

php artisan route:cache

只有在部署项目后才能真正完成缓存路由,因为在开发过程中很可能会弄乱它们。但您可以随时清除缓存:

php artisan route:clear

再次感谢Marcin Nabiałek的原始答案。这对我很有帮助。


1
投票

使用更简单的方法可以应用相同的结果..不完美,但确实提供了快速简便的解决方案。但是,在这种情况下,您必须编写每个路由,以便它可能不会为大型网站执行此操作。

Route::get('/contact-us', function () {
    return view('contactus');
})->name('rte_contact'); // DEFAULT

Route::get('/contactez-nous', function () {
    return view('contactus');
})->name('rte_contact_fr');

只需在本地化文件中定义路由名称,如下所示:

# app/resources/lang/en.json
{ "rte_contact": "rte_contact" } //DEFAULT

// app/resources/lang/fr.json
{ "rte_contact": "rte_contact_fr" }

然后,您可以使用生成的区域设置变量在刀片模板中使用它们,如下所示:

<a class="nav-link" href="{{ route(__('rte_contact')) }}"> {{ __('nav_contact') }}</a>
© www.soinside.com 2019 - 2024. All rights reserved.