'Laravel set default language not working

I have changed the default website localization in confing/app.php to be (de)

'locale' => 'de',
'fallback_locale' => 'de',

But still website is loading in 'en' localization .

I tried clearing the view cache and cache but nothing changed .

I am using https://github.com/mcamara/laravel-localization

The de will be activate only when i use the URL domainname.com/de



Solution 1:[1]

In the scenario to not be using the dynamic locale in the URL it seems you will have to adjust the configuration file for this package:

'useAcceptLanguageHeader' => false,

From the comments for that variable:

"Negotiate for the user locale using the Accept-Language header if it's not defined in the URL? If false, system will take app.php locale attribute"

Seems to get it to use that default you have to set this to false.

You could play with the other variable:

'hideDefaultLocaleInURL' => true,

to see how that adjusts the default behavior as well.

Solution 2:[2]

i had the same proplem go to laravellocalization.php in config

and make

'useAcceptLanguageHeader' => false,

and then run

php artisan config:clear

and it will work

Solution 3:[3]

try to add __construct() function to your controller. :)

public function __construct(){}

Solution 4:[4]

this way is best for me

app/Providers/AppServiceProvider.php

in boot() function write these lines

session()->put('locale', 'ar');

app()->setLocale(session()->get('locale'));

Solution 5:[5]

//To make the default website with the language AR.

//At the first time, the user does not have any session. So, we will add a

//session and set the language to be AR if it is not the first time.

//Then, depending on the language you choose.

//In the web.php

Route::get('/', function () {
    if (Session::get('language') == 'en') {
        Session::put('language', 'en');
        App::setLocale('en');
        return redirect('main-services/1/1');
    } else {
        Session::put('language', 'ar');
        App::setLocale('ar');
        return redirect('main-services/1/1');
    }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lagbox
Solution 2 malah
Solution 3 Ayman Elshehawy
Solution 4 Muhammad Umar
Solution 5 Hassan Joseph