Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/6_Maths_matchingAnglesAndShapes/js/mo2js.py
blob: b0932029ba5f253f8636ad18aa314ba8660dbe89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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);