Laravel APP_LOCALE in spanish

后端 未结 1 1339
后悔当初
后悔当初 2021-01-27 09:12

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:

1条回答
  •  囚心锁ツ
    2021-01-27 09:14

    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.

    0 讨论(0)
提交回复
热议问题