0

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.

pmiranda
  • 7,602
  • 14
  • 72
  • 155
  • 1
    `date()` is not aware of locales. Laravel must have some builtin way to localise dates. – Álvaro González Oct 11 '17 at 14:17
  • 2
    See https://stackoverflow.com/a/30291265/463205. This isn't really a Laravel-specific issue, just that `date()` doesn't respect locale, whereas `strftime()` does. – athms Oct 11 '17 at 14:19
  • 2
    Take a look at [Carbon](http://carbon.nesbot.com/docs/), it handles also locales and it is widely supported by Laravel. – Desh901 Oct 11 '17 at 14:23
  • Thanks guys, I get it with `strftime`. I was checking Carbon but had to use too much lines of code to something simple that I need to use only once. – pmiranda Oct 11 '17 at 14:24
  • 1
    I don't get people who use Laravel and then they resort to `date` or `strftime`. Why would you use something that was outdated 10 years ago today? You got Carbon with Laravel anyway, use it. `date` will assume server's default time zone. You're simply lucky to have your server in your own time zone, and that you're doing this for people within your country. Learn how to use Carbon and forget about date, time zone or daylight savings forever. It's also super simple to use. – Mjh Oct 11 '17 at 14:28
  • Laravel use Carbon out of the box, if your dates are inside model's `$dates` array you can add a simply `Carbon::setLocale('es')` before use `$data->created` or even set it in a provider and forget about locales in controllers – aaron0207 Oct 11 '17 at 14:29
  • 1
    @Mjh of course you can't get it because you don't know the whole context of the question: reuse of MB of code, multiple services from different languages and frameworks, a team and not only me touching the code, etc, etc. There's no need to comment things like this. – pmiranda Oct 11 '17 at 14:42
  • So using `strftime` is fine, in context of reuse, multiple services from different languages and frameworks, but creating a helper function that uses carbon is what, black magic and violates all that? It's easier just to say you won't use it because you found another thing, and Carbon looks intimidating. It's fine to be lazy, we're programmers, but excuses suck so much. Your solution has a flaw, I hinted at it, it's up to you to stick to your ego or to try to listen to other guys, we might actually know something you don't. – Mjh Oct 11 '17 at 14:52

1 Answers1

2

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.

Desh901
  • 2,633
  • 17
  • 24