【问题标题】:Python locale not working on alpine linuxPython 语言环境不适用于 alpine linux
【发布时间】:2020-08-28 20:19:01
【问题描述】:

代码很简单:

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.

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

有什么方法可以让语言环境在 alpine 上工作?

自己试试吧:

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)'"

更新:this repo 也不起作用。

更新:我也试过this指南,但它似乎与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.

【问题讨论】:

标签: python docker locale python-3.7 alpine


【解决方案1】:

这个最小的 Dockerfile

FROM alpine:3.12

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

RUN apk --no-cache add \
    musl-locales \
    musl-locales-lang \
    python3

通过使用上面提到的 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'
  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.
  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'
  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': []}

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

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

【讨论】:

    猜你喜欢
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 2020-04-05
    • 1970-01-01
    相关资源
    最近更新 更多