forked from ministryofjustice/govuk-bank-holidays
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_extensions.py
79 lines (61 loc) · 2.68 KB
/
setup_extensions.py
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import distutils.log
import os
import subprocess
import setuptools
class MessagesCommand(setuptools.Command):
user_options = []
def __init__(self, *args, **kwargs):
super(MessagesCommand, self).__init__(*args, **kwargs)
self.domain = 'messages'
self.cwd = os.getcwd()
self.root_path = os.path.join(os.path.dirname(__file__))
self.app_path = 'govuk_bank_holidays'
self.locale_path = os.path.join(self.app_path, 'locale')
self.pot_name = '%s.pot' % self.domain
self.po_name = '%s.po' % self.domain
self.mo_name = '%s.mo' % self.domain
self.pot_path = os.path.join(self.locale_path, self.pot_name)
self.locales = [
path
for path in os.listdir(self.locale_path)
if os.path.isfile(os.path.join(self.locale_path, path, 'LC_MESSAGES', self.po_name))
]
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
os.chdir(self.root_path)
self.run_command()
os.chdir(self.cwd)
def run_command(self):
raise NotImplementedError
class MakeMessages(MessagesCommand):
description = 'update localisation messages files'
def find_source_files(self):
return [os.path.join(self.app_path, 'i18n.py')]
def run_command(self):
self.announce('Writing intermediate POT file', level=distutils.log.INFO)
xgettext = ['xgettext', '-d', self.domain, '-o', self.pot_path,
'--language=Python', '--from-code=UTF-8', '--no-wrap']
subprocess.check_call(xgettext + self.find_source_files())
msgmerge = ['msgmerge', '--no-wrap', '--previous', '--update']
for locale in self.locales:
self.announce('Writing PO file for %s locale' % locale, level=distutils.log.INFO)
po_path = os.path.join(self.locale_path, locale, 'LC_MESSAGES', self.po_name)
subprocess.check_call(msgmerge + [po_path, self.pot_path])
class CompileMessages(MessagesCommand):
description = 'compile localisation messages files'
def run_command(self):
msgfmt = ['msgfmt', '--check']
for locale in self.locales:
self.announce('Compiling PO file for %s locale' % locale, level=distutils.log.INFO)
po_path = os.path.join(self.locale_path, locale, 'LC_MESSAGES', self.po_name)
mo_path = os.path.join(self.locale_path, locale, 'LC_MESSAGES', self.mo_name)
subprocess.check_call(msgfmt + ['--output-file', mo_path, po_path])
command_classes = {
'makemessages': MakeMessages,
'compilemessages': CompileMessages,
}