Naveed Ali
Naveed Ali

Reputation: 1045

laravel localization using mcamara package giving 404 error on root url but not other urls

i am trying to use mcamara package for language translation but on the root url i am getting 404 error. Actually i am trying to detect the ip address of the user and then set the locale according to that country. i stored the locales and country names in the database. Below is my code:

AppServiceProvider.php in this file i am fetching user ip address through https://github.com/stevebauman/location this package and checking the locale from database and set the locale according to that.

        $ip = request()->getClientIp(); 
        $position = Location::get($ip); 
        $locale = Locale::where('country_code',strtolower($position->countryCode))->first();
        if($locale){
            LaravelLocalization::setLocale($locale->country_code);
            
        }

Web.php

Route::group([
'prefix' => LaravelLocalization::getCurrentLocale(),
'middleware' => ['localizationRedirect', 'localeViewPath' ]], function(){
   Route::get('/',function(){
     dd('check');
  }); 
}

i am getting 404 error.

Upvotes: 1

Views: 2962

Answers (4)

Ahmad Al-Tarifi
Ahmad Al-Tarifi

Reputation: 1

Tried this and worked for me, deleted the cache folder content, bootstrap\cache

Head to your projects files, Then go to bootstrap folder, then open cache folder, you will find many files there, example: ( routes.php ), ( routes.php (1) ). You need to delete these files and the localization will return to work. These files generated when you run these commands: php artisan optimize php artisan optimize:clear

SO DON'T RUN THESE WHEN USING MCAMARA

Upvotes: 0

Mohamed farh
Mohamed farh

Reputation: 15

go to bootstrap folder and delete (config.php) file and if there is any file called (route.php) delete it too from bootstrap folder.

Upvotes: -2

Farrux Choriyev
Farrux Choriyev

Reputation: 329

https://github.com/mcamara/laravel-localization/blob/master/CACHING.md

this url help to you. use \Mcamara\LaravelLocalization\Traits\LoadsTranslatedCachedRoutes; trait in RouteServiceProvider this problem at route:trans:cache instead of route:cache

Upvotes: 0

hai_shar
hai_shar

Reputation: 99

this steps works for me

Route::group(['prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]],function (){
// routes here

});

then 1 - remove cache files in bootstrap folder 2- php artisan optimize 3- php artisan route:trans:cache 4- php artisan cache:clear 5- php artisan route:clear

Upvotes: 5

Related Questions