Php on docker : Using setLocale
Asked Answered
M

4

6

I'm trying to migrate an existing Apache/php site to docker and have problem with site localization. Digging into the code, the problem is that setLocale is returning false on Docker install (and true on the existing site). Here is a php test that runs well on existing site and fails on Docker installation.

<?php
$locale = "fr_FR";
putenv("LC_ALL=$locale");
$ok = setlocale(LC_ALL, $locale);
if ($ok) {
  echo "success";
} else {
  echo "failure";
}
?>

Here is my Docker file:

FROM php:5-apache

RUN apt-get update && apt-get install -y locales && apt-get clean
RUN locale-gen fr_FR && locale-gen zh_TW && locale-gen tr_TR && locale-gen ru_R$
RUN docker-php-ext-install gettext

RUN a2enmod rewrite && a2enmod headers

What am I doing wrong?

Monoacid answered 3/9, 2016 at 16:53 Comment(1)
Could you please remove the "answer" part from your question and post it as a separate answer? You can answer your own questions here.Satsuma
H
2

You need to reconfigure your locales:

RUN locale-gen fr_FR.UTF-8 && dpkg-reconfigure locales

And you might need (but I don't know exactly under which circumstances) to add LC_ALL and LANGUAGE environment variables to /etc/environment:

LC_ALL=...
LANGUAGE=...
Happen answered 4/9, 2016 at 14:12 Comment(3)
Thanks, you gave me the right direction. It seems locale-gen does not work, but spkg-reconfigure locales did the trick (see my edit in my question). LC_ALL has to be set in the php script like in my php example.Monoacid
For the php:*-apache images it seems to be not possible to generate locales by locale-gen <locale> but you need to edit the /etc/locales.gen file and then run locale-genPogue
Thanks @monofone. But it's /etc/locale.gen not locales.genGooseberry
S
2

In my situation, I could not generate locales by simply calling RUN locale-gen fr_FR.UTF-8, I had to add the specific locales to the /etc/locale.gen file and then run the locale-gen :

RUN echo 'fr_CA.UTF-8 UTF-8' >> /etc/locale.gen && \
    echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen  && \
    locale-gen

The following command showed me the good locales available to the system :

root@df6edda12ef7:/var/www/html# locale -a
C
C.UTF-8
POSIX
en_US.utf8
fr_CA.utf8

If the problem is with PHP setlocale() do not forget to restart your web server (apache2) of apache2-foreground Docker container for the locales to be applied.

Sha answered 8/1, 2020 at 17:28 Comment(0)
M
1

As requested by "That Brazilian Guy", here is an answer with the fixed docker file

FROM php:5-apache

RUN apt-get update && apt-get install -y locales && apt-get clean
RUN sed -i -e 's/# fr_FR ISO-8859-1/fr_FR ISO-8859-1/' /etc/locale.gen && \
    dpkg-reconfigure --frontend=noninteractive locales
RUN docker-php-ext-install gettext

RUN a2enmod rewrite && a2enmod headers
If you need more locales, you'll have to look at /etc.locale.gen in your container and add a line

sed -i -e 's/# locale/locale/' /etc/locale.gen && \

for every locale you need (assuming # locale is the content of a line in /ec tc/locale.gen containing the locale you need).

Monoacid answered 23/1, 2019 at 9:23 Comment(0)
S
0

Ubuntu in Docker doesn't ship with locales.. i dont know why. You have to add this to your Dockerfile:

RUN locale-gen fr_FR.UTF-8  
ENV LANG fr_FR.UTF-8  
ENV LANGUAGE fr_FR:en  
ENV LC_ALL fr_FR.UTF-8 

This should do it.

Sortie answered 4/9, 2016 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.