1. Luke Plant
  2. django

Commits

garc...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed 8674708

[soc2009/i18n] formats made available for javascript, and used to change the first day of week in admin calendar

  • Participants
  • Parent commits 981a69b
  • Branches soc2009/i18n-improvements

Comments (0)

Files changed (3)

File django/contrib/admin/media/js/calendar.js

View file
 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));
     },
         // 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

File django/utils/formats.py

View file
     """
     return getattr(settings, format_type)
 
+def formats_module():
+    """
+    Returns the formats module for the current locale. It can be the
+    custom one from the user, or Django's default.
+    """
+    if settings.FORMAT_MODULE_PATH:
+        try:
+            return import_module('.formats', '%s.%s' % (settings.FORMAT_MODULE_PATH, 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'
     """
-    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()))
-        except ImportError:
-            pass
-
-    if not module:
-        try:
-            module = import_module('.formats', 'django.conf.locale.%s' % get_language())
-        except ImportError:
-            pass
-
+    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

File django/views/i18n.py

View file
 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
 
                 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 */
 
         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')
+