PHP-Intl对于PHP V7.0.20的行为有所不同 [英] PHP-Intl acts different for PHP V7.0.20

查看:106
本文介绍了PHP-Intl对于PHP V7.0.20的行为有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$lang="de-AT";
$currency="3333";
$formatter = new NumberFormatter($lang, NumberFormatter::DECIMAL);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2);

echo $formatter->format($currency);

可以在此处复制/粘贴:在线php (尽管我不知道该网站的php版本)

can be run in here with copy/paste:online php (though I don't know the php version of this site)

其输出:3.333正是我所期望的.

which outputs: 3.333 and is exactly what I've expected.

但是在使用PHP 7.0.20和PHPUnit 6.2.1的本地主机上,单元测试给出的结果是:3 333.

But on my local host with PHP 7.0.20 and PHPUnit 6.2.1 the Unit test gives me as result: 3 333.

有什么新消息吗?为什么?

Any idea what's new or why?

推荐答案

您在此处看到的语言环境de-AT的区别:

The difference you see here for the locales de-AT:

3.333

3 333

只是通过简单的字符串比较就可以实现区别.

is just that with a bare string comparison, there is a difference.

对于书面语言,没有.两种形式在该语言环境中都是正确的,并显示相同编号.

For written language, there is not. Both forms are correct in that locale and show the same number.

但是,您想要更明确地进行配置,为此,请命令数字格式器使用点'.作为千位分隔符(而不是使用编译的语言环境信息):

However, you'd like to configure that more explicitly, for that, command the number-formatter to use the dot '.' as thousands separator (instead of using the compiled in locale information):

$formatter->setSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, '.');

配置数据

根本原因是实际代码旁边的PHP intl扩展附带了语言数据(来自CLDR项目),该语言数据由符号和规则形式的格式信息组成.这意味着,即使扩展版本相同,格式也可以更改(如您在Phpunit测试中所发现的).从您的示例中导出 :

  • hhvm-3.15.4-3.19.0,7.2.0alpha1的输出

  • Output for hhvm-3.15.4 - 3.19.0, 7.2.0alpha1

3.333 1.1.0

3.333 1.1.0

5.6.0-5.6.30,7.0.0-7.1.6的输出

Output for 5.6.0 - 5.6.30, 7.0.0 - 7.1.6

3 333 1.1.0

3 333 1.1.0

引起空格而不是点的行是:

The line which is causing the space instead of the dot is:

http://unicode.org/cldr /trac/browser/trunk/common/main/de_AT.xml#L120 (@ 13494)

具体变化:

http://unicode.org/cldr /trac/changeset/11798/trunk/common/main/de_AT.xml

以前,数字组符号是从具有点的父语言环境中接管的.

Previously the number group symbol was taken over from parent locale which has the dot.

这是在Changeset 11798中的时间戳记:07/13/15 12:38:02(2年前)-版本需要时间,因此这就是为什么您只能在该库的更高版本中看到它的原因.

That is in Changeset 11798 Timestamp: 07/13/15 12:38:02 (2 years ago) - Releases take time, so this is why you see this only in the later releases of the library.

我只能强调一点,那就是配置数据.

I can only stress the point that this is configuration data.

在单元测试中,您通常不想对此进行测试.在配置测试中,您可能需要对此进行测试.您的Phpunit测试确实表明您到目前为止的期望已不再匹配.因此,这可能需要更改代码(以使配置期望再次起作用)或测试中的更改,因为在测试特定于区域设置的配置时,您可能并不是您想要的.

In a unit-test you normally don't want to test for that. In a configuration test, you might want to test for that. Your Phpunit-test did reveal that the expectation you had so far is not matched any longer. So this might require either a code-change (that is to make your configuration expectation working again) or a change in the test, as you were testing locale specific configuration you might have not been what you were looking for.

根据您的问题,我可以想象两者都是可能的.最后,测试反映了您的期望,并且可能显示了隐藏的依赖关系,此处为语言环境配置数据.

From your question I can imagine that both is possible. In the end, the test reflects your expectation and might have revealed a hidden dependency, here the locale configuration data.

一个修复程序可能与将语言环境切换为"de"而不是"de-AT"一样: https ://3v4l.org/hB15n

A fix could be as little as switching the locale to "de" instead of "de-AT": https://3v4l.org/hB15n

  • 5.6.0-5.6.30,hhvm-3.15.4-3.19.0、7.0.0-7.2.0alpha1的输出

  • Output for 5.6.0 - 5.6.30, hhvm-3.15.4 - 3.19.0, 7.0.0 - 7.2.0alpha1

3.333 1.1.0

3.333 1.1.0

但是请记住,这也会隐藏您可能已经为错误的事物测试的潜在问题.因此最好保留失败的测试作为笔记,以问自己对自己的期望以及代码是否符合编写的目的.

But keep in mind that this would also hide the underlying issue that you might have tested for the wrong thing. So better keep the failing test as a note to ask yourself about the expectations you had and if the code does for what you have it written for.

这篇关于PHP-Intl对于PHP V7.0.20的行为有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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