更新: 在研究了一下这个问题后,(解决了ASCII错误,但现在返回了ValueError)我发现这是一个记录错误(issue #3067),因此在Python 2.7.3
中修复bug之前无法解决我正在尝试在django中本地化使用python HTMLCalendar制作的日历,但没有结果。日历是用code from Elving Uggedal制作的,但似乎无法将其本地化。
以下是代码:
models.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', ' ')
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,但是使用它作为基类,它会在日历模块中返回一个ASCII解码错误(这些文件声明了utf-8编码)。
我还尝试覆盖语言环境数组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'), ]
我似乎无法找到本地化此日历的方法。有什么想法吗?
答案 0 :(得分:6)
好笑,我昨天刚刚做了这件事,偶然发现了你的问题。
这是我如何克服障碍,抱歉有点复杂:
设置:
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标题。继续......
视图:
# -*- 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
希望有所帮助。
答案 1 :(得分:0)
这可能完全无关紧要,但如果没有其他想法,请尝试添加
# -*- coding: UTF-8 -*-
作为模块文件的第一行。