-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_language_headers.py
92 lines (72 loc) · 3.95 KB
/
generate_language_headers.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
# coding=utf-8
class LanguageHeadersGenerator():
def __init__(self, input_filename):
import codecs
in_file = codecs.open(input_filename, 'r', encoding='utf-8')
self.data_matrix = []
for line in in_file:
if line.strip() != '':
self.data_matrix.append(line.strip('\n').split('\t'))
in_file.close()
self.row_offset = 1 # Row number where the data begins (row 0 contains titles)
self.max_word_length = 20 # Maximum length for the words in the translated strings
def create_dicts(self):
# Finds languages IDs (ISO 639-1) enclosed in brackets
lang_id_list = [(i, e[e.find('(')+1:e.find(')')]) for i, e in enumerate(self.data_matrix[0]) if ('(' in e and ')' in e)]
for lang_element in lang_id_list:
if lang_element[1] not in iso_639_1_language_ids:
print " *** WARNING: Language ID not found in ISO 639-1 list (%s)" % lang_element[1]
# Get list of Labels from first column
label_list = [line[0] for line in self.data_matrix[self.row_offset:]]
for lang_col, lang_id in lang_id_list:
translations_list = [row[lang_col] for row in self.data_matrix[self.row_offset:]]
self._check_words_length(lang_id, translations_list)
self._export_files(lang_id, zip(label_list, translations_list))
def _check_words_length(self, lang_id, translations_list):
for string in translations_list:
for word in string.split(' '):
if len(word) > self.max_word_length:
print " *** WARNING: Found word with length over the limit of %d" % self.max_word_length
print " Language = %s" % lang_id
print " Word = %s" % word
print " String = %s" % string
def _export_files(self, lang_id, translations_tuples):
out_filename = "Language_%2s.h" % lang_id
out_file = open(out_filename, 'w')
out_file.write("#ifndef LANGUAGE_%2s_H\n" % lang_id)
out_file.write("#define LANGUAGE_%2s_H\n" % lang_id)
out_file.write("\n")
out_file.write("#include <avr/pgmspace.h>\n")
out_file.write("\n")
for key, value in translations_tuples:
encoded_value = ""
for c in value:
try:
encoded_c = c.encode('ascii')
except:
if lang_id == 'RU':
if repr(c) == "u'\u0410'":
encoded_c = 'A'
else:
encoded_c = '""%s""' % repr(c.encode('iso-8859-5'))[1:-1]
else:
encoded_c = '""%s""' % repr(c)[2:-1]
finally:
encoded_value += encoded_c
out_file.write('%-55s = "%s";\n' % ("const char %2s_%s[] PROGMEM" % (lang_id, key), encoded_value))
out_file.write("\n#endif //LANGUAGE_%2s_H" % lang_id)
out_file.close()
print "Exported translation file for %2s (%s)" % (lang_id, out_filename)
iso_639_1_language_ids = ['AA', 'AB', 'AF', 'AK', 'SQ', 'AM', 'AR', 'AN', 'HY', 'AS', 'AV', 'AE', 'AY', 'AZ', 'BA', 'BM', 'EU', 'BE', 'BN',\
'BH', 'BI', 'BO', 'BS', 'BR', 'BG', 'MY', 'CA', 'CS', 'CH', 'CE', 'ZH', 'CU', 'CV', 'KW', 'CO', 'CR', 'CY', 'CS', 'DA', 'DE', 'DV', 'NL',\
'DZ', 'EL', 'EN', 'EO', 'ET', 'EU', 'EE', 'FO', 'FA', 'FJ', 'FI', 'FR', 'FR', 'FY', 'FF', 'GA', 'DE', 'GD', 'GA', 'GL', 'GV', 'EL', 'GN',\
'GU', 'HT', 'HA', 'HE', 'HZ', 'HI', 'HO', 'HR', 'HU', 'HY', 'IG', 'IS', 'IO', 'II', 'IU', 'IE', 'IA', 'ID', 'IK', 'IS', 'IT', 'JV', 'JA',\
'KL', 'KN', 'KS', 'KA', 'KR', 'KK', 'KM', 'KI', 'RW', 'KY', 'KV', 'KG', 'KO', 'KJ', 'KU', 'LO', 'LA', 'LV', 'LI', 'LN', 'LT', 'LB', 'LU',\
'LG', 'MK', 'MH', 'ML', 'MI', 'MR', 'MS', 'MI', 'MK', 'MG', 'MT', 'MN', 'MI', 'MS', 'MY', 'NA', 'NV', 'NR', 'ND', 'NG', 'NE', 'NL', 'NN',\
'NB', 'NO', 'OC', 'OJ', 'OR', 'OM', 'OS', 'PA', 'FA', 'PI', 'PL', 'PT', 'PS', 'QU', 'RM', 'RO', 'RO', 'RN', 'RU', 'SG', 'SA', 'SI', 'SK',\
'SK', 'SL', 'SE', 'SM', 'SN', 'SD', 'SO', 'ST', 'ES', 'SQ', 'SC', 'SR', 'SS', 'SU', 'SW', 'SV', 'TY', 'TA', 'TT', 'TE', 'TG', 'TL', 'TH',\
'BO', 'TI', 'TO', 'TN', 'TS', 'TK', 'TR', 'TW', 'UG', 'UK', 'UR', 'UZ', 'VE', 'VI', 'VO', 'CY', 'WA', 'WO', 'XH', 'YI', 'YO', 'ZA', 'ZH', 'ZU']
if __name__ == "__main__":
input_filename = 'Texto de Firmware - Graphic LCD.tsv'
gen = LanguageHeadersGenerator(input_filename)
gen.create_dicts()