Carbon formatLocalized 在 Blade 中不起作用

Ale*_*ico 5 php php-carbon laravel-5.3

在 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 
Run Code Online (Sandbox Code Playgroud)

因此, format() 和 formatLocalized 总是以英文格式返回日期。diffForHumans 返回本地化的日期(在本例中为西班牙语)。

我错过了什么吗?不敢相信“Carbon's formatLocalized”没有返回本地化的格式化日期......

小智 6

我找到了两种用其他语言输出日期的方法。在 AppServiceProvider 中添加这个

    Carbon::setLocale('es');
    setlocale(LC_TIME,'es_ES');

//This output dates in spanish
Run Code Online (Sandbox Code Playgroud)

在 App.php 中,用“es”代替“en”。现在您可以使用 FormatLocalized 并且所有 Carbon 函数都将使用您在 setLocale 中指定的语言。

注意:如果您使用的是 Oracle DB,请添加:

 setlocale(LC_TIME, config('app.locale'));
Run Code Online (Sandbox Code Playgroud)

反而:

setlocale(LC_TIME,'es_ES');
Run Code Online (Sandbox Code Playgroud)


Ale*_*ico 5

找到了。问题是 \Carbon::setlocale()

这看起来很丑但是有效:

{{ setlocale(LC_ALL, 'es_MX', 'es', 'ES') }}
{{ $registro->fecha_desde->formatLocalized("%A %d %B %Y") }}
Run Code Online (Sandbox Code Playgroud)

输出:

es viernes 30 diciembre 2016
Run Code Online (Sandbox Code Playgroud)