I've checked on the questions regarding this issue, they did not help (see what I tried below).
I setup a multi-container app on docker with a httpd:2.4 (apache) container acting as reverse-proxy forwarding requests to php files to my php-fpm container.
Everything in this setup works perfectly, with the exception of gettext translations. I simply cannot get the Docker container to return a gettext translation in the locale en_GB.
Setup:
- The
.mofiles are present in a folder successfully mounted into myphp-fpmcontainer, at/var/www/php/translations, with the structure:
en_GB.utf8
LC_MESSAGES
first.mo
second.mo
- In my php code copied into my container, I call this before trying to fetch a gettext translation via
_()ordgettext():
bindtextdomain('first', '/var/www/php/translations');
bindtextdomain('second', '/var/www/php/translations');
What I've tried:
- Runnig
locale -ain the runningphp-fpmcontainer returns:
C
C.utf8
POSIX
en_GB.utf8
- I tried to change the language folders to
e.g. en_GB.UTF-8 - I tried to additionally call:
bind_textdomain_codeset('first', 'UTF-8');
bind_textdomain_codeset('second', 'UTF-8');
var_dumps() of each of the following methods when run in the container echo:
setlocale(LC_ALL, "en_GB.UTF-8")>en_GB.UTF-8setlocale(LC_ALL, "en_GB.utf8")>en_GB.utf8textdomain('first')>firsttextdomain('second')>second
Still, it is not working (I've also double-checked that the msgid is properly setup and translated, and even re-generated .mo files over and over again).
Note: Nothing in relation to locales is done on the apache container, but that should be fine, as it is a completely separate container?
Note: This exact setup works when deployed on an actual VM with apache as reverse proxy with php running as fastcgi behind it. So I am totally clueless what I am missing.
Dockerfile of my php-fpm container (relevant parts) :
FROM php:8.2-fpm
RUN <<EOF
apt-get update
apt-get install -y locales gettext libgettextpo-dev
docker-php-ext-install gettext
apt-get install -y libzip-dev unzip
docker-php-ext-install zip
docker-php-ext-install pdo_mysql
EOF
# Generate the locale(s) you need
RUN echo "en_GB.utf8 utf8" >> /etc/locale.gen && \
echo "de_CH.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
# Set environment variables so PHP uses the locale
ENV LANG=en_GB.utf8
ENV LANGUAGE=en_GB:en
ENV LC_ALL=en_GB.utf8
More details:
PHP Code with which I'm trying to load the translations ATM:
$locale_value = 'en_GB';
putenv("LANGUAGE=$locale_value.utf8");
putenv("LC_LANGUAGE=$locale_value.utf8");
putenv("LANG=$locale_value.utf8");
putenv("LC_LANG=$locale_value.utf8");
putenv("LC_ALL=$locale_value.utf8");
setlocale(
LC_ALL,
"$locale_value.utf8"
);
// Load text domains
bindtextdomain(
domain : 'first',
directory: '/var/www/php/translations'
);
bindtextdomain(
domain : 'second',
directory: '/var/www/php/translations'
);
// Set default textdomain to first textdomain
textdomain('first');
I then try to access my gettext translations via:
_('Hallo')
... for first, or:
dgettext('second','Hallo')
... for second.
The result is that simply the msgid is always returned, in the gettext default language (so Hallo instead of Hello, no matter if all of the calls succeed as explained in my var_dumps shown above).
I've initially only tried to load the language via:
putenv("LC_ALL=$locale_value.utf8");
setlocale(
LC_ALL,
"$locale_value.utf8"
);
... but am so done with this issue patience wise that I simply set pretty much each global variable I'm reading about in relation to this issue.
I've then also tried to replace the /var/www/php/translations directory in the calls above with /usr/lib/locale, as that appears to be php-fpms native locale directory for LC_MESSAGES for the docker image I'm pulling. No effect though, same outcome.
PS: If that matters, the php-fpm image is built on top of Debian, as you can see in the link of the image my dockerfile uses.
Then, if this matters, the way I am implementing the reverse-proxy logic over apache is by using the following in the apache config that is copied into my apache docker container:
<FilesMatch \.php$>
ProxyFCGISetEnvIf "true" SCRIPT_FILENAME "/var/www/html%{reqenv:SCRIPT_NAME}"
SetHandler "proxy:fcgi://php-fpm:9000"
</FilesMatch>
(With the name of the php-fpm container being php-fpm, and the port the image exposes being 9000).
Again everything works, the entire php application, except from the gettext thing, as mentioned above.
_("Hello")and it returns"Hello"? What's in your .mo file? What does your PHP code look like? I would leave out the docker stuff and concentrate on what you're doing in PHP first.