在Blade视图中,我有这个代码
{{ \Carbon\Carbon::setLocale("es") }}
{{ $registro->fecha_desde->format("l j F Y") }}<br />
{{ $registro->fecha_desde->formatLocalized("%A %d %B %Y") }}<br />
{{ $registro->fecha_desde->diffForHumans() }}
这不起作用,它返回:
Friday 30 December 2016
Friday 30 December 2016
dentro de 1 semana
因此,format()和formatLocalized始终以英文格式返回日期。 diffForHumans返回本地化的日期(在本例中为西班牙语)。
我错过了什么吗?不能相信“碳格式本地化”没有返回本地化的格式化日期....
我找到了两种用其他语言输出日期的方法。在AppServiceProvider中添加它
Carbon::setLocale('es');
setlocale(LC_TIME,'es_ES');
//This output dates in spanish
在App.php中输入'es'而不是'en'。现在您可以使用FormatLocalized,所有Carbon Functions都将使用您在setLocale中指定的语言。
注意:如果您使用的是Oracle DB,请添加:
setlocale(LC_TIME, config('app.locale'));
代替:
setlocale(LC_TIME,'es_ES');
找到了。问题是\ Carbon :: setlocale()
这看起来很难看,但有效:
{{ setlocale(LC_ALL, 'es_MX', 'es', 'ES') }}
{{ $registro->fecha_desde->formatLocalized("%A %d %B %Y") }}
输出:
es viernes 30 diciembre 2016
要查看具有其他重音的格式,您必须在格式化之前启用UTF8编码:
\Carbon::setUTF8(true);
打开AppServiceProvider.php
调用碳use Carbon\Carbon;
类
public function boot()
{
Carbon::setUTF8(true);
Carbon::setLocale(config('app.locale'));
setlocale(LC_TIME, config('app.locale'));
}
在config
文件夹中打开app.php
设置'locale' => 'es',
在ubuntu
public function boot()
{
Carbon::setUTF8(true);
Carbon::setLocale(config('app.locale'));
setlocale(LC_TIME,'');
}