diff options
author | Peter <Peter.Gijsels@gmail.com> | 2010-03-01 21:09:21 (GMT) |
---|---|---|
committer | Peter <Peter.Gijsels@gmail.com> | 2010-03-01 21:09:21 (GMT) |
commit | 54d2b035550efbe0dab12a0df140982b252d76ef (patch) | |
tree | f1ee4b46208ffaa69ce3fcafea3e0a6ce9d0e9f5 | |
parent | c7516409af8b0b7f69d1036cf6fd5f0dce480b9b (diff) |
mo2js.py
-rwxr-xr-x | examples/6_Maths_matchingAnglesAndShapes/js/mo2js.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/examples/6_Maths_matchingAnglesAndShapes/js/mo2js.py b/examples/6_Maths_matchingAnglesAndShapes/js/mo2js.py new file mode 100755 index 0000000..b093202 --- /dev/null +++ b/examples/6_Maths_matchingAnglesAndShapes/js/mo2js.py @@ -0,0 +1,74 @@ +#! /usr/bin/env python + +import codecs +import gettext +import json + +# To deal with keys that occur in more than one place and have a +# different translation, gettext uses contexts and the +# pgettext(context, msg) call. This would fit well with what we +# are trying to do. +# gettext uses a hack: the context and msg are concatenated into +# one string with an unprintable character between them, '\004'. +# A lot of gettext tools can then be oblivious to contexts. +# +# The problem however is that xgettext (to extract the strings) +# doesn't support pgettext when used with --language=Python. +# +# Two possibilities: +# 1. we roll our own xgettext for javascript (starting from e.g. the +# jslint parser). The code below should be adapted to recognise +# the '\004' character and extract the context. +# 2. we abandon using contexts and combine the context and msg into +# one string. e.g. '$.ui.kFooter.Play Again' + +# Other remarks: +# 1. If there is no context specified, we should not use 'default' +# as context, because that context might already be used. +# We should restructure the $.i18n.<lang> dict, e.g.: +# $.i18n.<lang> = +# { strings = { 'key': 'val', ...}, +# contextualized_strings = { context1 = { 'key' : 'val', ...}, +# context2 = { 'key' : 'val', ...}}}; +# 2. We should rename messages.<lang>.json to messages.<lang>.js +# 3. In lesson.html we should refer to messages.js, which is a link to +# one of the messages.<lang>.js + +def gettext_json(fp, indent = False): + import gettext + import json + try: + tr = gettext.GNUTranslations(fp) + keys = tr._catalog.keys() + keys.sort() + ret = {} + for k in keys: + v = tr._catalog[k] + if type(k) is tuple: + # plural: k = (key, n) + if k[0] not in ret: + ret[k[0]] = [] + ret[k[0]].append(v) + else: + ret[k] = v + return json.dumps(ret, ensure_ascii = False, indent = indent) + except IOError: + return None + +def usage(argv0): + print 'Usage:' + print '\t' + argv0 + ' language mo-file js-file' + +if __name__ == '__main__': + import sys + if len(sys.argv) != 4: + usage(sys.argv[0]) + sys.exit(1) + [lang, mo, js] = sys.argv[1:] + f = codecs.open(js, encoding='utf-8', mode='w+') + lang = sys.argv[1] + f.write('$.i18n.%s = {};\n' % lang); + f.write('$.i18n.%s.strings =\n' % lang); + f.write(gettext_json(open(mo, 'r'), True)) + f.write(';\n'); + f.write('$.i18n.setLocale(\'%s\');\n' % lang); |