使用Docker在Laravel中设置本地环境

4

你好,我的Laravel项目在Docker中运行。但是当我使用setlocal来设置我的日期为本地语言时,Laravel会以英文显示结果。

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

我相信这个问题是由容器引起的,但我不知道如何解决。

enter image description here

2个回答

5

如果你在主机 PC 上输入 locale -a,你一定会发现像 fr_FR 这样的东西。但是在容器中,默认情况下,大多数镜像都不提供法语语言。

以下是一个最小化的示例,供你安装法语语言参考:

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

执行:

$ 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

说明:

  • 默认情况下,/etc/locale.gen 中的 fr_FR 被注释掉了,我们可以使用 sed 命令来取消注释。
  • 接下来,使用 locale-gen 命令生成法语语言环境。
  • 安装完法语语言环境后,运行 PHP 文件时可以得到日期为 mardi 22 juin 2021

1
感谢您提供的良好解释和花费的时间。 - Twistere

1
确保您的Docker镜像中安装了locales软件包。
RUN apt-get update && apt-get install -y locales

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接