Laravel APP_LOCALE西班牙文 [英] Laravel APP_LOCALE in spanish

查看:56
本文介绍了Laravel APP_LOCALE西班牙文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 5.4的 .env 中,我有:

In Laravel 5.4, in .env I have:

APP_LOCALE=es
APP_FALLBACK_LOCALE=en
APP_LOCALE_PHP=es_US

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 是一个来自数据库的字符串,来自 created_at 列.这样显示为:

$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?

我用 es 测试APP_LOCALES没有任何反应.我试图在$ mydate之前 setlocale(LC_TIME,"es"); 相同.我要保存所做的更改,并顺便做 php artisan config:cache .

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.

推荐答案

我建议您使用 Carbon ,它为DateTime操作提供了方便的原语,并且Laravel完全支持它(因此不需要其他程序包或 composer 操作).要格式化本地化日期,您只需要几行代码:

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');

如果要跳过 Carbon 步骤,只需使用 setlocale 设置时间区域设置即可.要获取UNIX计算机中已安装语言环境的列表,请运行命令

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

在终端中,或者如果您需要向计算机添加语言环境,请取消注释/etc/locale.gen 中与您的语言环境相对应的行(例如, es_ES.UTF8 ),然后在终端中运行以下命令以生成语言环境

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

希望有帮助.

这篇关于Laravel APP_LOCALE西班牙文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆