php IntlDateFormatter 在我的 docker 容器中不起作用

问题描述 投票:0回答:2

我想根据国家/地区的区域设置显示日期,更准确地显示月份名称,但由于某种原因,月份名称的类

IntlDateFormatt
返回记录
M05
,我将其理解为月份及其编号。我不明白为什么会发生这种情况,因为我安装了
icu-dev
库,并且如 文档中所述,该语言环境不是来自用户的系统,而是来自
icu
库:

未指定时将在 intl 函数中使用的区域设置(通过省略相应的参数或传递 NULL)。这些是 ICU 区域设置,而不是系统区域设置。内置 ICU 区域设置及其数据可以在 » 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

我的测试脚本,

ua_UK
是正确的区域设置

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

$formatter->setPattern('MMMM');

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

造成这种行为的原因是什么?

php docker alpine-linux icu intl
2个回答
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 秒优化到一些合理的数字。


0
投票

我还不能发表评论,所以我就写在这里。对于那个很抱歉! 我可以确认这对我有用!我花了 2 天的时间四处寻找解决方案,这是在使用 Alpine linux 和 php-fpm 时唯一适用于日期和货币格式的解决方案。

这是我的最终 Dockerfile 片段:

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
© www.soinside.com 2019 - 2024. All rights reserved.