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