File: babel.py

package info (click to toggle)
python-django-babel 0.4.0-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 180 kB
  • sloc: python: 434; makefile: 173
file content (93 lines) | stat: -rw-r--r-- 3,326 bytes parent folder | download
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#-*- coding: utf-8 -*-
import os
from distutils.dist import Distribution
from optparse import make_option
from subprocess import call

from django.core.management.base import LabelCommand, CommandError
from django.conf import settings


class Command(LabelCommand):

    args = '[makemessages] [compilemessages]'

    option_list = LabelCommand.option_list + (
        make_option(
            '--locale', '-l',
            default=None, dest='locale', action='append',
            help='Creates or updates the message files for the given locale(s)'
                 ' (e.g pt_BR). Can be used multiple times.'),
        make_option('--domain', '-d',
            default='django', dest='domain',
            help='The domain of the message files (default: "django").'),
        make_option('--mapping-file', '-F',
            default=None, dest='mapping_file',
            help='Mapping file')
    )

    def handle_label(self, command, **options):
        if command not in ('makemessages', 'compilemessages'):
            raise CommandError(
                "You must either apply 'makemessages' or 'compilemessages'"
            )

        if command == 'makemessages':
            self.handle_makemessages(**options)
        if command == 'compilemessages':
            self.handle_compilemessages(**options)

    def handle_makemessages(self, **options):
        locale_paths = list(settings.LOCALE_PATHS)
        domain = options.pop('domain')
        locales = options.pop('locale')

        # support for mapping file specification via setup.cfg
        # TODO: Try to support all possible options.
        distribution = Distribution()
        distribution.parse_config_files(distribution.find_config_files())

        mapping_file = options.pop('mapping_file', None)
        has_extract = 'extract_messages' in distribution.command_options
        if mapping_file is None and has_extract:
            opts = distribution.command_options['extract_messages']
            try:
                mapping_file = opts['mapping_file'][1]
            except (IndexError, KeyError):
                mapping_file = None

        for path in locale_paths:
            potfile = os.path.join(path, '%s.pot' % domain)
            if not os.path.exists(potfile):
                continue

            cmd = ['pybabel', 'extract', '-o', potfile]

            if mapping_file is not None:
                cmd.extend(['-F', mapping_file])

            cmd.append(os.path.dirname(os.path.relpath(path)))

            call(cmd)

            for locale in locales:
                cmd = ['pybabel', 'update', '-D', domain,
                       '-i', potfile,
                       '-d', os.path.relpath(path),
                       '-l', locale]
                call(cmd)

    def handle_compilemessages(self, **options):
        locale_paths = list(settings.LOCALE_PATHS)
        domain = options.pop('domain')
        locales = options.pop('locale')

        for path in locale_paths:
            for locale in locales:
                po_file = os.path.join(
                    path, locale, 'LC_MESSAGES', domain + '.po'
                )
                if os.path.exists(po_file):
                    cmd = ['pybabel', 'compile', '-D', domain,
                           '-d', path, '-l', locale]
                    call(cmd)