Carbon setLocale 不工作 Laravel

Carbon setLocale not working Laravel

我读过几个关于设置语言环境的计算器。我在终端中测试了 locale -a 以查看我的语言环境是否在其中,而且确实如此。在appServiceProvider中添加如下代码规则:

public function boot()
{
    Carbon::setLocale($this->app->getLocale());
}

$this->app->getLocale() returns "nl"

有人知道为什么 Carbon 仍然显示星期天而不是 Zondag 吗?

您可能希望在应用程序开头的某处使用 setLocale(LC_TIME, $this->app->getLocale())

然后,如果您希望使用本地名称的本地化日期格式,请使用 formatLocalized 函数

Carbon::now()->formatLocalized('%d %B %Y');

有关格式化参数,请参阅 http://php.net/manual/en/function.strftime.php

试试这个:setLocale(LC_TIME, app()->getLocale());

使用全球本地化格式翻译碳日期

测试时间:Laravel 5.8、Laravel 6、Laravel 8


在config/app.php

'locale' => 'id', // The default is 'en', but this time I want localize them to Indonesian (ID)

然后,要使语言环境输出执行如下操作:

// WITHOUT LOCALE
Carbon\Carbon::parse('2019-03-01')->format('d F Y'); //Output: "01 March 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 minutes ago"

// WITH LOCALE
Carbon\Carbon::parse('2019-03-01')->translatedFormat('d F Y'); // Output: "01 Maret 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 menit yang lalu"

有关转换本地化日期的更多信息,您可以在下面查看 link https://carbon.nesbot.com/docs/#api-localization