PHP NumberFormatter 总是给我默认的 en_US

Fra*_*ank 5 php intl docker alpine-linux

当尝试在我的 PHP 8.1 alpine docker 容器中格式化货币时,我总是得到默认区域设置 (en_US)。

\n
$ docker run -it --rm php:8.1-fpm-alpine /bin/ash\n# apk update && apk add icu-dev\n# docker-php-ext-configure intl && docker-php-ext-install intl\n# php -a\n> $formatter = new NumberFormatter('nl_NL', NumberFormatter::CURRENCY);\n> echo $formatter->getLocale();\nen_US\n> echo $formatter->format(1234.567);\n\xe2\x82\xac1,234.57\n
Run Code Online (Sandbox Code Playgroud)\n

我期望 getLocale() 返回 nl_NL 和 format 返回 \xe2\x82\xac1.234,57。

\n

如果我尝试 locale en_GB,getLocale() 确实返回 en_GB。

\n

也欢迎任何有关编写更好问题的建议。

\n

Wan*_*uta 5

除了icu-dev软件包外,还要安装icu-data-full软件包。默认情况下,仅icu-data-en安装,其中包含您所看到的美式英语和英式英语,但不包含荷兰语。

\n

这种分割是在Alpine 3.16.0中引入的,可能是作为一种节省空间的措施:安装的完整包重量约为 29 MB,与镜像的其余部分相比很大。

\n

安装完整数据后,您的示例给出

\n
php > echo $formatter->getLocale();\nnl_NL\nphp > echo $formatter->format(1234.567);\n\xe2\x82\xac\xc2\xa01.234,57\n
Run Code Online (Sandbox Code Playgroud)\n

正如预期的那样。

\n