日历:特定语言环境中的日/月名称 [英] Calendar: day/month names in specific locale

查看:98
本文介绍了日历:特定语言环境中的日/月名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用标准库中Python的 calendar 模块。基本上,我需要一个月中所有天的列表,例如:

I am playing with Python's calendar module that's in the standard library. Basically I need a list of all days of a month, like so:

>>> import calendar
>>> calobject = calendar.monthcalendar(2012, 10)
>>> print calobject
[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31, 0, 0, 0, 0]]

现在我还需要的是特定语言环境中月份和日期的名称。我没有找到从 calobject 本身获取这些方法的方法-但我能够像这样获取它们:

Now what I also need are the names of the months and days in a specific locale. I didn't find a way to get these from the calobject itself - but I was able to get them like so:

>>> import calendar
>>> calobject = calendar.LocaleTextCalendar(calendar.MONDAY, 'de_DE')
>>> calobject.formatmonth(2012, 10)
'    Oktober 2012\nMo Di Mi Do Fr Sa So\n 1  2  3  4  5  6  7\n 8  9 10 11 12 13 14\n15 16 17 18 19 20 21\n22 23 24 25 26 27 28\n29 30 31\n'

因此,十月是十月的 de_DE 名称。精细。信息必须在那里。我想知道是否可以在普通 calendar 对象而不是 calendar.LocaleTextCalendar 对象上以某种方式访问​​该月的名称。第一个示例(带有列表)确实是我所需要的,我不喜欢创建两个日历对象来获取本地化名称的想法。

So Oktober is the de_DE name for october. Fine. The information must be there. I'm wondering if I can access that month name somehow on a plain calendar object instead of a calendar.LocaleTextCalendar object. The first example (with the list) is really what I need and I don't like the idea to create two calendar objects to get localized names.

每个人都聪明

推荐答案

这是来自 calendar 模块的源代码:

This is from the source code of the calendar module:

def formatmonthname(self, theyear, themonth, width, withyear=True):
    with TimeEncoding(self.locale) as encoding:
        s = month_name[themonth]
        if encoding is not None:
            s = s.decode(encoding)
        if withyear:
            s = "%s %r" % (s, theyear)
        return s.center(width)

<可以从 calendar 模块导入code> TimeEncoding 和 month_name 。这提供了以下方法:

TimeEncoding and month_name can be imported from the calendar module. This gives the following method:

from calendar import TimeEncoding, month_name

def get_month_name(month_no, locale):
    with TimeEncoding(locale) as encoding:
        s = month_name[month_no]
        if encoding is not None:
            s = s.decode(encoding)
        return s

print get_month_name(3, "nb_NO.UTF-8")

对我来说,不需要解码步骤,只需在 TimeEncoding 上下文中打印 month_name [3] 即可打印火星,

For me the decode step is not needed, simply printing month_name[3] in the TimeEncoding context prints "mars", which is norwegian for "march".

在工作日中,有一种类似的方法使用 day_name day_abbr 格:

For weekdays there's a similar method using the day_name and day_abbr dicts:

from calendar import TimeEncoding, day_name, day_abbr

def get_day_name(day_no, locale, short=False):
    with TimeEncoding(locale) as encoding:
        if short:
            s = day_abbr[day_no]
        else:
            s = day_name[day_no]
        if encoding is not None:
            s = s.decode(encoding)
        return s

这篇关于日历:特定语言环境中的日/月名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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