]> git.saurik.com Git - wxWidgets.git/blob - misc/languages/genlang.py
Document ctors creating a wxString from repeated characters.
[wxWidgets.git] / misc / languages / genlang.py
1 #!/usr/bin/env python
2
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
5 #
6 # Warning: error detection and reporting here is rudimentary, check if the
7 # files were updated correctly with "svn diff" before committing them!
8
9 import os
10 import string
11 import sys
12
13 def ReadTable():
14 table = []
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
21 for i in f.readlines():
22 ispl = i.split()
23 table.append((ispl[0], ispl[1], ispl[2], ispl[3], ispl[4], string.join(ispl[5:])))
24 f.close()
25 return table
26
27
28 def WriteEnum(f, table):
29 f.write("""
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 */
36 enum wxLanguage
37 {
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
44 """);
45 knownLangs = []
46 for i in table:
47 if i[0] not in knownLangs:
48 f.write(' %s,\n' % i[0])
49 knownLangs.append(i[0])
50 f.write("""
51 /// For custom, user-defined languages.
52 wxLANGUAGE_USER_DEFINED
53 };
54
55 """)
56
57
58 def WriteTable(f, table):
59 all_langs = []
60 all_sublangs = []
61
62 lngtable = ''
63 ifdefs = ''
64
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'
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])
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:
87 if s != '0' and s != 'SUBLANG_DEFAULT':
88 ifdefs += '#ifndef %s\n#define %s SUBLANG_DEFAULT\n#endif\n' % (s, s)
89
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
94 #if !defined(__WIN32__) || defined(__WXMICROWIN__)
95
96 #define SETWINLANG(info,lang,sublang)
97
98 #else
99
100 #define SETWINLANG(info,lang,sublang) \\
101 info.WinLang = lang, info.WinSublang = sublang;
102
103 %s
104
105 #endif // __WIN32__
106
107 #define LNG(wxlang, canonical, winlang, winsublang, layout, desc) \\
108 info.Language = wxlang; \\
109 info.CanonicalName = wxT(canonical); \\
110 info.LayoutDirection = layout; \\
111 info.Description = wxT(desc); \\
112 SETWINLANG(info, winlang, winsublang) \\
113 AddLanguage(info);
114
115 void wxLocale::InitLanguagesDB()
116 {
117 wxLanguageInfo info;
118 wxStringTokenizer tkn;
119
120 %s
121 }
122 #undef LNG
123
124 """ % (ifdefs, lngtable))
125
126
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)
166
167 table = ReadTable()
168 ReplaceGeneratedPartOfFile('include/wx/intl.h', WriteEnum)
169 ReplaceGeneratedPartOfFile('interface/wx/intl.h', WriteEnum)
170 ReplaceGeneratedPartOfFile('src/common/intl.cpp', WriteTable)