pmiranda
pmiranda

Reputation: 8420

Laravel APP_LOCALE in spanish

In Laravel 5.4, in .env I have:

APP_LOCALE=es
APP_FALLBACK_LOCALE=en
APP_LOCALE_PHP=es_US

and in config/app.php:

'locale' => env('APP_LOCALE', 'es'),
'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),
'locale_php' => env('APP_LOCALE_PHP', 'en_US'),

But I want to set as spanish. For example, In a controller I have:

$mydate = date('d F Y', strtotime($data->created));

$data->created is a string that comes from database, from column created_at. This way it show as:

14 August 2017

English, so ¿how can I get it in spanish?

I test APP_LOCALES with esnothing happens. I tried to setlocale(LC_TIME, "es"); right before the $mydate, and it's the same. I'm saving the changes and doing php artisan config:cache by the way.

Upvotes: 0

Views: 4303

Answers (1)

Desh901
Desh901

Reputation: 2723

I suggest you to use Carbon, that provides handy primitives for DateTime operations and it is fully supported by Laravel out of the box (so no additional packages or composer require actions). To format a localized date you need just few lines of code:

use \Carbon\Carbon;

...

setlocale(LC_TIME, 'es_ES.UTF-8');
Carbon::setLocale('es');
$mydate = Carbon::parse($data->created)->formatLocalized('%d %B %Y');

If you want to skip the Carbon steps just setting the time locale with setlocale will work out of the box. To get the list of installed locales in your UNIX machine run the command

$ locale -a

in your terminal or if you need to add a locale to your machine uncomment the line corresponding to you locale in /etc/locale.gen (e.g. es_ES.UTF8) then run the the following command in your terminal to generate the locale

$ sudo locale-gen es_ES.UTF-8

then update the locale list in your machine typing

$ sudo update-locale

hope it helps.

Upvotes: 2

Related Questions