Python locale not working on alpine linux

Viewed 3049

The code is simple:

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.

It works when I run it on ubuntu. On alpine, however, this error pops up. I tried the workaround from this comment without success. I also added /usr/glibc-compat/bin to PATH on top of that script, didn't help.

Is there any way to make locales work on alpine?

Try it out yourself:

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

Update: this repo also doesn't work.

Update: I tried this guide too, but it seems like it's not compatible with python? Even though the locale does show up, I still get this:

/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.
1 Answers

This minimum Dockerfile

FROM alpine:3.12

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

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

by using the above mentioned musl-locales packages seem to be partly working only at the moment for Alpine Linux with Python:

  1. LC_TIME: Succeeds
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: Fails
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: Fails
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. This also looks bad:
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': []}

The failing localisations might be due to incomplete PO files in https://gitlab.com/rilian-la-te/musl-locales or due to specific Python expectations which have not been met.

Next, someone could check with another programming language like PHP if functions using locales work as expected.

Related