Pytz时区和地区的国际化
l18n的Python项目详细描述
版权所有2014-2016 Thomas Khyn
语言环境国际化包地点、时区的翻译…
使用最新的python 2和3小版本进行测试。
支持的语言:英语、法语、德语、捷克语、中文 (want to add yours?)
L18N是什么?
您可能已经注意到,l18n是i18n和l10n的缩写, 即“国际化”和“本地化”。它基本上提供了 用于本地化目的的名称的惰性翻译(例如,地点和 时区)
当我在寻找pytz的翻译时,我开始写l18n。 图书馆。实际上,在一个多语种的网站上,用户可以选择时区 他们在,如果他们能用他们的语言来选择就更好了,就像在一些 案例中,与英文名称的差异可能很大,因此 按字母顺序排序时查找的位置。
由于我很懒,我想到了一种几乎自动获取 来自CLDR(unicode的公共区域设置数据存储库)数据库的翻译。
集成功能将时区与国家/地区代码链接,没有理由不 为后者提供翻译。在不久的将来,我- 或贡献者-也可以添加从中获取的货币或度量单位 CLDR数据库…
它是如何工作的?
要使用l18n,首先需要安装它。它与pip:
pip install l18n
然后,在您的代码中:
>>> import l18n
l18n公开几个类似于只读字典的对象:
L18N.TZ_城市
is a mapping between all the timezones listed in ^{tt8}$ and human-friendly lazy versions of the translated name of the city in the current language (see Selecting the language below). For example, if the language is English:
>>> l18n.tz_cities['Pacific/Easter'] L18NLazyString <Easter Island> >>> str(l18n.tz_cities['Pacific/Easter']) 'Easter Island'In French, it would give:
>>> str(l18n.tz_cities['Pacific/Easter']) 'Île de Pâques'
L18N.TZ_全名
is a mapping between all the timezones listed in ^{tt8}$ and lazy versions of the timezones’ full names in the current language. For example:
>>> str(l18n.tz_fullnames['Pacific/Easter']) 'Pacific/Easter Island' # or 'Pacifique/Île de Pâques' in FrenchIt is interesting to note that for 3-components timezone names where the local state or territory appears in the city name, ^{tt1}$ cleverly strips this information so that it is not repeated:
>>> str(l18n.tz_fullnames['America/North_Dakota/New_Salem']) 'America/North Dakota/New Salem'indeed:
>>> str(l18n.tz_cities['America/North_Dakota/New_salem']) 'New Salem, North Dakota'
L18N.地区
is a mapping between the territory codes as defined in the CLDR and their localized names, lazily defined. For example:
>>> str(l18n.territories['CZ']) 'Czech Republic' # or 'République Tchèque' in French
注意
每次计算值时都会转换这些值,不存在缓存。 这意味着相同的l18nlazystring/l18nlazystringslist实例可以 如果将语言更改为 评估。
注意
上面提到的字典中的值可以被 自己翻译字典不是只读的,值可以是 在您方便的时候添加或删除
惰性映射特殊功能(v.2016.6.3以后的版本)
前面提到的tz_cities、tz_fullnames和territories不是 简单的字典和提供额外的功能。
排序
当在L18NMap上迭代时,项、键或值在 当前选定语言中的字母顺序。为了表演, 结果按语言缓存,因此每种语言只执行一次排序。 注意,这些值仍然是惰性对象,只有在 呈现为字符串。
子集
使用现有的方法,可以从现有的一个生成新的^ {TT14} $ subset方法并传递需要保存在中的keys的iterable 新地图。任何缓存排序也用于生成新缓存,因此 在新的子集中没有要重新计算的内容。
例如,可以为 pytz.common_timezones:
>>> common_cities = l18n.tz_cities.subset(pytz.common_timezones.keys())
选择语言
默认情况下,导入l18n时,使用当前的默认区域设置(通过 locale.getdefaultlocale())。如果不是你想要的或者你需要的话 改变它,很容易:
>>> l18n.set_language('en') >>> str(l18n.tz_cities['Pacific/Easter']) 'Easter Island' >>> l18n.set_language('fr') >>> str(l18n.tz_cities['Pacific/Easter']) 'Île de Pâques'
如果要禁用转换并使用原始默认字符串:
>>> l18n.set_language(None)
实用程序
l18n还公开了一些在某些情况下可能有用的函数:
l18n.utils.get_country_tzs(country_code)
returns a list of locations for the given country code, sorted in alphabetical order in the currently selected language
l18n.utils.get_country_code_from_tz(timezone)
returns the country code from a given (untranslated) timezone
版本化
l18n的主版本号与pytz的版本号匹配。l18n 2014.10.x将与^{tt25}2014.10完全兼容,无论 实际上,主要目的是保持l18n的翻译文件的一致性。 使用pytz的时区名称。
在l18n2016.6之前,pytz版本固定在l18n 版本。现在,l18nYEAR.MONTH现在可以用于任何后续的pytz 版本。但是,请注意,如果两个版本 彼此太不一样了。在这种情况下,打开issue请求 要发布的l18n的新版本。
要添加语言吗?
好主意!!看看CONTRIBUTE.rst。
路线图
- 添加支持的语言
- 添加货币和其他内容