Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set language for Carbon?

So i want to set language for Carbon, but i always get same result.

  Carbon::setLocale('es');
$archive_current_year = Articles::whereBetween('created_at', [
    Carbon::now()->startOfYear(),
    Carbon::now()->endOfYear(),
  ])->get()->groupBy(function($item) {
    return $item->created_at->format('F');
  });
like image 233
None Avatar asked Dec 26 '16 12:12

None


2 Answers

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', [
 ....
like image 191
Eimantas Gabrielius Avatar answered Nov 15 '22 12:11

Eimantas Gabrielius


Carbon is actually using the php setlocale();. The Carbon::setLocale('es') method is only for localized Carbon methods like ->diffForHumans() method.

As explained here, notice that the php setlocale() reference to the locale stored on your OS. To choose one of the installed one, type locale -a on your console. Secondly, you will have to use ->formatLocalized() method instead of ->format() in order to use the format on the desired locale which can be found at the following link.

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

like image 4
xhulio Avatar answered Nov 15 '22 11:11

xhulio