forked from Jardel93/yougi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.py
executable file
·144 lines (123 loc) · 4.63 KB
/
i18n.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
import sys
import fnmatch
import argparse
import re
try:
import i18n_conf
except:
print 'Error in i18n_conf file'
sys.exit(1)
# Load Constants
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TEXT_BASE = getattr(i18n_conf, 'TEXT_BASE', '')
TEXT_TRANSLATE = getattr(i18n_conf, 'TEXT_TRANSLATE', '[TRANSLATE: "{key}"]')
# Get SRC_DIR
SRC_DIR = getattr(i18n_conf, 'SRC_DIR', None)
if not SRC_DIR:
print 'SRC_DIR is not configured in i18n_conf.py'
sys.exit(1)
SRC_DIR = os.path.join(BASE_DIR, *SRC_DIR.split('/'))
# Get VAR_NAME
VAR_NAME = getattr(i18n_conf, 'VAR_NAME', None)
if not VAR_NAME:
print 'VAR_NAME is not configured in i18n_conf.py'
sys.exit(1)
# Get I18N_DIR
I18N_DIR = getattr(i18n_conf, 'I18N_DIR', None)
if not I18N_DIR:
print 'I18N_DIR is not configured in i18n_conf.py'
sys.exit(1)
I18N_DIR = os.path.join(BASE_DIR, *I18N_DIR.split('/'))
def rglob(path, pattern):
matches = []
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, pattern):
matches.append(os.path.join(root, filename))
return matches
class Bundle(object):
def __init__(self, filename, debug=False):
self.filename = filename
self.locale = self._load_locale()
self.keywords = self._load_keywords()
self.debug = debug
def _load_locale(self):
_, filename = os.path.split(self.filename)
locale_re = re.match(r'Resources_(?P<locale>[^\.]+).properties', filename)
if locale_re:
return locale_re.group('locale')
else:
return 'default'
def _load_keywords(self):
keywords = {}
with open(self.filename) as f:
for line in f:
if not line.startswith("#") and "=" in line:
key, value = line.split("=", 1)
keywords[key] = value
return keywords
def add(self, new_keyword):
msg = TEXT_TRANSLATE.strip()
msg = msg.format(key=new_keyword)
self.keywords[new_keyword] = msg + '\n'
def save(self):
with open(self.filename, 'w') as f:
if TEXT_BASE:
f.write(TEXT_BASE)
if self.debug:
print "Saving keywords in: {fn}".format(fn=self.filename)
conf_language = self.keywords.pop('confLanguage')
f.write("confLanguage={}\n\n".format(conf_language))
for key in sorted(self.keywords.keys()):
f.write("{key}={value}\n".format(key=key, value=self.keywords[key].rstrip('\n')))
self.keywords['confLanguage'] = conf_language
class I18NExtractor(object):
def __init__(self, var_name, src_dir, i18n_dir, debug=False):
self.var_name = var_name
self.src_dir = src_dir
self.i18n_dir = i18n_dir
self.debug = debug
self.src_keywords = []
self.bundles = []
self.new_keywords = []
self.load_bundles()
self.load_src_keywords()
self.load_new_keywords()
def load_bundles(self):
for filename in rglob(self.i18n_dir, 'Resources*.properties'):
self.bundles.append(Bundle(filename, debug=self.debug))
def load_src_keywords(self):
for filename in rglob(self.src_dir, '*.xhtml'):
if self.debug:
print "Extracting: {fn}".format(fn=filename)
with open(filename) as f:
text = f.read()
self.src_keywords += re.findall(r'{v}\.(\w+)'.format(v=self.var_name), text)
self.src_keywords = list(set(self.src_keywords))
def load_new_keywords(self):
bundle_default = [b for b in self.bundles if b.locale == 'default'][0]
new_keywords = set(self.src_keywords) - set(bundle_default.keywords.keys())
self.new_keywords = list(new_keywords)
def extract(self):
for new_keyword in self.new_keywords:
for bundle in self.bundles:
bundle.add(new_keyword)
def save(self):
if not self.new_keywords:
print "*** No new keyword was found"
else:
print "*** New keywords: {kw}".format(kw=', '.join(self.new_keywords))
for bundle in self.bundles:
bundle.save()
print "*** Finished!"
def main(debug):
i18n = I18NExtractor(VAR_NAME, SRC_DIR, I18N_DIR, debug)
i18n.extract()
i18n.save()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Extract keywords from source to i18n')
parser.add_argument('-d', '--debug', action='store_true', help="execute with debug mode")
args = parser.parse_args()
main(args.debug)