summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Garcia <garcia.marc@gmail.com>2009-07-09 09:22:14 +0000
committerMarc Garcia <garcia.marc@gmail.com>2009-07-09 09:22:14 +0000
commit637aa76d072682a94c1a02965d2f92dad20bb46a (patch)
treecdd48738e0a035aacd79dbaf056a2eff038f3df1
parent07ed7fea103ee7f507fb2e49cdd6d983fe54c7b0 (diff)
[soc2009/i18n] formats made available for javascript, and used to change the first day of week in admin calendar
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/i18n-improvements@11207 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/admin/media/js/calendar.js5
-rw-r--r--django/utils/formats.py32
-rw-r--r--django/views/i18n.py17
3 files changed, 37 insertions, 17 deletions
diff --git a/django/contrib/admin/media/js/calendar.js b/django/contrib/admin/media/js/calendar.js
index 90351763a7..3f57145041 100644
--- a/django/contrib/admin/media/js/calendar.js
+++ b/django/contrib/admin/media/js/calendar.js
@@ -25,6 +25,7 @@ function quickElement() {
var CalendarNamespace = {
monthsOfYear: gettext('January February March April May June July August September October November December').split(' '),
daysOfWeek: gettext('S M T W T F S').split(' '),
+ firstDayOfWeek: parseInt(gettext('FIRST_DAY_OF_WEEK')),
isLeapYear: function(year) {
return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
},
@@ -56,10 +57,10 @@ var CalendarNamespace = {
// Draw days-of-week header
var tableRow = quickElement('tr', tableBody);
for (var i = 0; i < 7; i++) {
- quickElement('th', tableRow, CalendarNamespace.daysOfWeek[i]);
+ quickElement('th', tableRow, CalendarNamespace.daysOfWeek[(i + CalendarNamespace.firstDayOfWeek) % 7]);
}
- var startingPos = new Date(year, month-1, 1).getDay();
+ var startingPos = new Date(year, month-1, 1 - CalendarNamespace.firstDayOfWeek).getDay();
var days = CalendarNamespace.getDaysInMonth(month, year);
// Draw blanks before first of month
diff --git a/django/utils/formats.py b/django/utils/formats.py
index 2b8e2704c0..c126d3e246 100644
--- a/django/utils/formats.py
+++ b/django/utils/formats.py
@@ -15,32 +15,36 @@ def getformat_null(format_type):
"""
return getattr(settings, format_type)
-def getformat_real(format_type):
+def formats_module():
"""
- For a specific format type, returns the format for the
- current language (locale) defaulting to the format on settings.
- format_type is the name of the format, for example 'DATE_FORMAT'
+ Returns the formats module for the current locale. It can be the
+ custom one from the user, or Django's default.
"""
- import_formats = lambda s: import_module('.formats', 'django.conf.locale.%s' % s)
- module = format = None
if settings.FORMAT_MODULE_PATH:
try:
- module = import_module('.formats', '%s.%s' % (settings.FORMAT_MODULE_PATH, get_language()))
+ return import_module('.formats', '%s.%s' % (settings.FORMAT_MODULE_PATH, get_language()))
except ImportError:
pass
- if not module:
- try:
- module = import_module('.formats', 'django.conf.locale.%s' % get_language())
- except ImportError:
- pass
+ try:
+ return import_module('.formats', 'django.conf.locale.%s' % get_language())
+ except ImportError:
+ return None
+
+def getformat_real(format_type):
+ """
+ For a specific format type, returns the format for the
+ current language (locale) defaulting to the format on settings.
+ format_type is the name of the format, for example 'DATE_FORMAT'
+ """
+ module = formats_module()
if module:
try:
- format = getattr(module, format_type)
+ return getattr(module, format_type)
except AttributeError:
pass
- return format or getformat_null(format_type)
+ return getformat_null(format_type)
# getformat will just return the value on setings if
# we don't use i18n in our project
diff --git a/django/views/i18n.py b/django/views/i18n.py
index 0280698aae..eaa4723f94 100644
--- a/django/views/i18n.py
+++ b/django/views/i18n.py
@@ -3,6 +3,7 @@ from django.conf import settings
from django.utils import importlib
from django.utils.translation import check_for_language, activate, to_locale, get_language
from django.utils.text import javascript_quote
+from django.utils.formats import formats_module
import os
import gettext as gettext_module
@@ -32,6 +33,17 @@ def set_language(request):
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
return response
+def get_formats():
+ """
+ Returns an iterator over all formats in formats file
+ """
+ module = formats_module()
+ result = {}
+ for attr in dir(module):
+ if attr[:2] != '__':
+ result[attr] = getattr(module, attr)
+ return result
+
NullSource = """
/* gettext identity library */
@@ -185,10 +197,13 @@ def javascript_catalog(request, domain='djangojs', packages=None):
else:
raise TypeError, k
csrc.sort()
- for k,v in pdict.items():
+ for k, v in pdict.items():
src.append("catalog['%s'] = [%s];\n" % (javascript_quote(k), ','.join(["''"]*(v+1))))
+ for k, v in get_formats().items():
+ src.append("catalog['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(unicode(v))))
src.extend(csrc)
src.append(LibFoot)
src.append(InterPolate)
src = ''.join(src)
return http.HttpResponse(src, 'text/javascript')
+