Python locale settings on Heroku
Asked Answered
L

3

5

I'm developing a python web application on Heroku and I'm facing a problem with the locale settings.

My aim ist to format a python datetime object as a string like this

import datetime
now = datetime.datetime.now()
print now.strftime('%a %d %B %Y')  # output: Sat 14 July 2012

but in different languages.

On my local machine I use therefore:

import locale
locale.setlocale(locale.LC_ALL, '')

or locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') for specific languages.

On my local machine this works and I get the date in the right language but on Heroku it fails and all I get is a locale.Error: unsupported locale settings.

Am I doing something wrong or is it permitted to change locale setting in a python app on Heroku?

Thanks.

Lochner answered 14/7, 2012 at 18:6 Comment(0)
D
3

Only English locales are installed on the Heroku environment by default. So far there seems to be no way to install additional locales. Your best bet will be to implement your own formatting functions for the languages you support.

Dynamism answered 9/9, 2012 at 1:9 Comment(2)
They have more now. I believe the problem was in the locale name as it was in my case.Dendritic
This isn't true anymore, see Craig Anderson's answer which points to heroku-buildpack-localeDiastyle
D
17

You can see available locales by running:

$ heroku run "locale -a"
Running `locale -a` attached to terminal... up, run.5061
aa_DJ.utf8
aa_ER
aa_ER@saaho
aa_ET
af_ZA.utf8
am_ET
an_ES.utf8
ar_AE.utf8
ar_BH.utf8
ar_DZ.utf8
ar_EG.utf8
ar_IN
ar_IQ.utf8
ar_JO.utf8
ar_KW.utf8
ar_LB.utf8
ar_LY.utf8
ar_MA.utf8
ar_OM.utf8
ar_QA.utf8
ar_SA.utf8
ar_SD.utf8
ar_SY.utf8
ar_TN.utf8
ar_YE.utf8
as_IN
ast_ES.utf8
az_AZ
be_BY@latin
be_BY.utf8
ber_DZ
ber_MA
bg_BG.utf8
bn_BD
bn_IN
bo_CN
bo_IN
br_FR.utf8
bs_BA.utf8
C
ca_AD.utf8
ca_ES.utf8
ca_ES.utf8@valencia
ca_FR.utf8
ca_IT.utf8
crh_UA
csb_PL
cs_CZ.utf8
cy_GB.utf8
da_DK.utf8
de_AT.utf8
de_BE.utf8
de_CH.utf8
de_DE.utf8
de_LI.utf8
de_LU.utf8
dv_MV
dz_BT
el_CY.utf8
el_GR.utf8
en_AG
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_NG
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZW.utf8
eo_US.utf8
eo.utf8
es_AR.utf8
es_BO.utf8
es_CL.utf8
es_CO.utf8
es_CR.utf8
es_DO.utf8
es_EC.utf8
es_ES.utf8
es_GT.utf8
es_HN.utf8
es_MX.utf8
es_NI.utf8
es_PA.utf8
es_PE.utf8
es_PR.utf8
es_PY.utf8
es_SV.utf8
es_US.utf8
es_UY.utf8
es_VE.utf8
et_EE.utf8
eu_ES.utf8
eu_FR.utf8
fa_IR
fi_FI.utf8
fil_PH
fo_FO.utf8
fr_BE.utf8
fr_CA.utf8
fr_CH.utf8
fr_FR.utf8
fr_LU.utf8
fur_IT
fy_DE
fy_NL
ga_IE.utf8
gd_GB.utf8
gl_ES.utf8
gu_IN
ha_NG
he_IL.utf8
hi_IN
hne_IN
hr_HR.utf8
hsb_DE.utf8
ht_HT
hu_HU.utf8
hy_AM
ia
id_ID.utf8
ig_NG
is_IS.utf8
it_CH.utf8
it_IT.utf8
iu_CA
ja_JP.utf8
ka_GE.utf8
kk_KZ.utf8
km_KH
kn_IN
ko_KR.utf8
ks_IN
ks_IN@devanagari
ku_TR.utf8
kw_GB.utf8
ky_KG
la_AU.utf8
lg_UG.utf8
li_BE
li_NL
lo_LA
lt_LT.utf8
lv_LV.utf8
mai_IN
mg_MG.utf8
mi_NZ.utf8
mk_MK.utf8
ml_IN
mn_MN
mr_IN
ms_MY.utf8
mt_MT.utf8
nan_TW@latin
nb_NO.utf8
nds_DE
nds_NL
ne_NP
nl_AW
nl_BE.utf8
nl_NL.utf8
nn_NO.utf8
nr_ZA
nso_ZA
oc_FR.utf8
om_ET
om_KE.utf8
or_IN
pa_IN
pap_AN
pa_PK
pl_PL.utf8
POSIX
pt_BR.utf8
pt_PT.utf8
ro_RO.utf8
ru_RU.utf8
ru_UA.utf8
rw_RW
sa_IN
sc_IT
sd_IN
sd_IN@devanagari
se_NO
shs_CA
si_LK
sk_SK.utf8
sl_SI.utf8
so_DJ.utf8
so_ET
so_KE.utf8
so_SO.utf8
sq_AL.utf8
sr_ME
sr_RS
sr_RS@latin
ss_ZA
st_ZA.utf8
sv_FI.utf8
sv_SE.utf8
ta_IN
te_IN
tg_TJ.utf8
th_TH.utf8
ti_ER
ti_ET
tk_TM
tlh_GB.utf8
tl_PH.utf8
tn_ZA
tr_CY.utf8
tr_TR.utf8
ts_ZA
tt_RU
tt_RU@iqtelif
ug_CN
uk_UA.utf8
ur_PK
uz_UZ@cyrillic
uz_UZ.utf8
ve_ZA
vi_VN
wa_BE.utf8
wo_SN
xh_ZA.utf8
yi_US.utf8
yo_NG
zh_CN.utf8
zh_HK.utf8
zh_SG.utf8
zh_TW.utf8
zu_ZA.utf8

To fix your issue try

locale.setlocale(locale.LC_ALL, 'de_DE.utf8')

or

heroku config:add LANG=de_DE.utf8
Dendritic answered 17/11, 2013 at 22:0 Comment(2)
Do you still have so many locales available? I only have a handful, all en_* it seems.Thacher
Yeah, now only en_* are listed.Dendritic
D
3

Only English locales are installed on the Heroku environment by default. So far there seems to be no way to install additional locales. Your best bet will be to implement your own formatting functions for the languages you support.

Dynamism answered 9/9, 2012 at 1:9 Comment(2)
They have more now. I believe the problem was in the locale name as it was in my case.Dendritic
This isn't true anymore, see Craig Anderson's answer which points to heroku-buildpack-localeDiastyle
E
2

This is an older question, but I thought it worth mentioning here that Heroku added support for installing additional locales in September 2018.

To do this, commit a .locales file containing the locales you'd like to set up:

de_DE
fr_FR

Then add the locale buildpack:

heroku buildpacks:add https://github.com/heroku/heroku-buildpack-locale

For more information, check the buildpack's GitHub repository.

Euphorbia answered 16/10, 2018 at 11:38 Comment(1)
One thing to note: while the buildpack makes the locale available for the app, if you need the locale during Heroku build time (for example, i'm running a django command in bin/post_compile), you'll need to source the .profile.d/000_locale.sh file created by the buildpackDiastyle

© 2022 - 2024 — McMap. All rights reserved.