Laravel Carbon本地化不起作用(从数字中获取月份的本地化名称)

问题描述:

使用Laravel 5.3,

Using Laravel 5.3,

我用我的方法

setlocale(LC_TIME, 'hr-HR');
dd(Carbon::now()->formatLocalized('%A'));

但是我得到的是Sunday而不是CroatianWordForSunday.

but I get Sunday instead of CroatianWordForSunday.

我尝试使用Carbon::setLocale('hr')代替setlocale(),但是我仍然得到Sunday.

I tried using Carbon::setLocale('hr') instead setlocale() but I still get Sunday.

在我的config/app.php文件中,我设置了'locale' => 'hr'.

In my config/app.php file I have set 'locale' => 'hr'.

需要注意的是,如果使用Carbon::setLocale('hr'),Carbon的diffForHumans()方法已成功翻译.

Thing to note is that Carbon's diffForHumans() method is successfully translated if I use Carbon::setLocale('hr').

最后,我要做的就是将数字8转换为8月,但使用克罗地亚语. 我总是可以手动将1月更改为Siječanj,依此类推,但是如果可以使用一些PHP或Carbon的方法来简化代码,那将是很好的选择.

In the end all I'm trying to do is convert number 8 to August but in Croatian. I could always just manually change January to Siječanj and so on but it would be nice if it could be done using some PHP's or Carbon's method to keep my code concise.

您确定系统上已安装hr_HR(而不是hr-HR!)语言环境吗?

Are you sure the hr_HR (and not hr-HR !) locale is installed on your system?

假设您的服务器在Unix环境中运行,那么在终端中使用locale -a磁带时您会看到什么?

Supposing your server runs on an Unix environment, what do you see when you tape locale -a in a terminal?

如果看不到它,则应首先尝试安装它. 根据您的系统,您可以尝试:

If you do not see it, then you should try to install it first. Depending of your system, you could try:

$ sudo locale-gen hr_HR.UTF-8
$ sudo dpkg-reconfigure locales

根据PHP strftime 的文档(Carbon将此称为功能):

According to the documentation of PHP strftime (Carbon is calling this function) :

如果您在系统中安装了各自的语言环境,此示例将起作用.

This example will work if you have the respective locales installed in your system.

我成功地使用了App\Providers\AppServiceProvider引导方法中的以下行来用法语进行Carbon翻译工作:

I succeeded to have the Carbon translation works in french using those lines in App\Providers\AppServiceProvider boot's method:

use Config;
use Carbon\Carbon;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        setlocale(LC_ALL, Config::get('app.lc_all'));
        Carbon::setLocale(Config::get('app.locale'));
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

具有以下配置设置:

// [...]
'locale' => env('APP_LOCALE', 'en'),
'lc_all' => env('APP_LC_ALL', 'en_US.UTF-8'), // Pay attention to the locale name!
// [...]

然后使用.env文件:

Then using the .env file :

APP_LOCALE = fr
APP_LC_ALL = fr_FR.UTF-8