当尝试在我的 PHP 8.1 alpine docker 容器中格式化货币时,我总是获得默认语言环境 (en_US)。
$ docker run -it --rm php:8.1-fpm-alpine /bin/ash
# apk update && apk add icu-dev
# docker-php-ext-configure intl && docker-php-ext-install intl
# php -a
> $formatter = new NumberFormatter('nl_NL', NumberFormatter::CURRENCY);
> echo $formatter->getLocale();
en_US
> echo $formatter->format(1234.567);
€1,234.57
我希望 getLocale() 返回 nl_NL 并格式化返回 €1.234,57。
如果我尝试语言环境 en_GB,getLocale() 会返回 en_GB。
也欢迎提供有关编写更好问题的任何提示。
最佳答案
除了icu-dev
包,同时安装 icu-data-full
包裹。默认情况下,只有 icu-data-en
已安装,如您所见,其中包含美国英语和英国英语,但没有荷兰语。
拆分是在 Alpine 3.16.0 中引入的,可能是一种节省空间的措施:安装后的完整包重约 29 MB,与图像的其余部分相比很大。
安装完整数据后,您的示例给出了
php > echo $formatter->getLocale();
nl_NL
php > echo $formatter->format(1234.567);
€ 1.234,57
正如预期的那样。
关于PHP NumberFormatter 总是给我默认的 en_US,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73895691/