locale — 国际化服务

源代码: Lib/locale.py


The locale 模块打开对 POSIX 区域设置数据库和功能的访问。POSIX 区域设置机制允许程序员处理某些应用程序文化问题,不要求程序员知道每个执行软件国家的所有细节。

The locale 模块的实现是基于 _locale 模块,又使用了 ANSI C 区域设置实现,若可用。

The locale 模块定义了下列异常和函数:

exception 区域设置。 Error

引发异常当区域设置被传递给 setlocale() 无法识别。

区域设置。 setlocale ( category , locale = None )

若 locale 有给定且不是 None , setlocale() 把区域设置修改为 category . The available categories are listed in the data description below. locale may be a string, or an iterable of two strings (language code and encoding). If it’s an iterable, it’s converted to a locale name using the locale aliasing engine. An empty string specifies the user’s default settings. If the modification of the locale fails, the exception Error 被引发。若成功,返回新的区域设置。

若 locale 被省略或 None ,当前设置为 category 被返回。

setlocale() 在大多数系统中是非线程安全的。应用程序开始通常以调用

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