mcamara / laravel-localization

Easy localization for Laravel
MIT License
3.38k stars 515 forks source link

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

Open Naveedali1234 opened 4 years ago

Naveedali1234 commented 4 years ago

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 on www.example.com but when i do www.example.com/en then it's working fine. Any Help would be really appreciated. i am stuck in it from few days.

Mohamed-Farh commented 2 years ago

go to your bootstrap folder and delete the ( config.php ) and if there is a file called route in the bootstrap folder delete it too

coder2318 commented 2 years ago

php artisan route:trans:clear

royallife88 commented 2 years ago

@Naveedali1234 I am also trying the same. But no success. Any solution you found?

mohamedalwhaidi commented 2 years ago

Same error

muhammadelalfy commented 1 year ago

check your RouteServiceProvider if there are prfix assigned to your routes remove it .. if you added a prefix and need it .. move it to routes file itself .. 'prefix' => LaravelLocalization::setLocale() ... this prefix should be the first prefix

ricardofreeman commented 1 year ago

In case this will help anyone in the future, routes must not be cached so if you run php artisan route:cache or php artisan optimize locally or after deploying to the server, you will get the 404.

kevin4dhd commented 1 year ago

any solution?

AhmedFawzy commented 5 months ago

Just follow the guide

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