Project

General

Profile

Revision ef840436

IDef8404366b1064a00bf7ce8ed80ccb813f6b2743
Parent a51531ed
Child 5b63b889

Added by Francois POIROTTE almost 7 years ago

[#1721] Fusion traductions du thème et de l'appli

Change-Id: I3c63552637ce9f5041fd19288d82c0ad52de1dec
Refs: #1721

View differences:

MANIFEST.in
12 12
graft vigigraph/public
13 13
include *.ini
14 14
include jsl.cfg
15
include babeljs.py
16 15
include app_cfg.py
babeljs.py
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 isinstance(self.domain, list):
42
                domains = self.domain
43
            else:
44
                domains = [self.domain]
45

  
46
            if not self.input_file:
47
                if self.locale:
48
                    for domain in domains:
49
                        basename = os.path.join(self.directory, self.locale,
50
                                                'LC_MESSAGES', domain)
51
                        po_files.append( (self.locale, basename + '.po') )
52
                        js_files.append( basename + '.js')
53
                else:
54
                    for locale in os.listdir(self.directory):
55
                        for domain in domains:
56
                            basename = os.path.join(self.directory, locale,
57
                                                    'LC_MESSAGES', domain)
58
                            if os.path.exists(basename + '.po'):
59
                                po_files.append( (locale, basename + '.po') )
60
                                js_files.append(basename + '.js')
61
            else:
62
                po_files.append( (self.locale, self.input_file) )
63
                if self.output_file:
64
                    js_files.append(self.output_file)
65
                else:
66
                    for domain in domains:
67
                        js_files.append(os.path.join(
68
                            self.directory,
69
                            self.locale,
70
                            'LC_MESSAGES',
71
                            domain + '.js'
72
                         ))
73

  
74
            for js_file, (locale, po_file) in zip(js_files, po_files):
75
                infile = open(po_file, 'r')
76
                try:
77
                    catalog = read_po(infile, locale)
78
                finally:
79
                    infile.close()
80

  
81
                if catalog.fuzzy and not self.use_fuzzy:
82
                    continue
83

  
84
                log.info('writing JavaScript strings in catalog %r to %r',
85
                         po_file, js_file)
86

  
87
                jscatalog = {}
88
                for message in catalog:
89
                    # Si le message n'a pas encore été traduit,
90
                    # on ne l'ajoute pas. Le message d'origine
91
                    # (non traduit) sera renvoyé.
92
                    if not message.string:
93
                        continue
94

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

  
104
                outfile = open(js_file, 'wb')
105
                try:
106
                    outfile.write('babel.Translations.load(');
107
                    dump(dict(
108
                        messages=jscatalog,
109
                        plural_expr=catalog.plural_expr,
110
                        locale=str(catalog.locale)
111
                        ), outfile)
112
                    outfile.write(').install();')
113
                finally:
114
                    outfile.close()
setup.py
14 14

  
15 15
cmdclass = {}
16 16
try:
17
    from babeljs import compile_catalog_plusjs
17
    from buildenv.babeljs import compile_catalog_plusjs
18 18
except ImportError:
19 19
    pass
20 20
else:
vigigraph/controllers/root.py
67 67
            localedir = os.path.join(conf['paths']['root'], 'i18n')
68 68

  
69 69
        lang = get_lang()
70
        modules = (
71
            (conf['package'].__name__, localedir),
72
            ('vigilo-themes', resource_filename('vigilo.themes.i18n', '')),
73
            ('vigilo-vigigraph-enterprise',
74
             resource_filename('vigilo.vigigraph_enterprise.i18n', '')),
75
        )
70 76

  
71
        # Localise le fichier *.mo actuellement chargé
72
        # et génère le chemin jusqu'au *.js correspondant.
73
        filename = gettext.find(conf['package'].__name__, localedir,
74
            languages=lang)
75
        js = filename[:-3] + '.js'
76
        # Récupère et envoie le contenu du fichier de traduction *.js.
77
        fhandle = open(js, 'r')
78
        translations = fhandle.read()
79
        fhandle.close()
80

  
81
        # Même chose pour les thèmes
82
        themes_filename = gettext.find(
83
            'vigilo-themes',
84
            resource_filename('vigilo.themes.i18n', ''),
85
            languages=lang)
86
        themes_js = themes_filename[:-3] + '.js'
87
        fhandle = open(themes_js, 'r')
88
        translations += fhandle.read()
89
        fhandle.close()
77
        # Charge et installe le fichier JS de traduction de chaque module
78
        translations = "babel.Translations.load("
79
        for domain, directory in modules:
80
            try:
81
                mofile = gettext.find(domain, directory, languages=lang)
82
                if mofile is None:
83
                    continue
90 84

  
91
        # Extensions Enterprise
92
        try:
93
            ent_filename = gettext.find(
94
                'vigilo-vigigraph-enterprise',
95
                resource_filename('vigilo.vigigraph_enterprise.i18n', ''),
96
                languages=lang)
97
        except ImportError:
98
            pass
99
        else:
100
            # Le nom du fichier sera None s'il n'existe pas
101
            # de traductions dans la langue demandée.
102
            if ent_filename is not None:
103
                fhandle = open(ent_filename[:-3] + '.js', 'r')
85
                fhandle = open(mofile[:-3] + '.js', 'r')
104 86
                translations += fhandle.read()
105 87
                fhandle.close()
88
                translations += ").load("
89
            except ImportError:
90
                pass
91
        translations += "{}).install()"
106 92

  
107 93
        response.headers['Content-Type'] = 'text/javascript; charset=utf-8'
108 94
        return translations

Also available in: Unified diff