Python 语言环境在 alpine linux 上不起作用

y-s*_*een 8 python locale docker alpine-linux python-3.7

代码很简单:

import locale
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') # I tried de_DE and de_DE.utf8 too
locale.currency(0)

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.7/locale.py", line 267, in currency
    raise ValueError("Currency formatting is not possible using "
ValueError: Currency formatting is not possible using the 'C' locale.
Run Code Online (Sandbox Code Playgroud)

当我在 ubuntu 上运行它时它有效。然而,在高山上,会弹出此错误。我尝试了此评论中的解决方法,但没有成功。我还添加/usr/glibc-compat/binPATH该脚本之上,但没有帮助。

有什么办法可以让语言环境在高山上工作吗?

自己尝试一下:

docker run --rm alpine sh -c "apk add python3; python3 -c 'import locale; locale.setlocale(locale.LC_ALL, \"de_DE.UTF-8\"); locale.currency(0)'"
Run Code Online (Sandbox Code Playgroud)

更新:这个仓库也不起作用。

更新:我也尝试过这个指南,但它似乎与 python 不兼容?即使语言环境确实出现了,我仍然得到这个:

/app # locale -a
C
C.UTF-8
sv_SE.UTF-8
en_GB.UTF-8
ch_DE.UTF-8
pt_BR.UTF-8
ru_RU.UTF-8
it_IT.UTF-8
de_CH.UTF-8
en_US.UTF-8
fr_FR.UTF-8
nb_NO.UTF-8
de_DE.UTF-8 <--
nl_NL.UTF-8
es_ES.UTF-8
/app # python
Python 3.7.7 (default, Apr 24 2020, 22:09:29) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
'de_DE.UTF-8'
>>> locale.currency(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/locale.py", line 267, in currency
    raise ValueError("Currency formatting is not possible using "
ValueError: Currency formatting is not possible using the 'C' locale.
Run Code Online (Sandbox Code Playgroud)

小智 3

这个最小的 Dockerfile

FROM alpine:3.12

ENV MUSL_LOCPATH="/usr/share/i18n/locales/musl"

RUN apk --no-cache add \
    musl-locales \
    musl-locales-lang \
    python3
Run Code Online (Sandbox Code Playgroud)

通过使用上面提到的 musl-locales 包,目前似乎仅对带有 Python 的 Alpine Linux 部分起作用:

  1. LC_TIME:成功
import locale
from time import gmtime, strftime
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Wed, 26 Aug 2020 16:50:17 +0000'
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Mi, 26 Aug 2020 16:50:31 +0000'
Run Code Online (Sandbox Code Playgroud)
  1. LC_MONETARY:失败
import locale
locale.getlocale()
('en_US', 'UTF-8')
locale.currency(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/locale.py", line 267, in currency
    raise ValueError("Currency formatting is not possible using "
ValueError: Currency formatting is not possible using the 'C' locale.
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
locale.currency(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/locale.py", line 267, in currency
    raise ValueError("Currency formatting is not possible using "
ValueError: Currency formatting is not possible using the 'C' locale.
Run Code Online (Sandbox Code Playgroud)
  1. LC_NUMERIC:失败
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
locale.format_string("%.1f", 1.4)
'1.4'
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
locale.format_string("%.1f", 1.4)
'1.4'
Run Code Online (Sandbox Code Playgroud)
  1. 这看起来也很糟糕:
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
locale.localeconv()
{'int_curr_symbol': '', 'currency_symbol': '', 'mon_decimal_point': '', 'mon_thousands_sep': '', 'mon_grouping': [], 'positive_sign': '', 'negative_sign': '', 'int_frac_digits': 127, 'frac_digits': 127, 'p_cs_precedes': 127, 'p_sep_by_space': 127, 'n_cs_precedes': 127, 'n_sep_by_space': 127, 'p_sign_posn': 127, 'n_sign_posn': 127, 'decimal_point': '.', 'thousands_sep': '', 'grouping': []}
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
locale.localeconv()
{'int_curr_symbol': '', 'currency_symbol': '', 'mon_decimal_point': '', 'mon_thousands_sep': '', 'mon_grouping': [], 'positive_sign': '', 'negative_sign': '', 'int_frac_digits': 127, 'frac_digits': 127, 'p_cs_precedes': 127, 'p_sep_by_space': 127, 'n_cs_precedes': 127, 'n_sep_by_space': 127, 'p_sign_posn': 127, 'n_sign_posn': 127, 'decimal_point': '.', 'thousands_sep': '', 'grouping': []}
Run Code Online (Sandbox Code Playgroud)

本地化失败可能是由于https://gitlab.com/rilian-la-te/musl-locales中的 PO 文件不完整,或者由于未满足特定的 Python 期望。

接下来,有人可以使用另一种编程语言(例如 PHP)检查使用区域设置的函数是否按预期工作。