1
votes

I'm desperate finding a solution why gettext is not translating. I followed multiple tutorials and have no clue why it is not working. Locales are installed, gettext too, activated, loaded in php.ini and still not working. Currently I'm trying to translate from english to czech.

Dockerfile

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

Languages should be installed corrently

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

File structure

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

messages.po in cs translation

# 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 <[email protected]>\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 is generated via 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");

It returns

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

Is there any other way how to debug gettext? Am I missing something? Thank you!

1

1 Answers

1
votes

So the culprit was LANGUAGE, as debian looks up this variable first, even before LC_ALL!

putenv("LANGUAGE=$locale");