Using Python locale or equivalent in web applications?

前端 未结 4 578
天命终不由人
天命终不由人 2021-02-02 03:36

Python\'s locale implementation seems to want to either read the locale from system settings or have it be set via a setlocale call. Neither of these work for me since I\'d like

相关标签:
4条回答
  • 2021-02-02 04:01

    Your best approach will be to setlocale on the locale that the browser passes you, if you're doing currencies, dates, and numbers. There's a lot of zomgz warnings in the Python documentation for really off-color platforms; most of these can be ignored.

    "Frequent locale changes" shouldn't matter, unless I'm missing something.

    You're not doing message catalogs or anything fancy, so stick with what Python gives you.

    0 讨论(0)
  • 2021-02-02 04:04

    Don't use setlocale.

    Check how it is done in django. It looks like they use class api of gettext library and do not use the setlocale function I bet there is a reason for this.

    They manually store a translation per thread check here how it is implemented (gettext function and _active dictionary).

    0 讨论(0)
  • 2021-02-02 04:16

    locale is no good for any app that needs to support several locales -- it's really badly designed for those apps (basically any server-side app, including web apps). Where feasible, PyICU is a vastly superior solution -- top-quality i18n/L10n support, speed, flexibility (downside: while ICU's docs are good, PyICU's, well, not so much;-). Alas, not always are you allowed to deploy your own extensions...:-(.

    In particular, I'm still looking for a solid i18n/L10n solution for App Engine apps -- "translation" per se is the least of issues (you can just switch to the right set of templates), the problem is that there are many other L10n aspects (the ones that ICU supports so well, such as collation rules for example, etc, etc). I guess the already-mentioned Babel is the only sensible place to start from.

    0 讨论(0)
  • 2021-02-02 04:20

    Django's i18n framework works out the shortcomings of setlocale() by not using it. This way the locale is set per request and if you use LocaleMiddleware it can be set to change according to UserAgent Accept-Language setting. See the docs.

    0 讨论(0)
提交回复
热议问题