Laravel Carbon本地化不起作用(从数字中获取月份的本地化名称) [英] Laravel Carbon localization not working (get localized name of month from number)

查看:215
本文介绍了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

这篇关于Laravel Carbon本地化不起作用(从数字中获取月份的本地化名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆