Formatink's avatar

Use the localisation of Laravel for Carbon

Hello,

I would like to use the localisation of Laravel, which is set in the app.php file, for Carbon.

I tried but it does not work. (config/app.php)

<?php
...
'locale' => 'fr',
...

(routes.php)

<?php
echo Carbon::now()->format('l j F Y H:i:s');
echo (string) (new Carbon($news[0]->created_at))->formatLocalized('%A %d %B %Y'); // this instruction works with setlocale(LC_TIME, 'French');
?>

These two instructions return the date in English (instead of French) despite the configuration of "local" on "fr".

Can you help me ? Thank you in advance.

Regards, Formatink

0 likes
19 replies
toniperic's avatar

@Formatink try this

Carbon::setLocale('fr')->now()->format('l j F Y H:i:s');

or if you want to leverage the app.locale key then you can do

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

Hope I could help.

3 likes
Formatink's avatar

Thanks for your answer.

Carbon::setLocale('fr')->now()->format('l j F Y H:i:s');

It does not work because the method setLocale return null.

setlocale(LC_TIME, 'fr');

It works only for the second instruction (it's strange)

I wonder if there is a way to translate without adding an instruction (and use the configuration of Laravel instead)

toniperic's avatar

You can try

setlocale(LC_TIME, config('app.locale'))
4 likes
Formatink's avatar

Yes, it works (for what I want to do).

But why it does not work without this instruction ?

toniperic's avatar

@Formatink because nowhere in the request lifecycle does Laravel make a call to PHP function setlocale() with app.locale key as its parameter.

It uses the app.locale key elsewhere.

3 likes
Formatink's avatar

@toniperic Ha, ok. How could I make a proper call to setlocale(LC_TIME, config('app.locale')) in the request lifecycle ?

Should we also make a pull request for this ?

toniperic's avatar

No need to submit a PR for this.

You could do it anywhere. You could make a service provider for it, or place it within AppServiceProvider.

saamretep's avatar

I would like to set the locale at one place as I use carbon diffForHumans at several places in my application. Can anyone tell what the best solution is? Where should I place Carbon::setlocale()?

In the AppServiceProvider

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

7 likes
arielcalcano's avatar

@saamretep Why not add this in the register method of your service provider?

This works for my Laravel package

in MyPackageServiceProvider.php:

    public function register()
    {
        setlocale(LC_TIME, config('app.locale'));
    }

Then in all my views just use:

$post->created_at->formatLocalized('%d de %B %Y')

Unfortunately the base class DateTime does not have any localization support. To begin localization support a formatLocalized($format) method was added.

4 likes
kornel's avatar

In my Laravel 5.6 when I try

dd(Carbon::setLocale('fr')->now()->format('l j F Y H:i:s'));

it gives me an error

Call to a member function now() on boolean

1 like
Cronix's avatar

@kornel Yes, because Carbon::setLocale() returns a boolean based on if it was successful or not. It doesn't return a carbon instance. So you just need to break it up into 2 different steps instead of a single call. 1) set local 2) use carbon as normal.

Ideally, you'd be setting Carbon::setLocale() in a single place in your app, like the AppServiceProvider, so all Carbon usage would be reflected in that locale instead of having to set it each time.

See the carbon docs for more info https://carbon.nesbot.com/docs/#api-localization

COTIGA's avatar

Since Carbon 2:

@php
    Carbon::setlocale(config('app.locale'));
@endphp

{{ Carbon::now()->translatedFormat('l j F Y H:i:s') }}
21 likes

Please or to participate in this conversation.