python - 无法在 Django 中本地化 Python HTMLCalendar

标签 python django localization calendar

更新: 在对该问题进行了一些研究之后,(解决了 ASCII 错误,但现在返回 ValueError)我发现这是一个记录在案的错误 (issue #3067),因此无法解决,直到该错误已在 Python 2.7.3 中修复


我正在尝试在 django 中本地化使用 python HTMLCalendar 制作的日历,但没有结果。日历是用code from Elving Uggedal制作的但似乎不可能将其本地化。

代码如下:

模型.py

import calendar
from datetime import date
from itertools import groupby

from django.utils.html import conditional_escape as esc

class EventCalendar(calendar.LocaleHTMLCalendar):

    """
    Event calendar is a basic calendar made with HTMLCalendar module.
    """

    def __init__(self, events, *args, **kwargs):
        self.events = self.group_by_day(events)
        super(EventCalendar, self).__init__(*args, **kwargs)

    def formatday(self, day, weekday):
        if day != 0:
            cssclass = self.cssclasses[weekday]
            if date.today() == date(self.year, self.month, day):
                cssclass += ' today'
            if day in self.events:
                cssclass += ' filled'
                body = ['<ul>']
                for event in self.events[day]:
                    body.append('<li>')
                    body.append('<a href="%s">' % event.get_absolute_url())
                    body.append(esc(event.title))
                    body.append('</a></li>')
                body.append('<ul>')
                return self.day_cell(cssclass, '%d %s' % (day, ''.join(body)))
            return self.day_cell(cssclass, day)
        return self.day_cell('noday', '&nbsp;')

    def formatmonth(self, year, month):
        self.year, self.month = year, month
        return super(EventCalendar, self).formatmonth(year, month)

    def group_by_day(self, events):
        field = lambda event: event.meeting_date.day
        return dict(
            [(day, list(items)) for day, items in groupby(events, field)]
        )

    def day_cell(self, cssclass, body):
        return '<td class="%s">%s</td>' % (cssclass, body)

views.py

from django.shortcuts import render_to_response, get_object_or_404
from django.utils.safestring import mark_safe
from django.template import RequestContext
from django.utils import translation

from e_cidadania.apps.spaces.models import Meeting, Space
from e_cidadania.apps.cal.models import EventCalendar

def calendar(request, space_name, year, month):

    # Avoid people writing wrong numbers or any program errors.
    if int(month) not in range(1, 13):
        return render_to_response('cal/error.html',
                                  context_instance=RequestContext(request))

    place = get_object_or_404(Space, url=space_name)
    next_month = int(month) + 1
    prev_month = int(month) - 1

    meetings = Meeting.objects.order_by('meeting_date') \
                              .filter(space = place,
                                      meeting_date__year = year,
                                      meeting_date__month = month)

    cur_lang = translation.get_language()
    print 'DEBUG:LANG: %s' % cur_lang
    cur_locale = translation.to_locale(cur_lang)+'.UTF-8' #default encoding with django
    print 'DEBUG:LOCALE: %s' % cur_locale
    cal = EventCalendar(meetings, settings.FIRST_WEEK_DAY, cur_locale).formatmonth(int(year), int(month))

    return render_to_response('cal/calendar.html',
                              {'calendar': mark_safe(cal),
                               'nextmonth': '%02d' % next_month,
                               'prevmonth': '%02d' % prev_month,
                               'get_place': place},
                               context_instance = RequestContext(request))

我试过使用 LocalizeHTMLCalendar,它应该返回本地化的 HTMLCalendar,但将它用作基类时会在日历模块(文件声明了 utf-8 编码)中返回 ASCII 解码错误。

我还尝试覆盖语言环境数组 day_name、day_abbr、month_name 和 month_abbr,结果日历呈现为空。

例子:

from django.utils.translation import ugettext_lazy as _

calendar.day_name = [_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday'), _('Sunday'), ]

我似乎找不到本地化此日历的方法。有什么想法吗?

最佳答案

有趣的是,我昨天才这样做,无意中偶然发现了你的问题。

以下是我如何克服障碍,抱歉有点复杂:

设置:

LANGUAGE_CODE           = 'en-us'
FIRST_DAY_OF_WEEK       = 0     # 0 is Sunday
# Convert to calendar module, where 0 is Monday :/
FIRST_DAY_OF_WEEK_CAL   = (FIRST_DAY_OF_WEEK - 1) % 7

# figure locale name
LOCAL_LANG              = LANGUAGE_CODE.split('-')[0]
LOCAL_COUNTRY           = LANGUAGE_CODE.split('-')[1].upper()
LOCALE_NAME             = LOCAL_LANG + '_' + LOCAL_COUNTRY + '.UTF8'

我想这就是上面的答案。你在使用 Ubuntu/Debian 吗?除非'.UTF8'在那里,否则我无法让它工作。我还需要下面的 utf8 python header 。继续......

查看:

# -*- coding: utf8 -*-
class kCalendar(calendar.LocaleHTMLCalendar):
    def __init__(self, *args):
        # some customization I did ...
        calendar.LocaleHTMLCalendar.__init__(self, *args)  
        # super didn't work, can't remember why...
# more customization here...

kcal = kCalendar(date, settings.FIRST_DAY_OF_WEEK_CAL, 
    settings.LOCALE_NAME)
calhtml = kcal.formatmonth(date.year, date.month)

另外,我需要安装一些语言环境“包”,因为我已经用 localepurge 搞砸了一切。这是 Ubuntu 的脚本:

add_lang_pack.sh:

#!/usr/bin/env bash

if [ "$#" -eq 0 ]; then
  echo -e "\nUsage: `basename $0` <locale, eg: en_US>\n"
  exit 1
fi

sudo /usr/share/locales/install-language-pack $1

if grep -q "$1" /etc/locale.nopurge
then
    # code if found
    echo -e "\n** not adding $1 to /etc/locale.nopurge, is already there.\n"
else
    sudo sh -c "echo $1 >> /etc/locale.nopurge"
    sudo sh -c "echo $1.UTF-8 >> /etc/locale.nopurge"
fi

sudo dpkg-reconfigure locales

希望对您有所帮助。

关于python - 无法在 Django 中本地化 Python HTMLCalendar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6496247/

相关文章:

Python - 查找数据框中包含单词的前 5 行

python - 忽略 Django 模型中的一个字段

django - 我应该将外键设置为 Django 用户还是配置文件模型?

android - 在运行时更改语言环境?

user-interface - 语言选择器应该以英语还是母语列出?

python - 详尽的 SQL 查询,count(*) 限制器不起作用

python - 如何将 PyFrameObject 转换为 PyObject

python - 如何使用 setuptools 创建包含多个模块的 python 包?

python - 如何使用某些表单字段值进行计算并在另一个 django 管理表单中自动填充该计算值?

javascript - HTML编码°度数符号额外空格