Advertisement
Infra_HDC

mo2json ???

Dec 30th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. #!/usr/bin/python
  2. # from http://archive.plugins.jquery.com/project/gettext
  3. # to extract strings to a .po file, you can use standard gettext utilities like xgettext and msgfmt; to generate the JSON, one could use the following Python snippet, assuming a domain.mo file exists under path/lang/LC_MESSAGES:
  4. import simplejson as enc
  5. import gettext
  6. def gettext_json(domain, path, lang = [], indent = False):
  7.     try:
  8.         tr = gettext.translation(domain, path, lang)
  9.         # for unknown reasons, instead of having plural entries like
  10.         # key: [sg, pl1...]
  11.         # tr._catalog has (key, n): pln,
  12.         keys = tr._catalog.keys()
  13.         keys.sort()
  14.         ret = {}
  15.         for k in keys:
  16.             v = tr._catalog[k]
  17.             if type(k) is tuple:
  18.                 if k[0] not in ret:
  19.                     ret[k[0]] = []
  20.                 ret[k[0]].append(v)
  21.             else:
  22.                 ret[k] = v
  23.         return enc.dumps(ret, ensure_ascii = False, indent = indent)
  24.     except IOError:
  25.         return None
  26.  
  27. # call our convert function
  28. print gettext_json('default', '/path/to/opac/locale/', 'en_US.UTF8')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement