我知道这有很多,但它不起作用,我安装了语言。
locale -a | grep es
es_ES
es_ES.ISO8859-1
es_ES.ISO8859-15
es_ES.UTF-8
我已经设置为 utf-8
\Carbon\Carbon::setUtf8(true);
setlocale(LC_ALL, 'es_ES.UTF-8');
$game_date = $date->formatLocalized('%A %d %B %Y %H %M %p');
我的 html 中有 utf-8 标签
<meta charset="utf-8">
但我仍然得到错误的字符。
"sábado" - "miércoles"
最佳答案
我在尝试使用 sk_SK.UTF-8
时遇到了同样的问题语言环境。帮助我解决问题的是删除 \Carbon\Carbon::setUtf8(true);
代码的一部分。
但是为什么会这样呢?首先,关于 setUtf8 函数的 Carbon 文档是这样说的:
Some languages require utf8 encoding to be printed (locale packages that does not ends with .UTF8 mainly). In this case you can use the static method Carbon::setUtf8() to encode the result of the formatLocalized() call to the utf8 charset.
在检查 Carbon 的源代码后,
formatLocalized()
函数调用 utf8_encode()
如果我们之前使用已经提到的 Carbon::setUtf8(true)
将变量 utf8 设置为 true,则可以从 PHP 库中调用函数。 .Carbon source on GitHub
return static::$utf8 ? utf8_encode($formatted) : $formatted;
因为您的语言环境已经配置为使用 UTF-8 标准,进一步的 php 编码会将格式化的字符串弄乱。
我想如果你想使用 Carbon 将你的字符串格式化为 utf8,你必须首先在使用
setLocale()
设置你的语言环境时摆脱 UTF-8 编码。 .但是,我会坚持删除 Carbon 函数并使用正确的语言环境。TL; 博士
使用带有 UTF-8 编码的正确语言环境,例如
es_ES.UTF-8
或尝试使用没有 UTF-8 编码的语言环境并依赖于 \Carbon\Carbon::setUtf8(true);
和 utf8_encode()
功能。我建议第一个选项。希望这有帮助:)
关于php - utf-8 Carbon 格式中的错误字符本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50336919/