简体   繁体   中英

Laravel 5.4 : Translate routes

is there a simple way to translate my routes in Laravel 5.4. My translation files located in here:

/resources
    /lang
        /en
            routes.php
        /de
            routes.php

In my web.php i define my routes like this:

Route::get('/{locale}', function ($locale) {
    App::setLocale($locale);

    return view('welcome');
});

Route::get('{locale}/contact', 'ContactController@index');

I have found very elaborate solutions or solutions for Laravel 4. I am sure that Laravel has also provided a simple solution. Can someone explain to me the best approach?

Thanks.

we usually do it like this

to get the current language:

$request = request()->segment(1);
$language = null;
if (!empty($request) && in_array($request,config('translatable.locales'))) {
  $language = $request;
  App::setLocale($language);
} else {
    $language = 'nl';
}

routes:

Route::group(['prefix' => $language], function () {
    Route::get(trans('routes.newsletter'), array('as' => 'newsletter.index', 'uses' => 'NewsletterController@index'));

I created a file translatable.php in my config folder:

<?php

return [

    'locales' => ['en', 'de'],

];

web.php:

$request = request()->segment(1);
$language = null;

if (!empty($request) && in_array($request,config('translatable.locales'))) {
    $language = $request;
    App::setLocale($language);
} else {
    $language = 'de';
}

Route::get('/', function() {
    return redirect()->action('WelcomeController@index');
});

Route::group(['prefix' => $language], function () {

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

    Route::get('/',
        array(  'as'      => 'welcome.index',
                'uses'    => 'WelcomeController@index'));


    Route::get(trans('routes.contact'),
        array('as'      => 'contact.index',
              'uses'    => 'ContactController@index'));

});

Works fine - Thanks. Is the redirect also the best practice? Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM