Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / babeljs.py @ db6fbc92

History | View | Annotate | Download (4.26 KB)

1
# -*- coding: utf-8 -*-
2
"""
3
Provides a class derived from Babel's compile_catalog
4
which automatically creates JavaScript files compatible
5
with Babel's JavaScript frontend.
6

7
This code was taken from:
8
http://svn.python.org/projects/doctools/trunk/setup.py
9

10
And generates files for use with:
11
http://babel.edgewall.org/browser/trunk/contrib/babel.js
12
"""
13
try:
14
    from babel.messages.pofile import read_po
15
    from babel.messages.frontend import compile_catalog
16
    try:
17
        from simplejson import dump
18
    except ImportError:
19
        from json import dump
20
except ImportError:
21
    pass
22
else:
23
    import os
24
    from distutils import log
25

    
26
    class compile_catalog_plusjs(compile_catalog):
27
        """
28
        An extended command that writes all message strings that occur in
29
        JavaScript files to a JavaScript file along with the .mo file.
30

31
        Unfortunately, babel's setup command isn't built very extensible, so
32
        most of the run() code is duplicated here.
33
        """
34

    
35
        def run(self):
36
            compile_catalog.run(self)
37

    
38
            po_files = []
39
            js_files = []
40

    
41
            if not self.input_file:
42
                if self.locale:
43
                    po_files.append((self.locale,
44
                                     os.path.join(self.directory, self.locale,
45
                                                  'LC_MESSAGES',
46
                                                  self.domain + '.po')))
47
                    js_files.append(os.path.join(self.directory, self.locale,
48
                                                 'LC_MESSAGES',
49
                                                 self.domain + '.js'))
50
                else:
51
                    for locale in os.listdir(self.directory):
52
                        po_file = os.path.join(self.directory, locale,
53
                                               'LC_MESSAGES', self.domain + '.po')
54
                        if os.path.exists(po_file):
55
                            po_files.append((locale, po_file))
56
                            js_files.append(os.path.join(self.directory, locale,
57
                                                         'LC_MESSAGES',
58
                                                         self.domain + '.js'))
59
            else:
60
                po_files.append((self.locale, self.input_file))
61
                if self.output_file:
62
                    js_files.append(self.output_file)
63
                else:
64
                    js_files.append(os.path.join(self.directory, self.locale,
65
                                                 'LC_MESSAGES',
66
                                                 self.domain + '.js'))
67

    
68
            for js_file, (locale, po_file) in zip(js_files, po_files):
69
                infile = open(po_file, 'r')
70
                try:
71
                    catalog = read_po(infile, locale)
72
                finally:
73
                    infile.close()
74

    
75
                if catalog.fuzzy and not self.use_fuzzy:
76
                    continue
77

    
78
                log.info('writing JavaScript strings in catalog %r to %r',
79
                         po_file, js_file)
80

    
81
                jscatalog = {}
82
                for message in catalog:
83
                    # Si le message n'a pas encore été traduit,
84
                    # on ne l'ajoute pas. Le message d'origine
85
                    # (non traduit) sera renvoyé.
86
                    if not message.string:
87
                        continue
88

    
89
                    # On n'ajoute le message au fichier de traduction JS
90
                    # auto-généré que si le message est utilisé dans du
91
                    # code JavaScript.
92
                    if any(x[0].endswith('.js') for x in message.locations):
93
                        msgid = message.id
94
                        if isinstance(msgid, (list, tuple)):
95
                            msgid = msgid[0]
96
                        jscatalog[msgid] = message.string
97

    
98
                outfile = open(js_file, 'wb')
99
                try:
100
                    outfile.write('babel.Translations.load(');
101
                    dump(dict(
102
                        messages=jscatalog,
103
                        plural_expr=catalog.plural_expr,
104
                        locale=str(catalog.locale)
105
                        ), outfile)
106
                    outfile.write(').install();')
107
                finally:
108
                    outfile.close()
109