| 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 | |
| 119 | %s |
| 120 | } |
| 121 | #undef LNG |
| 122 | |
| 123 | """ % (ifdefs, lngtable)) |
| 124 | |
| 125 | |
| 126 | def ReplaceGeneratedPartOfFile(fname, func): |
| 127 | """ |
| 128 | Replaces the part of file marked with the special comments with the |
| 129 | output of func. |
| 130 | |
| 131 | fname is the name of the input file and func must be a function taking |
| 132 | a file and language table on input and writing the appropriate chunk to |
| 133 | this file, e.g. WriteEnum or WriteTable. |
| 134 | """ |
| 135 | fin = open(fname, 'rt') |
| 136 | fnameNew = fname + '.new' |
| 137 | fout = open(fnameNew, 'wt') |
| 138 | betweenBeginAndEnd = 0 |
| 139 | afterEnd = 0 |
| 140 | for l in fin.readlines(): |
| 141 | if l == '// --- --- --- generated code begins here --- --- ---\n': |
| 142 | if betweenBeginAndEnd or afterEnd: |
| 143 | print 'Unexpected starting comment.' |
| 144 | betweenBeginAndEnd = 1 |
| 145 | fout.write(l) |
| 146 | func(fout, table) |
| 147 | elif l == '// --- --- --- generated code ends here --- --- ---\n': |
| 148 | if not betweenBeginAndEnd: |
| 149 | print 'End comment found before the starting one?' |
| 150 | break |
| 151 | |
| 152 | betweenBeginAndEnd = 0 |
| 153 | afterEnd = 1 |
| 154 | |
| 155 | if not betweenBeginAndEnd: |
| 156 | fout.write(l) |
| 157 | |
| 158 | if not afterEnd: |
| 159 | print 'Failed to process %s.' % fname |
| 160 | os.remove(fnameNew) |
| 161 | sys.exit(1) |
| 162 | |
| 163 | os.remove(fname) |
| 164 | os.rename(fnameNew, fname) |
| 165 | |
| 166 | table = ReadTable() |
| 167 | ReplaceGeneratedPartOfFile('include/wx/language.h', WriteEnum) |
| 168 | ReplaceGeneratedPartOfFile('interface/wx/language.h', WriteEnum) |
| 169 | ReplaceGeneratedPartOfFile('src/common/languageinfo.cpp', WriteTable) |