简体   繁体   English

Heroku 上的 Python 语言环境设置

[英]Python locale settings on Heroku

I'm developing a python web application on Heroku and I'm facing a problem with the locale settings.我正在Heroku上开发一个 python Web 应用程序,但我遇到了语言环境设置的问题。

My aim ist to format a python datetime object as a string like this我的目标是将 python datetime对象格式化为这样的字符串

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.locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')用于特定语言。

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 .在我的本地机器上,这有效,我得到了正确语言的日期,但在 Heroku 上它失败了,我得到的只是locale.Error: unsupported locale settings

Am I doing something wrong or is it permitted to change locale setting in a python app on Heroku?我做错了什么还是允许在 Heroku 上的 python 应用程序中更改区域设置?

Thanks.谢谢。

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

Only English locales are installed on the Heroku environment by default.默认情况下,在 Heroku 环境中仅安装英语语言环境。 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.您最好的选择是为您支持的语言实现您自己的格式化功能。

This is an older question, but I thought it worth mentioning here that Heroku added support for installing additional locales in September 2018 .这是一个较旧的问题,但我认为这里值得一提的是Heroku 在 2018 年 9 月增加了对安装其他语言环境的支持

To do this, commit a .locales file containing the locales you'd like to set up:为此,请提交一个.locales文件,其中包含您要设置的语言环境:

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 .有关更多信息,请查看buildpack 的 GitHub 存储库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM