Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP NumberFormatter always gives me the default en_US

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any tips on writing a better question are welcome too.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading