Fix ar9x compile/boot (#7102)
[opentx.git] / radio / util / translate.py
blob53e113ca7b34a019695c8d16bdd308c0e2d14735
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Imports
5 import argparse
6 import codecs
7 import sys
9 translations = {'cz_reduced': [('\\200', u'á'),
10 ('\\201', u'č'),
11 ('\\201', u'Č'),
12 ('\\202', u'é'),
13 ('\\203', u'ě'),
14 ('\\203', u'Ě'),
15 ('\\204', u'í'),
16 ('\\205', u'ó'),
17 ('\\206', u'ř'),
18 ('\\207', u'š'),
19 ('\\207', u'Š'),
20 ('\\210', u'ú'),
21 ('\\210', u'Ú'),
22 ('\\211', u'ů'),
23 ('\\211', u'Ů'),
24 ('\\212', u'ý'),
25 ('\\213', u'Á'),
26 ('\\214', u'Í'),
27 ('\\215', u'Ř'),
28 ('\\216', u'Ý'),
29 ('\\217', u'ž'),
30 ('\\217', u'Ž'),
31 ('\\220', u'É')],
33 'cz': [('\\200', u'ě'),
34 ('\\201', u'š'),
35 ('\\202', u'č'),
36 ('\\203', u'ř'),
37 ('\\204', u'ž'),
38 ('\\205', u'ý'),
39 ('\\206', u'á'),
40 ('\\207', u'í'),
41 ('\\210', u'é'),
42 ('\\211', u'ň'),
43 ('\\212', u'ó'),
44 ('\\213', u'ů'),
45 ('\\214', u'ú'),
46 ('\\215', u'ď'),
47 ('\\216', u'ť'),
48 ('\\217', u'Ě'),
49 ('\\220', u'Š'),
50 ('\\221', u'Č'),
51 ('\\222', u'Ř'),
52 ('\\223', u'Ž'),
53 ('\\224', u'Ý'),
54 ('\\225', u'Á'),
55 ('\\226', u'Í'),
56 ('\\227', u'É'),
57 ('\\230', u'Ň'),
58 ('\\231', u'Ó'),
59 ('\\232', u'Ú'),
60 ('\\233', u'Ů'),
61 ('\\234', u'Ď'),
62 ('\\235', u'Ť')],
64 'de': [('\\200', u'Ä'),
65 ('\\201', u'ä'),
66 ('\\202', u'Ö'),
67 ('\\203', u'ö'),
68 ('\\204', u'Ü'),
69 ('\\205', u'ü'),
70 ('\\206', u'ß')],
72 'es': [('\\200', u'Ñ'),
73 ('\\201', u'ñ')],
75 'fi': [('\\200', u'å'),
76 ('\\201', u'ä'),
77 ('\\202', u'ö'),
78 ('\\203', u'Å'),
79 ('\\204', u'Ä'),
80 ('\\205', u'Ö')],
82 'fr': [('\\200', u'é'),
83 ('\\201', u'è'),
84 ('\\202', u'à'),
85 ('\\203', u'î'),
86 ('\\204', u'ç')],
88 'it': [('\\200', u'à'),
89 ('\\201', u'ù')],
91 'pl': [('\\200', u'ą'),
92 ('\\201', u'ć'),
93 ('\\202', u'ę'),
94 ('\\203', u'ł'),
95 ('\\204', u'ń'),
96 ('\\205', u'ó'),
97 ('\\206', u'ś'),
98 ('\\207', u'ż'),
99 ('\\210', u'ź'),
100 ('\\211', u'Ą'),
101 ('\\212', u'Ć'),
102 ('\\213', u'Ę'),
103 ('\\214', u'Ł'),
104 ('\\215', u'Ń'),
105 ('\\216', u'Ó'),
106 ('\\217', u'Ś'),
107 ('\\220', u'Ż'),
108 ('\\221', u'Ź')],
110 'pt': [('\\200', u'Á'),
111 ('\\201', u'á'),
112 ('\\202', u'Â'),
113 ('\\203', u'â'),
114 ('\\204', u'Ã'),
115 ('\\205', u'ã'),
116 ('\\206', u'À'),
117 ('\\207', u'à'),
118 ('\\210', u'Ç'),
119 ('\\211', u'ç'),
120 ('\\212', u'É'),
121 ('\\213', u'é'),
122 ('\\214', u'Ê'),
123 ('\\215', u'ê'),
124 ('\\216', u'Í'),
125 ('\\217', u'í'),
126 ('\\218', u'Ó'),
127 ('\\219', u'ó'),
128 ('\\220', u'Ô'),
129 ('\\221', u'ô'),
130 ('\\222', u'Õ'),
131 ('\\223', u'õ'),
132 ('\\224', u'Ú'),
133 ('\\225', u'ú'), ],
135 'se': [('\\200', u'å'),
136 ('\\201', u'ä'),
137 ('\\202', u'ö'),
138 ('\\203', u'Å'),
139 ('\\204', u'Ä'),
140 ('\\205', u'Ö')],
142 'en': [],
144 'nl': [],
146 'all': [('\\306', u'Δ'),
147 ('\\173', u'~'),
148 ('\\036', u'\\n'),
149 ('\\035', u'\\t')],
152 # Take care of command line options
153 parser = argparse.ArgumentParser(description='Encoder for open9x translations')
154 parser.add_argument('input', action="store", help="Input file name")
155 parser.add_argument('output', action="store", help="Output file name")
156 parser.add_argument('language', action="store", help="Two letter language identifier")
157 parser.add_argument("--reverse", help="Reversed char conversion (from number to char)", action="store_true")
158 args = parser.parse_args()
160 if args.language not in translations:
161 parser.error(args.language + ' is not a supported language. Try one of the supported ones: ' + str(list(translations.keys())))
162 sys.exit()
164 if args.reverse:
165 for translation in translations:
166 translations[translation] = [(after, before) for (before, after) in translations[translation]]
168 # Read the input file into a buffer
169 in_file = codecs.open(args.input, "r", "utf-8")
171 # Write the result to a temporary file
172 out_file = codecs.open(args.output, 'w', 'utf-8')
174 for line in in_file.readlines():
175 # Do the special chars replacements
176 for after, before in translations[args.language] + translations["all"]:
177 line = line.replace(before, after)
178 if line.startswith("#define ZSTR_"):
179 before = line[32:-2]
180 after = ""
181 for c in before:
182 if ord(c) >= ord('A') and ord(c) <= ord('Z'):
183 c = "\\%03o" % (ord(c) - ord('A') + 1)
184 elif ord(c) >= ord('a') and ord(c) <= ord('z'):
185 c = "\\%03o" % (-ord(c) + ord('a') + 255)
186 elif ord(c) >= ord('0') and ord(c) <= ord('9'):
187 c = "\\%03o" % (ord(c) - ord('0') + 27)
188 after = after + c
189 line = line[:32] + after + line[-2:]
190 out_file.write(line)
192 out_file.close()
193 in_file.close()