Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carbon formatLocalized not working in Blade

In a Blade view I have this code

{{ \Carbon\Carbon::setLocale("es") }} {{ $registro->fecha_desde->format("l j F Y") }}<br /> {{ $registro->fecha_desde->formatLocalized("%A %d %B %Y") }}<br /> {{ $registro->fecha_desde->diffForHumans() }}

This does not work, it returns:

Friday 30 December 2016
Friday 30 December 2016
dentro de 1 semana 

So, format() and formatLocalized always returns the date in english format. diffForHumans returns the date localized (in spanish in this case).

Am I missing something? cant believe "Carbon's formatLocalized" is not returning localized formated dates....

like image 807
Alex Angelico Avatar asked Dec 20 '16 23:12

Alex Angelico


2 Answers

I found two ways to output dates in other language. add this in the AppServiceProvider

    Carbon::setLocale('es');
    setlocale(LC_TIME,'es_ES');

//This output dates in spanish

In App.php put 'es' instead 'en'. now you can use FormatLocalized and all Carbon Functions will be in the language that you assign in setLocale.

Note: If you're using Oracle DB add:

 setlocale(LC_TIME, config('app.locale'));

instead:

setlocale(LC_TIME,'es_ES');
like image 60
Eduardo Quiñonez Avatar answered Oct 30 '22 03:10

Eduardo Quiñonez


Found it. The problems is \Carbon::setlocale()

This looks ugly but works:

{{ setlocale(LC_ALL, 'es_MX', 'es', 'ES') }}
{{ $registro->fecha_desde->formatLocalized("%A %d %B %Y") }}

Output:

es viernes 30 diciembre 2016
like image 41
Alex Angelico Avatar answered Oct 30 '22 04:10

Alex Angelico