mikro
mikro

Reputation: 125

Where to put Carbon::setLocale() in Laravel 5.5?

I can't find out where the Carbon localization config should be stated, to be used globally.

\Carbon\Carbon::setLocale(config('app.locale'));

Where?

Upvotes: 1

Views: 1875

Answers (2)

Davide Casiraghi
Davide Casiraghi

Reputation: 18117

I found out that to set the locale for Carbon in the App Service Providers I need to use a View composer. Otherwise it was not possible to make the locale available for my directives.

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *    
     * @return void
     */
    public function boot()
    {
       View::composer('*', function ($view) {

            $locale = App::getLocale();

            \Carbon\Carbon::setUtf8(true);
            \Carbon\Carbon::setLocale($locale);
        });
    }
}

Here some more details about View Composer.

Upvotes: 0

Prince Lionel N'zi
Prince Lionel N'zi

Reputation: 2588

Go to AppServiceProvider.php and add it to the boot method

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *    
     * @return void
     */
    public function boot()
    {
        \Carbon\Carbon::setLocale(config('app.locale'));
    }
}

Upvotes: 2

Related Questions