2

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.

Arafat Rahman
  • 993
  • 5
  • 19
  • 46
  • Please add the error returned from B type routes to your question. Not sure if it matters but your routes shouldn't start with a forward slash. `doctor` not `/doctor`. – Jeemusu Jan 28 '20 at 07:23
  • @Jeemusu...I have updated my post. Please check. – Arafat Rahman Jan 28 '20 at 07:40
  • The buttons your clicking, what are the href="" value? Sounds like your not using an absolute path so the url is being appended to the current url. They should be `href="/locale/en/"`. – Jeemusu Jan 28 '20 at 07:50
  • I have updated my post with html button. – Arafat Rahman Jan 28 '20 at 08:11

1 Answers1

0

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>
Jeemusu
  • 10,415
  • 3
  • 42
  • 64
  • @Jeemusu.....if I am using forward slash into mu url's `{{ '/locale/en' }}` it gives another error `Object not found`....is it issue with the `.env` file ?....I have updated my post with `.env ` file. Please have a look – Arafat Rahman Jan 28 '20 at 09:09
  • The new error should tell you what line of what file the error is occurring? – Jeemusu Jan 29 '20 at 05:18
  • @Jeemusu....there is no error reporting file in `storage/logs`. Whenever I used forward slash in the url's then getting error `Object not found.The requested URL was not found on this server`..... – Arafat Rahman Jan 29 '20 at 06:23
  • It seems like your routes and URL's don't match up.. what is the base URL for your site? If it's `http://localhost:90/Office/sencare/`. Then you need to change your routes to reflect the `/Office/sencare` part of the URL. – Jeemusu Jan 29 '20 at 08:36
  • @Jeemusu...Your suggestion doesn't solve the issue brother ... :( – Arafat Rahman Jan 29 '20 at 09:01