laravel 如何为 Carbon 设置语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41331137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:00:49  来源:igfitidea点击:

How to set language for Carbon?

laravellaravel-5laravel-5.2

提问by None

So i want to set language for Carbon, but i always get same result.

所以我想为 Carbon 设置语言,但我总是得到相同的结果。

  Carbon::setLocale('es');
$archive_current_year = Articles::whereBetween('created_at', [
    Carbon::now()->startOfYear(),
    Carbon::now()->endOfYear(),
  ])->get()->groupBy(function($item) {
    return $item->created_at->format('F');
  });

采纳答案by Eimantas Gabrielius

try using PHP function setlocalealso check if your hosting allows and gives you the locales you want.

尝试使用 PHP 函数setlocale还检查您的主机是否允许并为您提供所需的语言环境。

 setlocale(LC_TIME, 'es_ES');
 Carbon::setLocale('es');
 $archive_current_year = Articles::whereBetween('created_at', [
 ....

回答by xhulio

Carbon is actually using the php setlocale();. The Carbon::setLocale('es')method is only for localized Carbon methods like ->diffForHumans()method.

Carbon 实际上使用的是 php setlocale();. 该Carbon::setLocale('es')方法仅适用于像方法这样的局部碳方法->diffForHumans()

As explained here, notice that the php setlocale()reference to the locale stored on your OS. To choose one of the installed one, type locale -aon your console. Secondly, you will have to use ->formatLocalized()method instead of ->format()in order to use the format on the desired locale which can be found at the following link.

正如解释这里,注意到PHP的setlocale()参考区域存储您的操作系统。要选择已安装的其中之一,请locale -a在您的控制台上键入。其次,您必须使用->formatLocalized()method 而不是->format()为了使用所需语言环境的格式,可以在以下链接中找到。

http://php.net/manual/en/function.strftime.php

http://php.net/manual/en/function.strftime.php