mercredi 9 mars 2022

Multiple language in laravel

I want to use two language in my web application for that i have created this dropdown in my view:

 @if ( Config::get('app.locale') == 'en')
 <li><a class="dropdown-item" href="">Arabic</a></li>
 @elseif ( Config::get('app.locale') == 'ar' )
 <li><a class="dropdown-item" href="">English</a></li>
 @endif

web route:

    Route::get('setlocale/{locale}', function ($locale) {
        if (in_array($locale, \Config::get('app.locales'))) {
          session(['locale' => $locale]);
        }
      
        return redirect()->back();
    });

 Route::get('contact', 'ContactController@index')->name('contact');
 Route::get('home', 'HomeController@index')->name('home');

config/app.php

'locale' => 'en',

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

Middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use App;
use Config;
use Session;
use Illuminate\Http\Request;

class Locale
{
    /**
   * Handle an incoming request.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \Closure  $next
   * @return mixed
   */
   public function handle($request, Closure $next)
   {
     
     //$raw_locale = Session::get('locale');
     $raw_locale = $request->session()->get('locale');
     if (in_array($raw_locale, Config::get('app.locales'))) {
       $locale = $raw_locale;
     }
     else $locale = Config::get('app.locale');
       App::setLocale($locale);
       return $next($request);
   }
}

when i click on dropdown the language changes but the url is still same i want on initial load of my application if url is testsite.com then it should be by default testsite.com/en if we visit on any page i want that language appended in the url like if i go to contact then it should be testsite.com/en/contact.Same language should be appended to url if i change from dropdown but presently language is not being appended. This language should be prefixed on frontend url only not on backend url (Admin). Any solution Thanks



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/fLVQo7l
via IFTTT

Aucun commentaire:

Enregistrer un commentaire