PHP IntlDateFormatter not working in my docker container

huangapple go评论62阅读模式
英文:

php IntlDateFormatter not working in my docker container

问题

I want to display the date and more precisely the names of the months according to the locale of the country, but for some reason the class IntlDateFormatter for the name of the month returns the record M05, which I understand as the month and its number. And I don't understand why this is happening because I installed the icu-dev library, and as described in the documentation, this locale is not from the user's system, but from the icu library:

Dockerfile:

FROM php:7.4-cli-alpine

RUN apk update && apk add icu-dev bash autoconf g++ make zlib-dev libzip-dev libpng-dev libwebp-dev freetype-dev libjpeg-turbo-dev libpng-dev supervisor \
    && docker-php-ext-configure \
        gd --enable-gd --with-freetype --with-jpeg --with-webp \
    && docker-php-ext-install intl pdo_mysql gd zip

RUN mv $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini

COPY ./common/php/conf.d /usr/local/etc/php/conf.d
COPY ./development/php-cli/conf.d /usr/local/etc/php/conf.d

RUN apk add unzip

# fix work iconv library with alphine
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php

COPY ./common/wait-for-it.sh /usr/local/bin/wait-for-it
RUN chmod 555 /usr/local/bin/wait-for-it

RUN addgroup -g 1000 app && adduser -u 1000 -G app -s /bin/sh -D app

ENV COMPOSER_ALLOW_SUPERUSER 1

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/bin --filename=composer --quiet \
    && rm -rf /root/.composer/cache

WORKDIR /app

CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisor.conf"]

USER app

My test script, ua_UK is a correct locale:

$formatter = new \IntlDateFormatter(
    'uk_UA',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL
);

$formatter->setPattern('MMMM');

echo $formatter->format(new \DateTime()); //M05

What is the reason for this behavior?

英文:

I want to display the date and more precisely the names of the months according to the locale of the country, but for some reason the class IntlDateFormatt for the name of the month returns the record M05, which I understand as the month and its number. And I don't understand why this is happening because I installed the icu-dev library, and as described in the documentation, this locale is not from the user's system, but from the icu library:

> The locale that will be used in intl functions when none is specified (either by omitting the corresponding argument or by passing NULL). These are ICU locales, not system locales. The built-in ICU locales and their data can be explored at » http://demo.icu-project.org/icu-bin/locexp.

Dockerfile:

FROM php:7.4-cli-alpine

RUN apk update && apk add icu-dev bash autoconf g++ make zlib-dev libzip-dev libpng-dev libwebp-dev freetype-dev libjpeg-turbo-dev libpng-dev supervisor \
    && docker-php-ext-configure \
        gd --enable-gd --with-freetype --with-jpeg --with-webp \
    && docker-php-ext-install intl pdo_mysql gd zip

RUN mv $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini

COPY ./common/php/conf.d /usr/local/etc/php/conf.d
COPY ./development/php-cli/conf.d /usr/local/etc/php/conf.d

RUN apk add unzip

# fix work iconv library with alphine
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php

COPY ./common/wait-for-it.sh /usr/local/bin/wait-for-it
RUN chmod 555 /usr/local/bin/wait-for-it

RUN addgroup -g 1000 app && adduser -u 1000 -G app -s /bin/sh -D app

ENV COMPOSER_ALLOW_SUPERUSER 1

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/bin --filename=composer --quiet \
    && rm -rf /root/.composer/cache

WORKDIR /app

CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisor.conf"]

USER app

My test script, ua_UK is a correct locale:

$formatter = new \IntlDateFormatter(
    'uk_UA',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL
);

$formatter->setPattern('MMMM');

echo $formatter->format(new \DateTime()); //M05

What is the reason for this behavior?

答案1

得分: 1

我解决了这个问题,因为我认为这是icu-dev的工作,我在这个图像中找到了解决这个问题的方法,但我想要提醒您,使用这些步骤构建的时间增加了300秒,对我来说是可以接受的,尽管我不能说好是70秒,它变成了**~400秒**,正如您所见,这不太好,如果您了解Docker和一些小细节,也许您可以优化这些点,或者解释为什么icu-dev库存在问题,但现在,我将留下一个简短的代码,应该添加到图像中:

FROM php:7.4-cli-alpine

ENV ICU_RELEASE=73.1

# Remove icu-dev from dependencies
RUN apk add --update --no-cache bash autoconf g++ make zlib-dev libzip-dev libpng-dev libwebp-dev freetype-dev libjpeg-turbo-dev \
    && docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg --with-webp \
    && docker-php-ext-install pdo_mysql gd zip \
    # And we need these three lines to install icu through the official repository, followed by its unpacking and build
    && cd /tmp && curl -Ls https://github.com/unicode-org/icu/releases/download/release-$(echo $ICU_RELEASE | tr '.' '-')/icu4c-$(echo $ICU_RELEASE | tr '.' '_')-src.tgz > icu4c-src.tgz \
    && cd /tmp && tar xzf icu4c-src.tgz && cd /tmp/icu/source && echo "#define FALSE 0" >> config.h && echo "#define TRUE 1" >> config.h && ./runConfigureICU Linux && make && make install && rm -rf /tmp/icu /tmp/icu4c-src.tgz \
    && docker-php-ext-install intl

...

希望有人看到这个答案,能够将构建时间从300秒优化到一些合理的数字。

英文:

I solved this problem, as I thought it was the work of icu-dev, I found this image in which this problem was solved, but want to warn that the build time with these steps increased by 300 seconds, which is acceptable for me, although I would not say what was good was 70, it became ~400, as you can see, it is not good, if you understand a docker and such little things, then maybe you could optimize these points or explain why there is a problem with the icu-dev library, but for now, I will leave a short code what should be added to the image:

FROM php:7.4-cli-alpine

ENV ICU_RELEASE=73.1

# Remove icu-dev from dependencies
RUN apk add --update --no-cache bash autoconf g++ make zlib-dev libzip-dev libpng-dev libwebp-dev freetype-dev libjpeg-turbo-dev \
    && docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg --with-webp \
    && docker-php-ext-install pdo_mysql gd zip \
    # And we need these three lines to install icu through the official repository, followed by its unpacking and build
    && cd /tmp && curl -Ls https://github.com/unicode-org/icu/releases/download/release-$(echo $ICU_RELEASE | tr '.' '-')/icu4c-$(echo $ICU_RELEASE | tr '.' '_')-src.tgz > icu4c-src.tgz \
    && cd /tmp && tar xzf icu4c-src.tgz && cd /tmp/icu/source && echo "#define FALSE 0" >> config.h && echo "#define TRUE 1" >> config.h && ./runConfigureICU Linux && make && make install && rm -rf /tmp/icu /tmp/icu4c-src.tgz \
    && docker-php-ext-install intl

...

I hope that someone will come across this answer and be able to optimize the build from 300 seconds to some reasonable numbers.

答案2

得分: 1

I can confirm this worked for me! I Lost 2 days searching for a solution everywhere and this was the only one that worked for dates and currency formatting when using an Alpine linux with php-fpm.

Here is my final Dockerfile snippet:

ENV ICU_RELEASE=73.1
RUN apk add --update --no-cache autoconf g++ make \
  && cd /tmp && curl -Ls https://github.com/unicode-org/icu/releases/download/release-$(echo $ICU_RELEASE | tr '.' '-')/icu4c-$(echo $ICU_RELEASE | tr '.' '_')-src.tgz > icu4c-src.tgz \
  && cd /tmp && tar xzf icu4c-src.tgz \
  && cd /tmp/icu/source \
  && echo "#define FALSE 0" >> config.h && echo "#define TRUE 1" >> config.h \
  && ./runConfigureICU Linux \
  && make -j$(nproc) && make install \
  && rm -rf /tmp/icu /tmp/icu4c-src.tgz
RUN apk add --no-cache php82-intl

(Note: I have translated the code part as per your request. If you need further assistance or translations, please let me know.)

英文:

I can't post comments yet, so I'll write here. Sorry about that!
I can confirm this worked for me! I Lost 2 days searching for a solution everywhere and this was the only one that worked for dates and currency formatting when using an Alpine linux with php-fpm.

Here is my final Dockerfile snippet:

ENV ICU_RELEASE=73.1
RUN apk add --update --no-cache autoconf g++ make \
  && cd /tmp && curl -Ls https://github.com/unicode-org/icu/releases/download/release-$(echo $ICU_RELEASE | tr '.' '-')/icu4c-$(echo $ICU_RELEASE | tr '.' '_')-src.tgz > icu4c-src.tgz \
  && cd /tmp && tar xzf icu4c-src.tgz \
  && cd /tmp/icu/source \
  && echo "#define FALSE 0" >> config.h && echo "#define TRUE 1" >> config.h \
  && ./runConfigureICU Linux \
  && make -j$(nproc) && make install \
  && rm -rf /tmp/icu /tmp/icu4c-src.tgz
RUN apk add --no-cache php82-intl

huangapple
  • 本文由 发表于 2023年5月7日 18:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定