Home > Software design >  PHP NumberFormatter always gives me the default en_US
PHP NumberFormatter always gives me the default en_US

Time:09-29

When trying to format currency in my PHP 8.1 alpine docker container I always get the default locale (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

I expect getLocale() to return nl_NL and format to return €1.234,57.

If I try locale en_GB, getLocale() does return en_GB.

Any tips on writing a better question are welcome too.

CodePudding user response:

In addition to the icu-dev package, also install the icu-data-full package. By default, only icu-data-en is installed, which contains American and British English as you've seen, but no Dutch.

The split was introduced in Alpine 3.16.0, probably as a space-saving measure: the full package weighs about 29 MB installed, which is big compared to the rest of the image.

With the full data installed, your example gives

php > echo $formatter->getLocale();
nl_NL
php > echo $formatter->format(1234.567);
€ 1.234,57

as expected.

  • Related