docker php gettext 未翻译

docker php gettext not translating

我很想找到 gettext 无法翻译的解决方案。我遵循了多个教程,但不知道为什么它不起作用。安装了区域设置,也安装了 gettext,激活,加载 php.ini 但仍然无法正常工作。目前我正在尝试将英语翻译成捷克语。

Docker 文件

FROM php:7.4-apache
RUN apt-get update && apt-get install -y zlib1g-dev libicu-dev g++ locales gettext

RUN docker-php-ext-configure intl \
    && docker-php-ext-configure gettext \
    && docker-php-ext-install \
    intl \
    gettext
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    sed -i -e 's/# cs_CZ.UTF-8 UTF-8/cs_CZ.UTF-8 UTF-8/' /etc/locale.gen && \
    dpkg-reconfigure --frontend=noninteractive locales && \
    update-locale LANG=en_US.UTF-8
ENV LC_ALL en_US.UTF-8 
ENV LANGUAGE cs_CZ:en_US:en
ENV LANG en_US.UTF-8 
RUN docker-php-ext-install pdo
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-enable intl gettext
RUN a2enmod rewrite
RUN service apache2 restart
RUN apache2ctl restart
EXPOSE 80

应正确安装语言

$ locale -a
C
cs_CZ.utf8
C.UTF-8
en_US.utf8
POSIX

文件结构

index.php
│
locale   
│
└───cs_CZ.utf8
│   │
│   └───LC_MESSAGES
│       │   messages.mo
│       │   messages.po
│   
└───en_US.utf8
│   │
│   └───LC_MESSAGES
│       │   messages.mo
│       │   messages.po

messages.po在cs翻译

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-10-05 10:15+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: cs_CZ.utf8\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"

#: index.php:42
msgid "HELLO_WORLD"
msgstr "Ahoj světe"

#: index.php:43
msgid "TEST_TRANSLATION"
msgstr "Testovací překlad"

messages.po 是通过 msgfmt

生成的

index.php

$locale = 'cs_CZ.utf8';
$domain = 'messages';
setlocale(LC_TIME, "");
$results = putenv("LC_ALL=$locale");
if (!$results) {
    exit('putenv failed');
}

$results = setlocale(LC_ALL, $locale);
if (!$results) {
    exit('setlocale failed: locale function is not available on this platform, or the given local does not exist in this environment');
}

$results = bindtextdomain($domain, "locale/$locale");
echo 'new text domain is set: ' . $results . "\n";

$results = textdomain($domain);
echo 'current message domain is set: ' . $results . "<br>";

echo _("HELLO_WORLD");
echo "<br>";
echo _("TEST_TRANSLATION");

它returns

new text domain is set: /var/www/html/locale/cs_CZ.utf8 current message domain is set: messages
HELLO_WORLD
TEST_TRANSLATION

有没有其他方法可以调试gettext?我错过了什么吗?谢谢!

所以罪魁祸首是 LANGUAGE,因为 debian 会首先查找这个变量,甚至在 LC_ALL!

之前
putenv("LANGUAGE=$locale");