Carbon formatLocalized返回奇怪的字符

时间:2017-04-27 13:49:25

标签: laravel localization php-carbon

我在Laravel中使用Carbon在我的项目中生成本地化的日期标签。

在我的控制器中,我使用以下代码生成标签:

$today = Carbon::now();

    $labels[] = [
        'date' => $today->format('Y-m-d'),
        'label' => ('Dziś ('.$today->formatLocalized('%a, %e %b').')')
    ];
    $labels[] = [
        'date' => (clone $today )->addDays(1)->format('Y-m-d'),
        'label' => 'Jutro ('.(clone $today)->addDays(1)->formatLocalized('%a, %e %b').')'
    ];
    $labels[] = [
        'date' => (clone $today )->addDays(2)->format('Y-m-d'),
        'label' => (clone $today)->addDays(2)->formatLocalized('%a, %e %b')
    ];
    $labels[] = [
        'date' => (clone $today )->addDays(3)->format('Y-m-d'),
        'label' => (clone $today)->addDays(3)->formatLocalized('%a, %e %b')
    ];
    $labels[] = [
        'date' => (clone $today )->addDays(4)->format('Y-m-d'),
        'label' => (clone $today)->addDays(4)->formatLocalized('%a, %e %b')
    ];

直到有任何语言特定的二丙烯酸符号,它才能正常工作。如果我用$labels输出dd(...),这就是我得到的:

array:5 [▼
  0 => array:2 [▼
    "date" => "2017-04-27"
    "label" => "Dziś (czw, 27 kwi)"
  ]
  1 => array:2 [▼
    "date" => "2017-04-28"
    "label" => b"Jutro (pi±, 28 kwi)"
  ]
  2 => array:2 [▼
    "date" => "2017-04-29"
    "label" => "sob, 29 kwi"
  ]
  3 => array:2 [▼
    "date" => "2017-04-30"
    "label" => "nie, 30 kwi"
  ]
  4 => array:2 [▼
    "date" => "2017-05-01"
    "label" => "pon,  1 maj"
  ]
]

注意第二项的奇怪标签:

"label" => b"Jutro (pi±, 28 kwi)"

正确的标签应为:"Jutro (pią, 29 kwi)"

当然,像Blade这样的标签输出不正确。

有什么问题?它是编码的东西还是我使用formatLocalized方法错了?

仅供参考:我已在boot()的{​​{1}}方法中设置了区域设置:

AppServiceProvider

1 个答案:

答案 0 :(得分:0)

嗨,这个问题可能与波兰语言所需的utf-8编码有关。您可以看到我对类似问题的回答here

TL; DR

使用setLocale函数时,请使用utf-8版本的区域设置 - setlocale(LC_TIME, 'pl_PL.UTF-8');