1

I'm making a js localizator using service provider, and I need to get current locale to fetch current lang translations and pass to js. Everything works, but App::getLocale() keeps returning default app language.

I tried to do this using both middlware and view composer based on others issue threads in laracasts and stackoverflow, but nothing helps. Here's links

https://laracasts.com/discuss/channels/laravel/get-current-locale-in-app-service-provider

Getting locale language at provider class in Laravel

Laravel get getCurrentLocale() in AppServiceProvider

class JstranslateServiceProvider extends ServiceProvider 
{ 
    protected $langPath; 

    public function __construct() 
    {   
        $locale = App::getLocale();
        $this->langPath = resource_path('lang/'.$locale);
        dd($locale);
    }
}

dd($locale); output is always 'en', despite the current language.

I made js localization using this guide Link, it seems to be working for them

Avdey
  • 31
  • 2
  • 5

3 Answers3

0

Do this outside of a constructor in the Service Provider.

These classes are instantiated before Laravel does anything so it is likely whatever you have written in your middleware/view composer hasn't taken affect.

Instead you should be doing this either in the boot or register method.

George Hanson
  • 2,940
  • 1
  • 6
  • 18
  • Just tried in both functions. It's the same result `public function register() { $locale = App::getLocale(); dd($locale); }` – Avdey May 17 '19 at 08:23
  • And how are you setting the locale, can you share the code? – George Hanson May 17 '19 at 08:25
  • I also tried that with middleware and view composer – Avdey May 17 '19 at 08:25
  • Yes but can you please share the code you have written to do this? – George Hanson May 17 '19 at 08:30
  • `Route::get('locale/{locale}', function($locale){ Session::put('locale', $locale); return redirect()->back(); });` The route gets url from app.blade.php. I also tried to put there `App::setLocale($locale)` but it doesn't work either – Avdey May 17 '19 at 08:36
  • I made js localization using this guide, it seems to be working for them [link](https://pineco.de/using-laravels-localization-js/) – Avdey May 17 '19 at 09:01
0

Getting locale in boot and putting everything in composer solved my issue.

public function boot()
{   
    Cache::forget('translations');
    view()->composer("layouts.app", function () {
        $locale = App::getLocale();
        if($locale == 'us')
            $locale = 'en';
        $this->langPath = resource_path('lang/'.$locale);
        Cache::rememberForever('translations', function () {
            return collect(File::allFiles($this->langPath))->flatMap( function ($file) {
                return [
                    ($translation = $file->getBasename('.php')) => trans($translation),
                ];
            })->toJson(JSON_UNESCAPED_UNICODE);
        });
    });
}
Avdey
  • 31
  • 2
  • 5
0

I'm using Lumen version 5.8 and figured out how to get the current locale:

app('translator')->getLocale();
Hana Hasanah
  • 145
  • 1
  • 6