]>
Commit | Line | Data |
---|---|---|
d3f3e35f VS |
1 | #!/usr/bin/env python |
2 | ||
741cca6b VZ |
3 | # Run this script from top-level wxWidgets directory to update the contents of |
4 | # include/wx/intl.h and src/common/intl.cpp using information from langtabl.txt | |
d3f3e35f | 5 | # |
741cca6b VZ |
6 | # Warning: error detection and reporting here is rudimentary, check if the |
7 | # files were updated correctly with "svn diff" before committing them! | |
d3f3e35f | 8 | |
741cca6b | 9 | import os |
d3f3e35f | 10 | import string |
741cca6b | 11 | import sys |
d3f3e35f VS |
12 | |
13 | def ReadTable(): | |
14 | table = [] | |
741cca6b VZ |
15 | try: |
16 | f = open('misc/languages/langtabl.txt') | |
17 | except: | |
18 | print "Did you run the script from top-level wxWidgets directory?" | |
19 | raise | |
20 | ||
d3f3e35f VS |
21 | for i in f.readlines(): |
22 | ispl = i.split() | |
978af864 | 23 | table.append((ispl[0], ispl[1], ispl[2], ispl[3], ispl[4], string.join(ispl[5:]))) |
d3f3e35f VS |
24 | f.close() |
25 | return table | |
26 | ||
27 | ||
741cca6b | 28 | def WriteEnum(f, table): |
d3f3e35f | 29 | f.write(""" |
969daeea FM |
30 | /** |
31 | The languages supported by wxLocale. | |
32 | ||
33 | This enum is generated by misc/languages/genlang.py | |
34 | When making changes, please put them into misc/languages/langtabl.txt | |
35 | */ | |
d3f3e35f VS |
36 | enum wxLanguage |
37 | { | |
969daeea FM |
38 | /// User's default/preffered language as got from OS. |
39 | wxLANGUAGE_DEFAULT, | |
40 | ||
41 | /// Unknown language, returned if wxLocale::GetSystemLanguage fails. | |
42 | wxLANGUAGE_UNKNOWN, | |
43 | ||
d3f3e35f | 44 | """); |
b5e2a450 | 45 | knownLangs = [] |
d3f3e35f | 46 | for i in table: |
b5e2a450 VZ |
47 | if i[0] not in knownLangs: |
48 | f.write(' %s,\n' % i[0]) | |
49 | knownLangs.append(i[0]) | |
d3f3e35f | 50 | f.write(""" |
969daeea | 51 | /// For custom, user-defined languages. |
d3f3e35f VS |
52 | wxLANGUAGE_USER_DEFINED |
53 | }; | |
323af196 | 54 | |
d3f3e35f | 55 | """) |
d3f3e35f VS |
56 | |
57 | ||
741cca6b | 58 | def WriteTable(f, table): |
63986ba6 VS |
59 | all_langs = [] |
60 | all_sublangs = [] | |
969daeea | 61 | |
63986ba6 | 62 | lngtable = '' |
969daeea FM |
63 | ifdefs = '' |
64 | ||
63986ba6 VS |
65 | for i in table: |
66 | ican = '"%s"' % i[1] | |
67 | if ican == '"-"': ican = '""' | |
68 | ilang = i[2] | |
69 | if ilang == '-': ilang = '0' | |
70 | isublang = i[3] | |
71 | if isublang == '-': isublang = '0' | |
978af864 VZ |
72 | if (i[4] == "LTR") : |
73 | ilayout = "wxLayout_LeftToRight" | |
74 | elif (i[4] == "RTL"): | |
75 | ilayout = "wxLayout_RightToLeft" | |
76 | else: | |
77 | print "ERROR: Invalid value for the layout direction"; | |
78 | lngtable += ' LNG(%-38s %-7s, %-15s, %-34s, %s, %s)\n' % \ | |
79 | ((i[0]+','), ican, ilang, isublang, ilayout, i[5]) | |
63986ba6 VS |
80 | if ilang not in all_langs: all_langs.append(ilang) |
81 | if isublang not in all_sublangs: all_sublangs.append(isublang) | |
82 | ||
83 | for s in all_langs: | |
84 | if s != '0': | |
85 | ifdefs += '#ifndef %s\n#define %s (0)\n#endif\n' % (s, s) | |
86 | for s in all_sublangs: | |
969daeea | 87 | if s != '0' and s != 'SUBLANG_DEFAULT': |
63986ba6 VS |
88 | ifdefs += '#ifndef %s\n#define %s SUBLANG_DEFAULT\n#endif\n' % (s, s) |
89 | ||
d3f3e35f VS |
90 | f.write(""" |
91 | // This table is generated by misc/languages/genlang.py | |
92 | // When making changes, please put them into misc/languages/langtabl.txt | |
93 | ||
b5e2a450 | 94 | #if !defined(__WIN32__) || defined(__WXMICROWIN__) |
63986ba6 VS |
95 | |
96 | #define SETWINLANG(info,lang,sublang) | |
97 | ||
98 | #else | |
99 | ||
d3f3e35f VS |
100 | #define SETWINLANG(info,lang,sublang) \\ |
101 | info.WinLang = lang, info.WinSublang = sublang; | |
63986ba6 VS |
102 | |
103 | %s | |
104 | ||
105 | #endif // __WIN32__ | |
d3f3e35f | 106 | |
978af864 | 107 | #define LNG(wxlang, canonical, winlang, winsublang, layout, desc) \\ |
d3f3e35f VS |
108 | info.Language = wxlang; \\ |
109 | info.CanonicalName = wxT(canonical); \\ | |
978af864 VZ |
110 | info.LayoutDirection = layout; \\ |
111 | info.Description = wxT(desc); \\ | |
d3f3e35f VS |
112 | SETWINLANG(info, winlang, winsublang) \\ |
113 | AddLanguage(info); | |
114 | ||
115 | void wxLocale::InitLanguagesDB() | |
116 | { | |
117 | wxLanguageInfo info; | |
118 | wxStringTokenizer tkn; | |
d3f3e35f | 119 | |
b5e2a450 VZ |
120 | %s |
121 | } | |
d3f3e35f | 122 | #undef LNG |
323af196 | 123 | |
63986ba6 | 124 | """ % (ifdefs, lngtable)) |
d3f3e35f VS |
125 | |
126 | ||
741cca6b VZ |
127 | def ReplaceGeneratedPartOfFile(fname, func): |
128 | """ | |
129 | Replaces the part of file marked with the special comments with the | |
130 | output of func. | |
131 | ||
132 | fname is the name of the input file and func must be a function taking | |
133 | a file and language table on input and writing the appropriate chunk to | |
134 | this file, e.g. WriteEnum or WriteTable. | |
135 | """ | |
136 | fin = open(fname, 'rt') | |
137 | fnameNew = fname + '.new' | |
138 | fout = open(fnameNew, 'wt') | |
139 | betweenBeginAndEnd = 0 | |
140 | afterEnd = 0 | |
141 | for l in fin.readlines(): | |
142 | if l == '// --- --- --- generated code begins here --- --- ---\n': | |
143 | if betweenBeginAndEnd or afterEnd: | |
144 | print 'Unexpected starting comment.' | |
145 | betweenBeginAndEnd = 1 | |
146 | fout.write(l) | |
147 | func(fout, table) | |
148 | elif l == '// --- --- --- generated code ends here --- --- ---\n': | |
149 | if not betweenBeginAndEnd: | |
150 | print 'End comment found before the starting one?' | |
151 | break | |
152 | ||
153 | betweenBeginAndEnd = 0 | |
154 | afterEnd = 1 | |
155 | ||
156 | if not betweenBeginAndEnd: | |
157 | fout.write(l) | |
158 | ||
159 | if not afterEnd: | |
160 | print 'Failed to process %s.' % fname | |
161 | os.remove(fnameNew) | |
162 | sys.exit(1) | |
163 | ||
164 | os.remove(fname) | |
165 | os.rename(fnameNew, fname) | |
d3f3e35f VS |
166 | |
167 | table = ReadTable() | |
741cca6b VZ |
168 | ReplaceGeneratedPartOfFile('include/wx/intl.h', WriteEnum) |
169 | ReplaceGeneratedPartOfFile('interface/wx/intl.h', WriteEnum) | |
170 | ReplaceGeneratedPartOfFile('src/common/intl.cpp', WriteTable) |