1
2
3
4
5 from gettext import NullTranslations
6 import re
7 from JSONService import loads
8 from HTTPRequest import HTTPRequest
9
10 try:
11 from gettext import c2py
12 except ImportError:
13 from __pyjamas__ import JS
15 f = None
16 JS('''eval("@{{f}}=function(n){return " + @{{plural}} + ";}")''')
17 return f
18
19
21 re_nplurals = re.compile('nplurals *= *(\d+)')
22 re_plural = re.compile('plural *= *([^;]+)')
23 base_url = None
24 domain = None
25
29
30 - def new_catalog(self, base_url=None, domain=None, lang=None):
31 self._catalog = {}
32 self.lang = lang
33 self.plural = lambda n: int(n != 1)
34
35 - def load(self, base_url=None, domain=None, lang=None, onCompletion=None, onError=None):
47
49 self.parse_json(text)
50 if self._onCompletion is not None:
51 self._onCompletion(text)
52
54 if self._onError is not None:
55 self._onError(text, code)
56 raise RuntimeError(code)
57
60
63
65 json = loads(text)
66 self._catalog.update(json)
67 for k, v in self._catalog[""].iteritems():
68 k = k.lower()
69 if k == 'plural-forms':
70 self.nplurals = int(self.re_nplurals.search(v).group(1))
71 self.plural = c2py(self.re_plural.search(v).group(1))
72
75
76 - def gettext(self, message):
77 tmsg = self._catalog.get(message)
78 if tmsg is None:
79 if self._fallback:
80 return self._fallback.gettext(message)
81 return message
82 return tmsg[0]
83
84 - def ngettext(self, msgid1, msgid2, n):
85 tmsg = self._catalog.get(msgid1)
86 if tmsg is None:
87 if self._fallback:
88 return self._fallback.ngettext(msgid1, msgid2, n)
89 if self.plural(n):
90 return msgid2
91 return msgid1
92 else:
93 return tmsg[self.plural(n)]
94
95 lgettext = gettext
96 lngettext = ngettext
97 ugettext = gettext
98 ungettext = ngettext
99