Laravel set default language not working
Asked Answered
A

5

6

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

Americana answered 13/12, 2017 at 1:38 Comment(9)
did you clear the 'config' cache? did you set 'useAcceptLanguageHeader' correctly?Cooler
@Cooler yes i cleared the config cache nothing changed. I don't have accept language header right now. But i want to force using de languageAmericana
so you didn't touch the config file for this package?Cooler
@Cooler I just uncommitted the required language in config/laravellocalization.phpAmericana
and how about the other vars in that config fileCooler
@Cooler nothing changed just uncommitted the 'de' lineAmericana
so how about the other vars with the comments that tell you what they are for ... any of them sound like they are important?Cooler
@Cooler yes i think there was one that sound like it's important :) 'useAcceptLanguageHeader' => true, Changed to false , issue solvedAmericana
;) ... ill post that upCooler
C
9

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.

Cooler answered 13/12, 2017 at 2:4 Comment(2)
you saved my day!Dipterous
how to search file?Slovene
P
4

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

Penetrate answered 30/1, 2021 at 20:49 Comment(0)
E
1

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

public function __construct(){}
Extractive answered 16/4, 2019 at 15:14 Comment(2)
This answer is under rated. I am using laravel 11 and none above worked out, the moment I saw that notorious :) in the answer I was like "Oh! in request life cycle the controller class is being instantiated (which is why controller functions are not static) hence you can use docs to compelete it thx :))Physoclistous
If you are using [form request validation](laravel.com/docs/11.x/validation#form- request-validation) you can add this constructor to that class as well.Physoclistous
C
1

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'));

Concavoconcave answered 10/3, 2022 at 9:20 Comment(0)
S
0

//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');
    }
});

Springtail answered 23/4, 2022 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.