Thomas Charlesworth
Thomas Charlesworth

Reputation: 1839

Laravel 5.4 Multi Language Locale not working in production server

I'm not getting any errors when switching languages on my production server.

I can see the session value being changed as the flag on my navbar changes but setting the locale isn't working.

Everything works fine on localhost though.

Im not quite sure where to approach this problem from so I'd really appreciate it if someone can point me in the right direction.

Upvotes: 0

Views: 2497

Answers (3)

Thomas Charlesworth
Thomas Charlesworth

Reputation: 1839

My site supported two locales, "en" and "th"

The issue was that I was setting the session('lang') key to "EN" and "TH".

On my mac environment, things like these are not case sensitive and therefore locales worked properly on my machine environment.

However, on a live production server which likely is running on Linux etc, it is case sensitive.

I fixed the issue by now setting the session key "lang" to lower case values of the Locales.

Upvotes: 4

Peter Reshetin
Peter Reshetin

Reputation: 925

It looks like you need to clear config cache at production:

php artisan config:cache

This came to my mind when I read your comment:

Yes. Apparently, I played around with the config and the locale is always falling to the fallback_locale section of the config file app.php. I tried setting fallback_locale to my second language and the site did infact change language. But I was then unable to switch back to the first language.

Upvotes: 6

Sérgio Reis
Sérgio Reis

Reputation: 2523

The example I talked in the comment is this

use Closure, Session, Auth, App;

class LocaleMiddleware {


    public function handle($request, Closure $next){

        if(Auth::user() ){
          App::setlocale(Auth::user()->lang);
        }elseif($locale = Session::has('lang')){
          $local = Session::get('lang');
          App::setlocale($local);
        }
        return $next($request);
    }
}

Upvotes: 0

Related Questions