setlocale in Laravel with docker

Viewed 366

Hello my Laravel project runs with docker. But when I set up my date with setlocal in my language. Laravel displays the result in English @php setlocale(LC_ALL, 'fr_FR'); echo strftime("%A %d %B %Y");@endphp

I'm pretty sure this issue is cause by the container, but I don't know how to fix it.

enter image description here

2 Answers

If you input locale -a on you host pc, you will surely find something like fr_FR. But in container, by default, most of image won't provide French lang.

Next is a minimal example for you to install french lang, FYI:

test.php:

<?php setlocale(LC_ALL, 'fr_FR'); echo strftime("%A %d %B %Y");

Dockerfile:

FROM php

COPY test.php /

RUN apt-get update; \
    apt-get install -y locales; \
    sed -i '/^#.* fr_FR.* /s/^#//' /etc/locale.gen; \
    locale-gen

RUN locale -a
RUN php /test.php

Execution:

$ docker build -t abc:1 .
Sending build context to Docker daemon  3.072kB
Step 1/5 : FROM php
 ---> 7a9e4bdd6171
Step 2/5 : COPY test.php /
 ---> 705d4f7695e4
Step 3/5 : RUN apt-get update;     apt-get install -y locales;     sed -i '/^#.* fr_FR.* /s/^#//' /etc/locale.gen;     locale-gen
 ---> Running in 1c9d6012e7b9
...
Generating locales (this might take a while)...
Generation complete.
Generating locales (this might take a while)...
  fr_FR.ISO-8859-1... done
  fr_FR.UTF-8... done
  fr_FR.ISO-8859-15@euro... done
Generation complete.
Removing intermediate container 1c9d6012e7b9
 ---> ff1733e34ad0
Step 4/5 : RUN locale -a
 ---> Running in bd21cd6c14e8
C
C.UTF-8
POSIX
fr_FR
fr_FR.iso88591
fr_FR.iso885915@euro
fr_FR.utf8
fr_FR@euro
french
Removing intermediate container bd21cd6c14e8
 ---> 699d9918f95f
Step 5/5 : RUN php /test.php
 ---> Running in 49316730728c
mardi 22 juin 2021Removing intermediate container 49316730728c
 ---> c8cc561fd306
Successfully built c8cc561fd306
Successfully tagged abc:1

Explanation:

  • By default, fr_FR is commented in /etc/locale.gen, we could use sed to uncomment it.
  • Next, use locale-gen to generate French lang.
  • After french lang installed in your container, you could get mardi 22 juin 2021 when run the php file.

Make sure the locales package is installed in your docker image.

RUN apt-get update && apt-get install -y locales
Related