Ticket #18231: js-i18n-patch.diff

File js-i18n-patch.diff, 4.7 KB (added by Matthew Tretter <matthew@…>, 12 years ago)
  • django/views/i18n.py

    diff --git c/django/views/i18n.py w/django/views/i18n.py
    index 140dc54..66ca940 100644
    c w def get_formats():  
    5353    src = []
    5454    for k, v in result.items():
    5555        if isinstance(v, (basestring, int)):
    56             src.append("formats['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(smart_unicode(v))))
     56            src.append("  formats['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(smart_unicode(v))))
    5757        elif isinstance(v, (tuple, list)):
    5858            v = [javascript_quote(smart_unicode(value)) for value in v]
    59             src.append("formats['%s'] = ['%s'];\n" % (javascript_quote(k), "', '".join(v)))
     59            src.append("  formats['%s'] = ['%s'];\n" % (javascript_quote(k), "', '".join(v)))
    6060    return ''.join(src)
    6161
    6262NullSource = """
    function npgettext(context, singular, plural, count) { return (count == 1) ? sin  
    7272LibHead = """
    7373/* gettext library */
    7474
    75 var catalog = new Array();
     75(function (globals) {
     76  var catalog = {};
    7677"""
    7778
    7879LibFoot = """
    7980
    80 function gettext(msgid) {
    81   var value = catalog[msgid];
    82   if (typeof(value) == 'undefined') {
    83     return msgid;
    84   } else {
    85     return (typeof(value) == 'string') ? value : value[0];
     81  globals.gettext = function (msgid) {
     82    var value = catalog[msgid];
     83    if (typeof(value) == 'undefined') {
     84      return msgid;
     85    } else {
     86      return (typeof(value) == 'string') ? value : value[0];
     87    }
    8688  }
    87 }
    8889
    89 function ngettext(singular, plural, count) {
    90   value = catalog[singular];
    91   if (typeof(value) == 'undefined') {
    92     return (count == 1) ? singular : plural;
    93   } else {
    94     return value[pluralidx(count)];
     90  globals.ngettext = function (singular, plural, count) {
     91    value = catalog[singular];
     92    if (typeof(value) == 'undefined') {
     93      return (count == 1) ? singular : plural;
     94    } else {
     95      return value[pluralidx(count)];
     96    }
    9597  }
    96 }
    9798
    98 function gettext_noop(msgid) { return msgid; }
     99  globals.gettext_noop = function (msgid) { return msgid; }
    99100
    100 function pgettext(context, msgid) {
    101   var value = gettext(context + '\x04' + msgid);
    102   if (value.indexOf('\x04') != -1) {
    103     value = msgid;
     101  globals.pgettext = function (context, msgid) {
     102    var value = gettext(context + '\x04' + msgid);
     103    if (value.indexOf('\x04') != -1) {
     104      value = msgid;
     105    }
     106    return value;
    104107  }
    105   return value;
    106 }
    107108
    108 function npgettext(context, singular, plural, count) {
    109   var value = ngettext(context + '\x04' + singular, context + '\x04' + plural, count);
    110   if (value.indexOf('\x04') != -1) {
    111     value = ngettext(singular, plural, count);
     109  globals.npgettext = function (context, singular, plural, count) {
     110    var value = ngettext(context + '\x04' + singular, context + '\x04' + plural, count);
     111    if (value.indexOf('\x04') != -1) {
     112      value = ngettext(singular, plural, count);
     113    }
     114    return value;
    112115  }
    113   return value;
    114 }
     116}(this));
    115117"""
    116118
    117119LibFormatHead = """
    118120/* formatting library */
    119121
    120 var formats = new Array();
     122(function (globals) {
     123  var formats = {};
    121124
    122125"""
    123126
    124127LibFormatFoot = """
    125 function get_format(format_type) {
     128  globals.get_format = function (format_type) {
    126129    var value = formats[format_type];
    127130    if (typeof(value) == 'undefined') {
    128131      return msgid;
    129132    } else {
    130133      return value;
    131134    }
    132 }
     135  }
     136}(this));
    133137"""
    134138
    135139SimplePlural = """
    136 function pluralidx(count) { return (count == 1) ? 0 : 1; }
     140  globals.pluralidx = function (count) { return (count == 1) ? 0 : 1; }
    137141"""
    138142
    139143InterPolate = r"""
    function interpolate(fmt, obj, named) {  
    147151"""
    148152
    149153PluralIdx = r"""
    150 function pluralidx(n) {
    151   var v=%s;
    152   if (typeof(v) == 'boolean') {
    153     return v ? 1 : 0;
    154   } else {
    155     return v;
     154  globals.pluralidx = function (n) {
     155    var v=%s;
     156    if (typeof(v) == 'boolean') {
     157      return v ? 1 : 0;
     158    } else {
     159      return v;
     160    }
    156161  }
    157 }
    158162"""
    159163
    160164def null_javascript_catalog(request, domain=None, packages=None):
    def javascript_catalog(request, domain='djangojs', packages=None):  
    259263        if k == '':
    260264            continue
    261265        if isinstance(k, basestring):
    262             csrc.append("catalog['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(v)))
     266            csrc.append("  catalog['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(v)))
    263267        elif isinstance(k, tuple):
    264268            if k[0] not in pdict:
    265269                pdict[k[0]] = k[1]
    266270            else:
    267271                pdict[k[0]] = max(k[1], pdict[k[0]])
    268             csrc.append("catalog['%s'][%d] = '%s';\n" % (javascript_quote(k[0]), k[1], javascript_quote(v)))
     272            csrc.append("  catalog['%s'][%d] = '%s';\n" % (javascript_quote(k[0]), k[1], javascript_quote(v)))
    269273        else:
    270274            raise TypeError(k)
    271275    csrc.sort()
Back to Top