| 26 | |
| 27 | NullSource = """ |
| 28 | /* gettext identity library */ |
| 29 | |
| 30 | function gettext(msgid) { |
| 31 | return msgid; |
| 32 | } |
| 33 | |
| 34 | function ngettext(singular, plural, count) { |
| 35 | if (count == 1) { |
| 36 | return singular; |
| 37 | } else { |
| 38 | return plural; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | function gettext_noop(msgid) { |
| 43 | return msgid; |
| 44 | } |
| 45 | """ |
| 46 | |
| 47 | LibHead = """ |
| 48 | /* gettext library */ |
| 49 | |
| 50 | var catalog = new Array(); |
| 51 | """ |
| 52 | |
| 53 | LibFoot = """ |
| 54 | |
| 55 | function gettext(msgid) { |
| 56 | var value = catalog[msgid]; |
| 57 | if (typeof(value) == 'undefined') { |
| 58 | return msgid; |
| 59 | } else { |
| 60 | if (typeof(value) == 'string') { |
| 61 | return value; |
| 62 | } else { |
| 63 | return value[0]; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | function ngettext(singular, plural, count) { |
| 69 | value = catalog[singular]; |
| 70 | if (typeof(value) == 'undefined') { |
| 71 | if (count == 1) { |
| 72 | return singular; |
| 73 | } else { |
| 74 | return plural; |
| 75 | } |
| 76 | } else { |
| 77 | return value[pluralidx(count)]; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | function gettext_noop(msgid) { |
| 82 | return msgid; |
| 83 | } |
| 84 | """ |
| 85 | |
| 86 | SimplePlural = """ |
| 87 | function pluralidx(count) { |
| 88 | if (count == 1) { |
| 89 | return 0; |
| 90 | } else { |
| 91 | return 1; |
| 92 | } |
| 93 | } |
| 94 | """ |
| 95 | |
| 96 | InterPolate = r""" |
| 97 | function interpolate(fmt, obj, named) { |
| 98 | if (named) { |
| 99 | return fmt.replace(/%\(\w+\)s/, function(match){return String(obj[match.slice(2,-2)])}); |
| 100 | } else { |
| 101 | return fmt.replace(/%s/, function(match){return String(obj.shift())}); |
| 102 | } |
| 103 | } |
| 104 | """ |
| 105 | |
| 106 | def javascript_catalog(request): |
| 107 | """ |
| 108 | Returns the selected language catalog as a javascript library. |
| 109 | """ |
| 110 | if request.GET: |
| 111 | if request.GET.has_key('language'): |
| 112 | activate(request.GET['language']) |
| 113 | t = catalog() |
| 114 | if isinstance(t, DjangoTranslation): |
| 115 | src = [LibHead] |
| 116 | plural = None |
| 117 | for l in t._catalog[''].split('\n'): |
| 118 | if l.startswith('Plural-Forms:'): |
| 119 | plural = l.split(':',1)[1].strip() |
| 120 | if plural is not None: |
| 121 | # this should actually be a compiled function of a typical plural-form: |
| 122 | # Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2; |
| 123 | plural = [el.strip() for el in plural.split(';') if el.strip().startswith('plural=')][0].split('=',1)[1] |
| 124 | src.append('function pluralidx(n) {\n return %s;\n}\n' % plural) |
| 125 | else: |
| 126 | src.append(SimplePlural) |
| 127 | csrc = [] |
| 128 | pdict = {} |
| 129 | for k, v in t._catalog.items(): |
| 130 | if k == '': |
| 131 | continue |
| 132 | if type(k) in (str, unicode): |
| 133 | csrc.append("catalog['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(v))) |
| 134 | elif type(k) == tuple: |
| 135 | if not pdict.has_key(k[0]): |
| 136 | pdict[k[0]] = k[1] |
| 137 | else: |
| 138 | pdict[k[0]] = max(k[1], pdict[k[0]]) |
| 139 | csrc.append("catalog['%s'][%d] = '%s';\n" % (javascript_quote(k[0]), k[1], javascript_quote(v))) |
| 140 | else: |
| 141 | raise TypeError, k |
| 142 | csrc.sort() |
| 143 | for k,v in pdict.items(): |
| 144 | src.append("catalog['%s'] = [%s];\n" % (javascript_quote(k), ','.join(["''"]*(v+1)))) |
| 145 | src.extend(csrc) |
| 146 | src.append(LibFoot) |
| 147 | src.append(InterPolate) |
| 148 | src = ''.join(src) |
| 149 | else: |
| 150 | src = [NullSource, InterPolate] |
| 151 | return httpwrappers.HttpResponse(src, 'text/javascript') |
| 152 | |