Laravel APP_LOCALE in spanish

為{幸葍}努か 提交于 2021-02-05 10:54:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/46690536/laravel-app-locale-in-spanish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!