在Web应用程序中使用Python语言环境或等效内容? [英] Using Python locale or equivalent in web applications?

查看:88
本文介绍了在Web应用程序中使用Python语言环境或等效内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的语言环境实现似乎想要从系统设置中读取语言环境,或者通过setlocale调用进行设置。这两个都不适用于我,因为我想使用Web应用程序中的功能,其中所需的区域设置是用户的区域设置。



并且在区域设置文档,使整个事情变得可怕:


除此之外,一些实现
被破坏,频繁的
语言环境更改可能会导致核心转储。
这使得区域设置有点痛苦
正确使用



在某些库例程
中调用
setlocale()通常是一个坏主意,因为副作用影响
全部程序


那么,是否有合理的区域设置替代在网络应用程序中使用? Babel 还是还有其他选择?我正在寻找可以处理货币以及日期和数字的东西。



[更新]为了澄清,我对日期,数量和货币最感兴趣

解决方案

locale 对任何应用程序都没有好处需要支持多个区域设置 - 这些应用程序的设计非常糟糕(基本上是任何服务器端应用程序,包括Web应用程序)。在可行的情况下, PyICU 是一款非常优秀的解决方案 - 顶级i18n / L10n支持,速度,灵活性(缺点:虽然ICU的文档很好,PyICU的,呃,不是那么多;-)。唉,并不总是允许你部署你自己的扩展...: - (。



特别是,我还在寻找一个坚实的i18n / L10n解决方案App Engine应用程序 - 翻译本身是最少的问题(您可以切换到正确的模板集),问题是还有许多其他L10n方面(ICU支持的方面,如排序规则我猜想已经提到的 Babel 是唯一合适的开始的地方来自。


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 to use the capabilities in a web application, where the desired locale is the user's locale.

And there are warnings in the locale docs that make the whole thing scary:

On top of that, some implementation are broken in such a way that frequent locale changes may cause core dumps. This makes the locale somewhat painful to use correctly

And

It is generally a bad idea to call setlocale() in some library routine, since as a side effect it affects the entire program

So, is there a reasonable locale alternative for use in web apps? Is Babel it or are there other alternatives? I'm looking for something that will handle currencies as well as dates and numbers.

[Update] To clarify, I'm most interested in date, number, and currency formatting for various locales.

解决方案

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.

这篇关于在Web应用程序中使用Python语言环境或等效内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆