Laravel 5 Carbon Global Locale

Laravel 5 Carbon global Locale

So this is my bad, Carbon is actually using the php

setlocale();

the

Carbon::setLocale('fr')

method is only for the

->diffForHumans()

method.
Notice that the php setlocale() reference to the locale stored on your OS
to choose one of the installed one use

locale -a

on your console

secondly, you have to use

->formatLocalized()

method instead of

->format()

method

and lastly all the usefull methods like

->toDateString()
->toFormattedDateString()
->toTimeString()
->toDateTimeString()
->toDayDateTimeString()

are not being localized

and lastly you have to use these parsing letters

http://php.net/manual/en/function.strftime.php

Carbon setLocale not working Laravel

You might want to use setLocale(LC_TIME, $this->app->getLocale()) somewhere at the start of your application.

Then if you wish to have the localized date format with local names use the formatLocalized function

Carbon::now()->formatLocalized('%d %B %Y');

See http://php.net/manual/en/function.strftime.php for parameter for formatting

Wrong locale for dates in Laravel app in production on Heroku

It simply means Heroku does not have it locale installed on the OS so LC_TIME won't work and formatLocalized neither. Hopefully, Carbon has its own translations with even more formats supported than formatLocalized (which actually rely on OS locales installed). See ->isoFormat() in https://carbon.nesbot.com/docs/#api-localization

{{ $user->dateBirth ? Carbon\Carbon::parse($user->dateBirth)->isoFormat('DD/MMM/YYYY') : ''}}

My recommandation is to always use ->isoFormat() so your code works on any machine with no extra install/setting, and those can be customized so you can change the Carbon translations without impacting the rest.

Laravel : How to localize dates within views with Carbon

You need to use the php function setlocale before setting the localized format in Carbon.

Unfortunately the base class DateTime does not have any localization support. To begin localization support a formatLocalized($format) method was added. The implementation makes a call to strftime using the current instance timestamp. If you first set the current locale with PHP function setlocale() then the string returned will be formatted in the correct locale.

Examples from the docs:

setlocale(LC_TIME, 'German');
echo $dt->formatLocalized('%A %d %B %Y'); // Mittwoch 21 Mai 1975
setlocale(LC_TIME, '');
echo $dt->formatLocalized('%A %d %B %Y'); // Wednesday 21 May 1975

How to set language for Carbon?

try using PHP function setlocale also check if your hosting allows and gives you the locales you want.

 setlocale(LC_TIME, 'es_ES');
Carbon::setLocale('es');
$archive_current_year = Articles::whereBetween('created_at', [
....

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

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


Related Topics



Leave a reply



Submit