Arafat Rahman
Arafat Rahman

Reputation: 1015

Localization isn't working for all URL in Laravel

I have applied Localization in my Laravel project. But Localization is getting error for some URL.

I have two languages buttons en and bn in my head section. while user click any of the language button, the whole site will be converted into that language.

button

<li><a href="{{ 'locale/en' }}">English</a></li>
<li><a href="{{ 'locale/bn' }}">Bangla</a></li>

.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Dm34MLg8AbQk4ADyIG9cYPaIwYbQgrUgrN7Ani/x+JA=
APP_DEBUG=true
APP_URL=http://localhost:90/office/sencare/

web.php

Route::get('/doctor', 'homeController@doctor'); //route A
Route::get('/doctor/{data}', 'homeController@doctor_detail'); //route B
Route::get('/technology', 'homeController@technology');//route A
Route::get('/technology/{data}', 'homeController@technology_detail'); //route B

Route::group(['prefix' => 'admin'], function () {
    Voyager::routes();
});

// ================LOCALIZATION=============
Route::get('locale/{locale}',function ($locale){
    Session::put('locale',$locale);
    return redirect()->back();
});
// ================LOCALIZATION=============

/*=============START CUSTOMIZE ERROR PAGE===========*/
Route::any('{catchall}', function() {  
  return App::call('App\Http\Controllers\errorController@error');

})->where('catchall', '.*');
/*=============END CUSTOMIZE ERROR PAGE===========*/

Localization Middlewer

public function handle($request, Closure $next)
    {
        if(\Session::get('locale')){

            \App::setLocale(\Session::get('locale'));

        }

        return $next($request);
    }

So while user visit pages under A type routes and click on any language button to convert the page, it's working fine and converted.

But while user visit pages under B type routes and click on any language button to convert the page, then got an error page under customize error routes.

error message

PAGE NOT FOUND The requested URL is not correct.

Another point is that while I clicked upon language button under A type route page the url remaining same. But for B type route page after clicking the language button the url changed. example :

http://localhost:90/Office/sencare/doctor/locale/en

this locale/en/ isn't come for any other URL.

How to solve this ?

Anybody help please ? Thanks in advance.

Upvotes: 3

Views: 1904

Answers (1)

Jeemusu
Jeemusu

Reputation: 10533

From the HTML code you posted I can see that your language buttons are using a relative url path. This means the path does not begin with a forward slash.

Relative paths append the url to the current pages url. For example, if a user on the http://localhost:90/Office/sencare/doctor/ page clicks the Bangla link, they will be taken to http://localhost:90/Office/sencare/doctor/locale/bn.

Instead of using a relative path for your url's you should change them to be absolute paths by adding a forward slash to the front of them.

<li><a href="{{ '/locale/en' }}">English</a></li>
<li><a href="{{ '/locale/bn' }}">Bangla</a></li>

Upvotes: 0

Related Questions