Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12492 - Refactored JavaScript format localization by separatin…

…g it from the translation backend. Thanks, Ramiro Morales.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12081 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f6c519e2b919cddc438f6ff52f2b4d7ef8248042 1 parent abcf997
Jannis Leidel authored January 04, 2010
15  django/contrib/admin/media/js/admin/DateTimeShortcuts.js
@@ -44,7 +44,7 @@ var DateTimeShortcuts = {
44 44
         var shortcuts_span = document.createElement('span');
45 45
         inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
46 46
         var now_link = document.createElement('a');
47  
-        now_link.setAttribute('href', "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + gettext('TIME_INPUT_FORMATS') + "'));");
  47
+        now_link.setAttribute('href', "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + get_format('TIME_INPUT_FORMATS')[0] + "'));");
48 48
         now_link.appendChild(document.createTextNode(gettext('Now')));
49 49
         var clock_link = document.createElement('a');
50 50
         clock_link.setAttribute('href', 'javascript:DateTimeShortcuts.openClock(' + num + ');');
@@ -80,10 +80,11 @@ var DateTimeShortcuts = {
80 80
         quickElement('h2', clock_box, gettext('Choose a time'));
81 81
         time_list = quickElement('ul', clock_box, '');
82 82
         time_list.className = 'timelist';
83  
-        quickElement("a", quickElement("li", time_list, ""), gettext("Now"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + gettext('TIME_INPUT_FORMATS') + "'));");
84  
-        quickElement("a", quickElement("li", time_list, ""), gettext("Midnight"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date(1970,1,1,0,0,0,0).strftime('" + gettext('TIME_INPUT_FORMATS') + "'));");
85  
-        quickElement("a", quickElement("li", time_list, ""), gettext("6 a.m."), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date(1970,1,1,6,0,0,0).strftime('" + gettext('TIME_INPUT_FORMATS') + "'));");
86  
-        quickElement("a", quickElement("li", time_list, ""), gettext("Noon"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date(1970,1,1,12,0,0,0).strftime('" + gettext('TIME_INPUT_FORMATS') + "'));");
  83
+        time_format = get_format('TIME_INPUT_FORMATS')[0];
  84
+        quickElement("a", quickElement("li", time_list, ""), gettext("Now"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + time_format + "'));");
  85
+        quickElement("a", quickElement("li", time_list, ""), gettext("Midnight"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date(1970,1,1,0,0,0,0).strftime('" + time_format + "'));");
  86
+        quickElement("a", quickElement("li", time_list, ""), gettext("6 a.m."), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date(1970,1,1,6,0,0,0).strftime('" + time_format + "'));");
  87
+        quickElement("a", quickElement("li", time_list, ""), gettext("Noon"), "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date(1970,1,1,12,0,0,0).strftime('" + time_format + "'));");
87 88
 
88 89
         cancel_p = quickElement('p', clock_box, '');
89 90
         cancel_p.className = 'calendar-cancel';
@@ -236,7 +237,7 @@ var DateTimeShortcuts = {
236 237
         DateTimeShortcuts.calendars[num].drawNextMonth();
237 238
     },
238 239
     handleCalendarCallback: function(num) {
239  
-        format = gettext('DATE_INPUT_FORMATS');
  240
+        format = get_format('DATE_INPUT_FORMATS')[0];
240 241
         // the format needs to be escaped a little
241 242
         format = format.replace('\\', '\\\\');
242 243
         format = format.replace('\r', '\\r');
@@ -248,7 +249,7 @@ var DateTimeShortcuts = {
248 249
     handleCalendarQuickLink: function(num, offset) {
249 250
        var d = new Date();
250 251
        d.setDate(d.getDate() + offset)
251  
-       DateTimeShortcuts.calendarInputs[num].value = d.strftime(gettext('DATE_INPUT_FORMATS'));
  252
+       DateTimeShortcuts.calendarInputs[num].value = d.strftime(get_format('DATE_INPUT_FORMATS')[0]);
252 253
        DateTimeShortcuts.dismissCalendar(num);
253 254
     },
254 255
     cancelEventPropagation: function(e) {
2  django/contrib/admin/media/js/calendar.js
@@ -25,7 +25,7 @@ function quickElement() {
25 25
 var CalendarNamespace = {
26 26
     monthsOfYear: gettext('January February March April May June July August September October November December').split(' '),
27 27
     daysOfWeek: gettext('S M T W T F S').split(' '),
28  
-    firstDayOfWeek: parseInt(gettext('FIRST_DAY_OF_WEEK')),
  28
+    firstDayOfWeek: parseInt(get_format('FIRST_DAY_OF_WEEK')),
29 29
     isLeapYear: function(year) {
30 30
         return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
31 31
     },
40  django/views/i18n.py
@@ -53,7 +53,14 @@ def get_formats():
53 53
                 result[attr] = getattr(module, attr)
54 54
             except AttributeError:
55 55
                 pass
56  
-    return result
  56
+    src = []
  57
+    for k, v in result.items():
  58
+        if isinstance(v, (basestring, int)):
  59
+            src.append("formats['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(smart_unicode(v))))
  60
+        elif isinstance(v, (tuple, list)):
  61
+            v = [javascript_quote(smart_unicode(value)) for value in v]
  62
+            src.append("formats['%s'] = ['%s'];\n" % (javascript_quote(k), "', '".join(v)))
  63
+    return ''.join(src)
57 64
 
58 65
 NullSource = """
59 66
 /* gettext identity library */
@@ -90,6 +97,25 @@ def get_formats():
90 97
 }
91 98
 
92 99
 function gettext_noop(msgid) { return msgid; }
  100
+
  101
+"""
  102
+
  103
+LibFormatHead = """
  104
+/* formatting library */
  105
+
  106
+var formats = new Array();
  107
+
  108
+"""
  109
+
  110
+LibFormatFoot = """
  111
+function get_format(format_type) {
  112
+    var value = formats[format_type];
  113
+    if (typeof(value) == 'undefined') {
  114
+      return msgid;
  115
+    } else {
  116
+      return value;
  117
+    }
  118
+}
93 119
 """
94 120
 
95 121
 SimplePlural = """
@@ -122,7 +148,8 @@ def null_javascript_catalog(request, domain=None, packages=None):
122 148
     Returns "identity" versions of the JavaScript i18n functions -- i.e.,
123 149
     versions that don't actually do anything.
124 150
     """
125  
-    return http.HttpResponse(NullSource + InterPolate, 'text/javascript')
  151
+    src = [NullSource, InterPolate, LibFormatHead, get_formats(), LibFormatFoot]
  152
+    return http.HttpResponse(''.join(src), 'text/javascript')
126 153
 
127 154
 def javascript_catalog(request, domain='djangojs', packages=None):
128 155
     """
@@ -210,15 +237,12 @@ def javascript_catalog(request, domain='djangojs', packages=None):
210 237
     csrc.sort()
211 238
     for k, v in pdict.items():
212 239
         src.append("catalog['%s'] = [%s];\n" % (javascript_quote(k), ','.join(["''"]*(v+1))))
213  
-    for k, v in get_formats().items():
214  
-        if isinstance(v, (basestring, int)):
215  
-            src.append("catalog['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(smart_unicode(v))))
216  
-        elif isinstance(v, (tuple, list)):
217  
-            v = [javascript_quote(smart_unicode(value)) for value in v]
218  
-            src.append("catalog['%s'] = ['%s'];\n" % (javascript_quote(k), "', '".join(v)))
219 240
     src.extend(csrc)
220 241
     src.append(LibFoot)
221 242
     src.append(InterPolate)
  243
+    src.append(LibFormatHead)
  244
+    src.append(get_formats())
  245
+    src.append(LibFormatFoot)
222 246
     src = ''.join(src)
223 247
     return http.HttpResponse(src, 'text/javascript')
224 248
 

0 notes on commit f6c519e

Please sign in to comment.
Something went wrong with that request. Please try again.