utf-8 Carbon格式的错误字符 [英] Bad characters in utf-8 Carbon formatLocalized
问题描述
我知道有很多关于此的内容,但是它不起作用,我已经安装了语言.
I know there is much about this, but it doesn't work, I have the language installed.
locale -a | grep es
es_ES
es_ES.ISO8859-1
es_ES.ISO8859-15
es_ES.UTF-8
我已设置为utf-8
I have set to 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标记
I have the utf-8 tag in my html
<meta charset="utf-8">
但是我仍然遇到错误的字符.
but I am still getting the wrong characters.
sábado"-miÃrcoles"
"sábado" - "miércoles"
推荐答案
在尝试使用sk_SK.UTF-8
语言环境时,我遇到了同样的问题.帮助我解决该问题的原因是删除了代码的\Carbon\Carbon::setUtf8(true);
部分.
I had the same problem when trying to use the sk_SK.UTF-8
locale. What helped me to solve the problem was to remove the \Carbon\Carbon::setUtf8(true);
portion of the code.
但是为什么它如此工作?首先,关于setUtf8函数的Carbon文档说:
But why does it work like this? Firstly the Carbon documentation regarding the setUtf8 function says this:
某些语言需要打印utf8编码(主要不是以.UTF8结尾的语言环境软件包).在这种情况下,您可以使用静态方法Carbon :: setUtf8()来编码对utf8字符集的formatLocalized()调用的结果.
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::setUtf8(true)
将变量utf8设置为true,则在检查Carbon的源代码后,formatLocalized()
函数将从PHP库调用utf8_encode()
函数.
GitHub上的碳源
Upon examining the source code for Carbon the formatLocalized()
function calls utf8_encode()
function from the PHP library if we previously set the variable utf8 to true with the already mentioned Carbon::setUtf8(true)
.
Carbon source on GitHub
return static::$utf8 ? utf8_encode($formatted) : $formatted;
由于您的语言环境已配置为使用UTF-8标准,因此进一步的php编码会使格式化的字符串混乱.
Because your locale is already configured to use the UTF-8 standard the further php encoding messes the formatted string up.
我认为,如果要使用Carbon来将字符串格式化为utf8,则在使用setLocale()
设置语言环境时必须首先摆脱UTF-8编码.但是,我只是坚持删除Carbon函数并使用正确的语言环境.
I figured that if you want to use Carbon to format your strings to utf8 you must first get rid of the UTF-8 encoding when setting your locale with setLocale()
. However, I would just stick to removing the Carbon function and using the proper locale.
TL; DR
使用具有UTF-8编码的正确语言环境,例如es_ES.UTF-8
或尝试使用不带UTF-8编码的语言环境,并依赖\Carbon\Carbon::setUtf8(true);
和utf8_encode()
函数.我建议第一种选择.希望这对您有所帮助:)
Use correct locale with UTF-8 encoding e.g. es_ES.UTF-8
or use try to use locale without the UTF-8 encoding and rely on \Carbon\Carbon::setUtf8(true);
and utf8_encode()
function. I suggest the first option. Hope this helped :)
这篇关于utf-8 Carbon格式的错误字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!