0
I’m using Laravel 5.2 and would like to bring the dates in full in Portuguese (pt_BR), but Carbon brings the dates text only in English.
$post->created_at->formatLocalized('%B'); //"January"
In the config/app.php Timezone is defined as 'America/Sao_paulo' already in the 'locale' tested 'pt_BR', 'pt' and 'pt_BR.UTF-8', and I could not bring in English.
I was successful in Tinker, using setlocate:
>>> setlocale(LC_ALL, 'pt_BR.UTF-8');
>>> $post = Post::find(1);
>>> $post->created_at->formatLocalized('%B');
=> "janeiro"
It is worth noting that I have the language package on the server (Ubuntu Trusty x64):
$ locale -a
POSIX
C.UTF-8
C
en_**.UTF-8
pt_BR.utf8
pt_PT.utf8
My main question is how best to bring the Carbon dates in full and pt_BR? Where can I put the setlocale in the code? Or it would be better to use Localization?
UPDATE:
Carbon doesn’t really support locales, because it uses the Datetime class:
Unfortunately the base class Datetime does not have any localization support. To Begin localization support a formatLocalized($format) method has been Added. The implementation makes a call to strftime using the Current instance timestamp. If you first set the Current locale with setlocale() then the string returned will be Formatted in the correct locale.
As the package website itself recommends:
setlocale(LC_TIME, 'German');
echo $dt->formatLocalized('%A %d %B %Y'); // Donnerstag 25 Dezember 1975
setlocale(LC_TIME, '');
echo $dt->formatLocalized('%A %d %B %Y'); // Thursday 25 December 1975
I found here a good option to solve the problem, using the Laravel-date that extends Carbon and offers Multiple Languages.
http://stackoverflow.com/questions/20057450/whats-the-best-way-to-localise-a-date-on-laravel
– Daniel Omine
Carbon is based on Datetime so it is not affected by the locale.
– rray
Thanks @daniel, I will use the Date class as updated in the question.
– Nícolas Huber
@rray is not quite so, huh. There is an implementation of
strftime
within theCarbon
. The method you can use for this is theCarbon::formatLocalized
. rsrsrss– Wallace Maxters
It is specific to the Laravel framework. So I do not consider duplicate.
– Daniel Omine