OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 '''Support for formatting an RC file for compilation. | 6 '''Support for formatting an RC file for compilation. |
7 ''' | 7 ''' |
8 | 8 |
9 import os | 9 import os |
10 import types | 10 import types |
11 import re | 11 import re |
12 | 12 |
13 from grit import lazy_re | 13 from grit import lazy_re |
14 from grit import util | 14 from grit import util |
15 from grit.format import interface | 15 from grit.format import interface |
| 16 from grit.node import misc |
| 17 |
16 | 18 |
17 ''' | 19 ''' |
18 This dictionary defines the langauge charset pair lookup table, which is used | 20 This dictionary defines the langauge charset pair lookup table, which is used |
19 for replacing the GRIT expand variables for language info in Product Version | 21 for replacing the GRIT expand variables for language info in Product Version |
20 resource. The key is the language ISO country code, and the value | 22 resource. The key is the language ISO country code, and the value |
21 is the language and character-set pair, which is a hexadecimal string | 23 is the language and character-set pair, which is a hexadecimal string |
22 consisting of the concatenation of the language and character-set identifiers. | 24 consisting of the concatenation of the language and character-set identifiers. |
23 The first 4 digit of the value is the hex value of LCID, the remaining | 25 The first 4 digit of the value is the hex value of LCID, the remaining |
24 4 digits is the hex value of character-set id(code page)of the language. | 26 4 digits is the hex value of character-set id(code page)of the language. |
25 | 27 |
(...skipping 14 matching lines...) Expand all Loading... |
40 END | 42 END |
41 BLOCK "VarFileInfo" | 43 BLOCK "VarFileInfo" |
42 BEGIN | 44 BEGIN |
43 VALUE "Translation", [GRITVERLANGID], [GRITVERCHARSETID] | 45 VALUE "Translation", [GRITVERLANGID], [GRITVERCHARSETID] |
44 END | 46 END |
45 END | 47 END |
46 | 48 |
47 ''' | 49 ''' |
48 | 50 |
49 _LANGUAGE_CHARSET_PAIR = { | 51 _LANGUAGE_CHARSET_PAIR = { |
50 'ar' : '040104e8', | 52 # Language neutral LCID, unicode(1200) code page. |
51 'fi' : '040b04e4', | 53 'neutral' : '000004b0', |
52 'ko' : '041203b5', | 54 # LANG_USER_DEFAULT LCID, unicode(1200) code page. |
53 'es' : '040a04e4', | 55 'userdefault' : '040004b0', |
54 'bg' : '040204e3', | 56 'ar' : '040104e8', |
| 57 'fi' : '040b04e4', |
| 58 'ko' : '041203b5', |
| 59 'es' : '040a04e4', |
| 60 'bg' : '040204e3', |
55 # No codepage for filipino, use unicode(1200). | 61 # No codepage for filipino, use unicode(1200). |
56 'fil' : '046404b0', | 62 'fil' : '046404e4', |
57 'fr' : '040c04e4', | 63 'fr' : '040c04e4', |
58 'lv' : '042604e9', | 64 'lv' : '042604e9', |
59 'sv' : '041d04e4', | 65 'sv' : '041d04e4', |
60 'ca' : '040304e4', | 66 'ca' : '040304e4', |
61 'de' : '040704e4', | 67 'de' : '040704e4', |
62 'lt' : '042704e9', | 68 'lt' : '042704e9', |
63 # Do not use! This is only around for backwards | 69 # Do not use! This is only around for backwards |
64 # compatibility. Use file instead. | 70 # compatibility and will be removed - use fil instead |
65 'tl' : '0c0004b0', | 71 'tl' : '0c0004b0', |
66 'zh-CN' : '080403a8', | 72 'zh-CN' : '080403a8', |
67 'el' : '040804e5', | 73 'zh-TW' : '040403b6', |
68 'no' : '041404e4', | 74 'zh-HK' : '0c0403b6', |
69 'th' : '041e036a', | 75 'el' : '040804e5', |
70 'zh-TW' : '040403b6', | 76 'no' : '041404e4', |
71 'iw' : '040d04e7', | 77 'th' : '041e036a', |
72 'pl' : '041504e2', | 78 'he' : '040d04e7', |
73 'tr' : '041f04e6', | 79 'iw' : '040d04e7', |
74 'hr' : '041a04e4', | 80 'pl' : '041504e2', |
75 # No codepage for hindi, use unicode(1200). | 81 'tr' : '041f04e6', |
76 'hi' : '043904b0', | 82 'hr' : '041a04e4', |
77 'pt-PT' : '081604e4', | 83 # No codepage for Hindi, use unicode(1200). |
78 'pt-BR' : '041604e4', | 84 'hi' : '043904b0', |
79 'uk' : '042204e3', | 85 'pt-PT' : '081604e4', |
80 'cs' : '040504e2', | 86 'pt-BR' : '041604e4', |
81 'hu' : '040e04e2', | 87 'uk' : '042204e3', |
82 'ro' : '041804e2', | 88 'cs' : '040504e2', |
83 # No codepage for urdu, use unicode(1200). | 89 'hu' : '040e04e2', |
84 'ur' : '042004b0', | 90 'ro' : '041804e2', |
85 'da' : '040604e4', | 91 # No codepage for Urdu, use unicode(1200). |
86 'is' : '040f04e4', | 92 'ur' : '042004b0', |
87 'ru' : '041904e3', | 93 'da' : '040604e4', |
88 'vi' : '042a04ea', | 94 'is' : '040f04e4', |
89 'nl' : '041304e4', | 95 'ru' : '041904e3', |
90 'id' : '042104e4', | 96 'vi' : '042a04ea', |
91 'sr' : '081a04e2', | 97 'nl' : '041304e4', |
92 'en-GB' : '0809040e', | 98 'id' : '042104e4', |
93 'it' : '041004e4', | 99 'sr' : '081a04e2', |
94 'sk' : '041b04e2', | 100 'en-GB' : '0809040e', |
95 'et' : '042504e9', | 101 'it' : '041004e4', |
96 'ja' : '041103a4', | 102 'sk' : '041b04e2', |
97 'sl' : '042404e2', | 103 'et' : '042504e9', |
98 'en' : '040904b0', | 104 'ja' : '041103a4', |
99 'fake_bidi' : '040d04e7', | 105 'sl' : '042404e2', |
| 106 'en' : '040904b0', |
| 107 # LCID for Mexico; Windows does not support L.A. LCID. |
| 108 'es-419' : '080a04e4', |
| 109 # No codepage for Bengali, use unicode(1200). |
| 110 'bn' : '044504b0', |
| 111 'fa' : '042904e8', |
| 112 # No codepage for Gujarati, use unicode(1200). |
| 113 'gu' : '044704b0', |
| 114 # No codepage for Kannada, use unicode(1200). |
| 115 'kn' : '044b04b0', |
| 116 # Malay (Malaysia) [ms-MY] |
| 117 'ms' : '043e04e4', |
| 118 # No codepage for Malayalam, use unicode(1200). |
| 119 'ml' : '044c04b0', |
| 120 # No codepage for Marathi, use unicode(1200). |
| 121 'mr' : '044e04b0', |
| 122 # No codepage for Oriya , use unicode(1200). |
| 123 'or' : '044804b0', |
| 124 # No codepage for Tamil, use unicode(1200). |
| 125 'ta' : '044904b0', |
| 126 # No codepage for Telugu, use unicode(1200). |
| 127 'te' : '044a04b0', |
| 128 # No codepage for Amharic, use unicode(1200). >= Vista. |
| 129 'am' : '045e04b0', |
| 130 'sw' : '044104e4', |
| 131 'af' : '043604e4', |
| 132 'eu' : '042d04e4', |
| 133 'fr-CA' : '0c0c04e4', |
| 134 'gl' : '045604e4', |
| 135 # No codepage for Zulu, use unicode(1200). |
| 136 'zu' : '043504b0', |
| 137 'fake-bidi' : '040d04e7', |
100 } | 138 } |
101 | 139 |
| 140 # There is no appropriate sublang for Spanish (Latin America) [es-419], so we |
| 141 # use Mexico. SUBLANG_DEFAULT would incorrectly map to Spain. Unlike other |
| 142 # Latin American countries, Mexican Spanish is supported by VERSIONINFO: |
| 143 # http://msdn.microsoft.com/en-us/library/aa381058.aspx |
| 144 |
102 _LANGUAGE_DIRECTIVE_PAIR = { | 145 _LANGUAGE_DIRECTIVE_PAIR = { |
103 'ar' : 'LANG_ARABIC, SUBLANG_DEFAULT', | 146 'neutral' : 'LANG_NEUTRAL, SUBLANG_NEUTRAL', |
104 'fi' : 'LANG_FINNISH, SUBLANG_DEFAULT', | 147 'userdefault' : 'LANG_NEUTRAL, SUBLANG_DEFAULT', |
105 'ko' : 'LANG_KOREAN, SUBLANG_KOREAN', | 148 'ar' : 'LANG_ARABIC, SUBLANG_DEFAULT', |
106 'es' : 'LANG_SPANISH, SUBLANG_SPANISH_MODERN', | 149 'fi' : 'LANG_FINNISH, SUBLANG_DEFAULT', |
107 'bg' : 'LANG_BULGARIAN, SUBLANG_DEFAULT', | 150 'ko' : 'LANG_KOREAN, SUBLANG_KOREAN', |
108 'fil' : '100, SUBLANG_DEFAULT', # LANG_FILIPINO is not in winnt.h | 151 'es' : 'LANG_SPANISH, SUBLANG_SPANISH_MODERN', |
109 'fr' : 'LANG_FRENCH, SUBLANG_FRENCH', | 152 'bg' : 'LANG_BULGARIAN, SUBLANG_DEFAULT', |
110 'lv' : 'LANG_LATVIAN, SUBLANG_DEFAULT', | 153 # LANG_FILIPINO (100) not in VC 7 winnt.h. |
111 'sv' : 'LANG_SWEDISH, SUBLANG_SWEDISH', | 154 'fil' : '100, SUBLANG_DEFAULT', |
112 'ca' : 'LANG_CATALAN, SUBLANG_DEFAULT', | 155 'fr' : 'LANG_FRENCH, SUBLANG_FRENCH', |
113 'de' : 'LANG_GERMAN, SUBLANG_GERMAN', | 156 'lv' : 'LANG_LATVIAN, SUBLANG_DEFAULT', |
114 'lt' : 'LANG_LITHUANIAN, SUBLANG_LITHUANIAN', | 157 'sv' : 'LANG_SWEDISH, SUBLANG_SWEDISH', |
115 'tl' : 'LANG_NEUTRAL, SUBLANG_DEFAULT', # Do not use! See above. | 158 'ca' : 'LANG_CATALAN, SUBLANG_DEFAULT', |
116 'zh-CN' : 'LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED', | 159 'de' : 'LANG_GERMAN, SUBLANG_GERMAN', |
117 'el' : 'LANG_GREEK, SUBLANG_DEFAULT', | 160 'lt' : 'LANG_LITHUANIAN, SUBLANG_LITHUANIAN', |
118 'no' : 'LANG_NORWEGIAN, SUBLANG_DEFAULT', | 161 # Do not use! See above. |
119 'th' : 'LANG_THAI, SUBLANG_DEFAULT', | 162 'tl' : 'LANG_NEUTRAL, SUBLANG_DEFAULT', |
120 'zh-TW' : 'LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL', | 163 'zh-CN' : 'LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED', |
121 'iw' : 'LANG_HEBREW, SUBLANG_DEFAULT', | 164 'zh-TW' : 'LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL', |
122 'pl' : 'LANG_POLISH, SUBLANG_DEFAULT', | 165 'zh-HK' : 'LANG_CHINESE, SUBLANG_CHINESE_HONGKONG', |
123 'tr' : 'LANG_TURKISH, SUBLANG_DEFAULT', | 166 'el' : 'LANG_GREEK, SUBLANG_DEFAULT', |
124 'hr' : 'LANG_CROATIAN, SUBLANG_DEFAULT', | 167 'no' : 'LANG_NORWEGIAN, SUBLANG_DEFAULT', |
125 'hi' : 'LANG_HINDI, SUBLANG_DEFAULT', | 168 'th' : 'LANG_THAI, SUBLANG_DEFAULT', |
126 'pt-BR' : 'LANG_PORTUGUESE, SUBLANG_DEFAULT', | 169 'he' : 'LANG_HEBREW, SUBLANG_DEFAULT', |
127 'pt-PT' : 'LANG_PORTUGUESE, SUBLANG_PORTUGUESE', | 170 'iw' : 'LANG_HEBREW, SUBLANG_DEFAULT', |
128 'uk' : 'LANG_UKRAINIAN, SUBLANG_DEFAULT', | 171 'pl' : 'LANG_POLISH, SUBLANG_DEFAULT', |
129 'cs' : 'LANG_CZECH, SUBLANG_DEFAULT', | 172 'tr' : 'LANG_TURKISH, SUBLANG_DEFAULT', |
130 'hu' : 'LANG_HUNGARIAN, SUBLANG_DEFAULT', | 173 'hr' : 'LANG_CROATIAN, SUBLANG_DEFAULT', |
131 'ro' : 'LANG_ROMANIAN, SUBLANG_DEFAULT', | 174 'hi' : 'LANG_HINDI, SUBLANG_DEFAULT', |
132 'ur' : 'LANG_URDU, SUBLANG_DEFAULT', | 175 'pt-PT' : 'LANG_PORTUGUESE, SUBLANG_PORTUGUESE', |
133 'da' : 'LANG_DANISH, SUBLANG_DEFAULT', | 176 'pt-BR' : 'LANG_PORTUGUESE, SUBLANG_DEFAULT', |
134 'is' : 'LANG_ICELANDIC, SUBLANG_DEFAULT', | 177 'uk' : 'LANG_UKRAINIAN, SUBLANG_DEFAULT', |
135 'ru' : 'LANG_RUSSIAN, SUBLANG_DEFAULT', | 178 'cs' : 'LANG_CZECH, SUBLANG_DEFAULT', |
136 'vi' : 'LANG_VIETNAMESE, SUBLANG_DEFAULT', | 179 'hu' : 'LANG_HUNGARIAN, SUBLANG_DEFAULT', |
137 'nl' : 'LANG_DUTCH, SUBLANG_DEFAULT', | 180 'ro' : 'LANG_ROMANIAN, SUBLANG_DEFAULT', |
138 'id' : 'LANG_INDONESIAN, SUBLANG_DEFAULT', | 181 'ur' : 'LANG_URDU, SUBLANG_DEFAULT', |
139 'sr' : 'LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC', | 182 'da' : 'LANG_DANISH, SUBLANG_DEFAULT', |
140 'en-GB' : 'LANG_ENGLISH, SUBLANG_ENGLISH_UK', | 183 'is' : 'LANG_ICELANDIC, SUBLANG_DEFAULT', |
141 'it' : 'LANG_ITALIAN, SUBLANG_DEFAULT', | 184 'ru' : 'LANG_RUSSIAN, SUBLANG_DEFAULT', |
142 'sk' : 'LANG_SLOVAK, SUBLANG_DEFAULT', | 185 'vi' : 'LANG_VIETNAMESE, SUBLANG_DEFAULT', |
143 'et' : 'LANG_ESTONIAN, SUBLANG_DEFAULT', | 186 'nl' : 'LANG_DUTCH, SUBLANG_DEFAULT', |
144 'ja' : 'LANG_JAPANESE, SUBLANG_DEFAULT', | 187 'id' : 'LANG_INDONESIAN, SUBLANG_DEFAULT', |
145 'sl' : 'LANG_SLOVENIAN, SUBLANG_DEFAULT', | 188 'sr' : 'LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC', |
146 'en' : 'LANG_ENGLISH, SUBLANG_ENGLISH_US', | 189 'en-GB' : 'LANG_ENGLISH, SUBLANG_ENGLISH_UK', |
147 'fake_bidi' : 'LANG_HEBREW, SUBLANG_DEFAULT', | 190 'it' : 'LANG_ITALIAN, SUBLANG_DEFAULT', |
| 191 'sk' : 'LANG_SLOVAK, SUBLANG_DEFAULT', |
| 192 'et' : 'LANG_ESTONIAN, SUBLANG_DEFAULT', |
| 193 'ja' : 'LANG_JAPANESE, SUBLANG_DEFAULT', |
| 194 'sl' : 'LANG_SLOVENIAN, SUBLANG_DEFAULT', |
| 195 'en' : 'LANG_ENGLISH, SUBLANG_ENGLISH_US', |
| 196 # No L.A. sublang exists. |
| 197 'es-419' : 'LANG_SPANISH, SUBLANG_SPANISH_MEXICAN', |
| 198 'bn' : 'LANG_BENGALI, SUBLANG_DEFAULT', |
| 199 'fa' : 'LANG_PERSIAN, SUBLANG_DEFAULT', |
| 200 'gu' : 'LANG_GUJARATI, SUBLANG_DEFAULT', |
| 201 'kn' : 'LANG_KANNADA, SUBLANG_DEFAULT', |
| 202 'ms' : 'LANG_MALAY, SUBLANG_DEFAULT', |
| 203 'ml' : 'LANG_MALAYALAM, SUBLANG_DEFAULT', |
| 204 'mr' : 'LANG_MARATHI, SUBLANG_DEFAULT', |
| 205 'or' : 'LANG_ORIYA, SUBLANG_DEFAULT', |
| 206 'ta' : 'LANG_TAMIL, SUBLANG_DEFAULT', |
| 207 'te' : 'LANG_TELUGU, SUBLANG_DEFAULT', |
| 208 'am' : 'LANG_AMHARIC, SUBLANG_DEFAULT', |
| 209 'sw' : 'LANG_SWAHILI, SUBLANG_DEFAULT', |
| 210 'af' : 'LANG_AFRIKAANS, SUBLANG_DEFAULT', |
| 211 'eu' : 'LANG_BASQUE, SUBLANG_DEFAULT', |
| 212 'fr-CA' : 'LANG_FRENCH, SUBLANG_FRENCH_CANADIAN', |
| 213 'gl' : 'LANG_GALICIAN, SUBLANG_DEFAULT', |
| 214 'zu' : 'LANG_ZULU, SUBLANG_DEFAULT', |
| 215 'fake-bidi' : 'LANG_HEBREW, SUBLANG_DEFAULT', |
148 } | 216 } |
149 | 217 |
150 def GetLangCharsetPair(language) : | 218 def GetLangCharsetPair(language): |
151 if _LANGUAGE_CHARSET_PAIR.has_key(language) : | 219 if _LANGUAGE_CHARSET_PAIR.has_key(language): |
152 return _LANGUAGE_CHARSET_PAIR[language] | 220 return _LANGUAGE_CHARSET_PAIR[language] |
153 else : | 221 else: |
154 print 'Warning:GetLangCharsetPair() found undefined language %s' %(language) | 222 print 'Warning:GetLangCharsetPair() found undefined language %s' %(language) |
155 return '' | 223 return '' |
156 | 224 |
157 def GetLangDirectivePair(language) : | 225 def GetLangDirectivePair(language): |
158 if _LANGUAGE_DIRECTIVE_PAIR.has_key(language) : | 226 if _LANGUAGE_DIRECTIVE_PAIR.has_key(language): |
159 return _LANGUAGE_DIRECTIVE_PAIR[language] | 227 return _LANGUAGE_DIRECTIVE_PAIR[language] |
160 else : | 228 else: |
161 print 'Warning:GetLangDirectivePair() found undefined language %s' % (langua
ge) | 229 print 'Warning:GetLangDirectivePair() found undefined language %s' % (langua
ge) |
162 return 'unknown language: see tools/grit/format/rc.py' | 230 return 'unknown language: see tools/grit/format/rc.py' |
163 | 231 |
164 def GetLangIdHex(language) : | 232 def GetLangIdHex(language): |
165 if _LANGUAGE_CHARSET_PAIR.has_key(language) : | 233 if _LANGUAGE_CHARSET_PAIR.has_key(language): |
166 langcharset = _LANGUAGE_CHARSET_PAIR[language] | 234 langcharset = _LANGUAGE_CHARSET_PAIR[language] |
167 lang_id = '0x' + langcharset[0:4] | 235 lang_id = '0x' + langcharset[0:4] |
168 return lang_id | 236 return lang_id |
169 else : | 237 else: |
170 print 'Warning:GetLangIdHex() found undefined language %s' %(language) | 238 print 'Warning:GetLangIdHex() found undefined language %s' %(language) |
171 return '' | 239 return '' |
172 | 240 |
173 | 241 |
174 def GetCharsetIdDecimal(language) : | 242 def GetCharsetIdDecimal(language): |
175 if _LANGUAGE_CHARSET_PAIR.has_key(language) : | 243 if _LANGUAGE_CHARSET_PAIR.has_key(language): |
176 langcharset = _LANGUAGE_CHARSET_PAIR[language] | 244 langcharset = _LANGUAGE_CHARSET_PAIR[language] |
177 charset_decimal = int(langcharset[4:], 16) | 245 charset_decimal = int(langcharset[4:], 16) |
178 return str(charset_decimal) | 246 return str(charset_decimal) |
179 else : | 247 else: |
180 print 'Warning:GetCharsetIdDecimal() found undefined language %s' %(language
) | 248 print 'Warning:GetCharsetIdDecimal() found undefined language %s' %(language
) |
181 return '' | 249 return '' |
182 | 250 |
183 | 251 |
184 def GetUnifiedLangCode(language) : | 252 def GetUnifiedLangCode(language) : |
185 r = re.compile('([a-z]{1,2})_([a-z]{1,2})') | 253 r = re.compile('([a-z]{1,2})_([a-z]{1,2})') |
186 if r.match(language) : | 254 if r.match(language) : |
187 underscore = language.find('_') | 255 underscore = language.find('_') |
188 return language[0:underscore] + '-' + language[underscore + 1:].upper() | 256 return language[0:underscore] + '-' + language[underscore + 1:].upper() |
189 else : | 257 else : |
190 return language | 258 return language |
191 | 259 |
192 | 260 |
| 261 def RcSubstitutions(substituter, lang): |
| 262 '''Add language-based substitutions for Rc files to the substitutor.''' |
| 263 unified_lang_code = GetUnifiedLangCode(lang) |
| 264 substituter.AddSubstitutions({ |
| 265 'GRITVERLANGCHARSETHEX': GetLangCharsetPair(unified_lang_code), |
| 266 'GRITVERLANGID': GetLangIdHex(unified_lang_code), |
| 267 'GRITVERCHARSETID': GetCharsetIdDecimal(unified_lang_code)}) |
| 268 |
| 269 |
193 class TopLevel(interface.ItemFormatter): | 270 class TopLevel(interface.ItemFormatter): |
194 '''Writes out the required preamble for RC files.''' | 271 '''Writes out the required preamble for RC files.''' |
195 def Format(self, item, lang='en', begin_item=True, output_dir='.'): | 272 def Format(self, item, lang='en', output_dir='.'): |
196 assert isinstance(lang, types.StringTypes) | 273 assert isinstance(lang, types.StringTypes) |
197 if not begin_item: | 274 assert isinstance(item, misc.GritNode) |
198 return '' | 275 # Find the location of the resource header file, so that we can include |
199 else: | 276 # it. |
200 # Find the location of the resource header file, so that we can include | 277 resource_header = 'resource.h' # fall back to this |
201 # it. | 278 language_directive = '' |
202 resource_header = 'resource.h' # fall back to this | 279 for output in item.GetRoot().GetOutputFiles(): |
203 language_directive = '' | 280 if output.attrs['type'] == 'rc_header': |
204 for output in item.GetRoot().GetOutputFiles(): | 281 resource_header = os.path.abspath(output.GetOutputFilename()) |
205 if output.attrs['type'] == 'rc_header': | 282 resource_header = util.MakeRelativePath(output_dir, resource_header) |
206 resource_header = os.path.abspath(output.GetOutputFilename()) | 283 if output.attrs['lang'] != lang: |
207 resource_header = util.MakeRelativePath(output_dir, resource_header) | 284 continue |
208 if output.attrs['lang'] != lang: | 285 if output.attrs['language_section'] == '': |
209 continue | 286 # If no language_section is requested, no directive is added |
210 if output.attrs['language_section'] == '': | 287 # (Used when the generated rc will be included from another rc |
211 # If no language_section is requested, no directive is added | 288 # file that will have the appropriate language directive) |
212 # (Used when the generated rc will be included from another rc | 289 language_directive = '' |
213 # file that will have the appropriate language directive) | 290 elif output.attrs['language_section'] == 'neutral': |
214 language_directive = '' | 291 # If a neutral language section is requested (default), add a |
215 elif output.attrs['language_section'] == 'neutral': | 292 # neutral language directive |
216 # If a neutral language section is requested (default), add a | 293 language_directive = 'LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL' |
217 # neutral language directive | 294 elif output.attrs['language_section'] == 'lang': |
218 language_directive = 'LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL' | 295 language_directive = 'LANGUAGE %s' % GetLangDirectivePair(lang) |
219 elif output.attrs['language_section'] == 'lang': | 296 resource_header = resource_header.replace('\\', '\\\\') |
220 language_directive = 'LANGUAGE %s' % GetLangDirectivePair(lang) | 297 return '''// Copyright (c) Google Inc. %d |
221 resource_header = resource_header.replace('\\', '\\\\') | |
222 return '''// Copyright (c) Google Inc. %d | |
223 // All rights reserved. | 298 // All rights reserved. |
224 // This file is automatically generated by GRIT. Do not edit. | 299 // This file is automatically generated by GRIT. Do not edit. |
225 | 300 |
226 #include "%s" | 301 #include "%s" |
227 #include <winresrc.h> | 302 #include <winresrc.h> |
228 #ifdef IDC_STATIC | 303 #ifdef IDC_STATIC |
229 #undef IDC_STATIC | 304 #undef IDC_STATIC |
230 #endif | 305 #endif |
231 #define IDC_STATIC (-1) | 306 #define IDC_STATIC (-1) |
232 | 307 |
233 %s | 308 %s |
234 | 309 |
235 | 310 |
236 ''' % (util.GetCurrentYear(), resource_header, language_directive) | 311 ''' % (util.GetCurrentYear(), resource_header, language_directive) |
237 # end Format() function | 312 # end Format() function |
238 | 313 |
239 | 314 |
240 | 315 |
241 class StringTable(interface.ItemFormatter): | 316 class StringTable(interface.ItemFormatter): |
242 '''Surrounds a collection of string messages with the required begin and | 317 '''Surrounds a collection of string messages with the required begin and |
243 end blocks to declare a string table.''' | 318 end blocks to declare a string table.''' |
244 | 319 |
245 def Format(self, item, lang='en', begin_item=True, output_dir='.'): | 320 def Format(self, item, lang='en', output_dir='.'): |
246 assert isinstance(lang, types.StringTypes) | 321 assert isinstance(lang, types.StringTypes) |
247 if begin_item: | 322 return 'STRINGTABLE\nBEGIN\n' |
248 return 'STRINGTABLE\nBEGIN\n' | 323 |
249 else: | 324 def FormatEnd(self, item, lang='en', output_dir='.'): |
250 return 'END\n\n' | 325 assert isinstance(lang, types.StringTypes) |
| 326 return 'END\n\n' |
251 | 327 |
252 | 328 |
253 class Message(interface.ItemFormatter): | 329 class Message(interface.ItemFormatter): |
254 '''Writes out a single message to a string table.''' | 330 '''Writes out a single message to a string table.''' |
255 | 331 |
256 def Format(self, item, lang='en', begin_item=True, output_dir='.'): | 332 def Format(self, item, lang='en', output_dir='.'): |
257 from grit.node import message | 333 from grit.node import message |
258 if not begin_item: | |
259 return '' | |
260 | |
261 assert isinstance(lang, types.StringTypes) | 334 assert isinstance(lang, types.StringTypes) |
262 assert isinstance(item, message.MessageNode) | 335 assert isinstance(item, message.MessageNode) |
263 | 336 |
264 message = item.ws_at_start + item.Translate(lang) + item.ws_at_end | 337 message = item.ws_at_start + item.Translate(lang) + item.ws_at_end |
265 # Escape quotation marks (RC format uses doubling-up | 338 # Escape quotation marks (RC format uses doubling-up |
266 message = message.replace('"', '""') | 339 message = message.replace('"', '""') |
267 # Replace linebreaks with a \n escape | 340 # Replace linebreaks with a \n escape |
268 message = util.LINEBREAKS.sub(r'\\n', message) | 341 message = util.LINEBREAKS.sub(r'\\n', message) |
| 342 substituter = getattr(item.GetRoot(), 'substituter', None) |
| 343 if substituter: |
| 344 message = substituter.Substitute(message) |
269 | 345 |
270 name_attr = item.GetTextualIds()[0] | 346 name_attr = item.GetTextualIds()[0] |
271 | 347 |
272 return ' %-15s "%s"\n' % (name_attr, message) | 348 return ' %-15s "%s"\n' % (name_attr, message) |
273 | 349 |
274 | 350 |
275 class RcSection(interface.ItemFormatter): | 351 class RcSection(interface.ItemFormatter): |
276 '''Writes out an .rc file section.''' | 352 '''Writes out an .rc file section.''' |
277 | 353 |
278 def Format(self, item, lang='en', begin_item=True, output_dir='.'): | 354 def Format(self, item, lang='en', output_dir='.'): |
279 if not begin_item: | |
280 return '' | |
281 | |
282 assert isinstance(lang, types.StringTypes) | 355 assert isinstance(lang, types.StringTypes) |
283 from grit.node import structure | 356 from grit.node import structure |
284 assert isinstance(item, structure.StructureNode) | 357 assert isinstance(item, structure.StructureNode) |
285 | 358 |
286 if item.IsExcludedFromRc(): | 359 if item.IsExcludedFromRc(): |
287 return '' | 360 return '' |
288 else: | 361 else: |
289 text = item.gatherer.Translate( | 362 text = item.gatherer.Translate( |
290 lang, skeleton_gatherer=item.GetSkeletonGatherer(), | 363 lang, skeleton_gatherer=item.GetSkeletonGatherer(), |
291 pseudo_if_not_available=item.PseudoIsAllowed(), | 364 pseudo_if_not_available=item.PseudoIsAllowed(), |
292 fallback_to_english=item.ShouldFallbackToEnglish()) + '\n\n' | 365 fallback_to_english=item.ShouldFallbackToEnglish()) + '\n\n' |
293 | 366 |
294 # Replace the language expand_variables in version rc info. | 367 # Replace the language expand_variables in version rc info. |
295 unified_lang_code = GetUnifiedLangCode(lang) | 368 if item.ExpandVariables(): |
296 if text.find('[GRITVERLANGCHARSETHEX]') != -1: | 369 substituter = getattr(item.GetRoot(), 'substituter', None) |
297 text = text.replace('[GRITVERLANGCHARSETHEX]', | 370 if substituter: |
298 GetLangCharsetPair(unified_lang_code)) | 371 text = substituter.Substitute(text) |
299 if text.find('[GRITVERLANGID]') != -1: | |
300 text = text.replace('[GRITVERLANGID]', GetLangIdHex(unified_lang_code)) | |
301 if text.find('[GRITVERCHARSETID]') != -1: | |
302 text = text.replace('[GRITVERCHARSETID]', | |
303 GetCharsetIdDecimal(unified_lang_code)) | |
304 | 372 |
305 return text | 373 return text |
306 | 374 |
307 | 375 |
308 class RcInclude(interface.ItemFormatter): | 376 class RcInclude(interface.ItemFormatter): |
309 '''Writes out an item that is included in an .rc file (e.g. an ICON)''' | 377 '''Writes out an item that is included in an .rc file (e.g. an ICON)''' |
310 | 378 |
311 def __init__(self, type, filenameWithoutPath = 0, relative_path = 0, | 379 def __init__(self, type, filenameWithoutPath = 0, relative_path = 0, |
312 flatten_html = 0): | 380 flatten_html = 0): |
313 '''Indicates to the instance what the type of the resource include is, | 381 '''Indicates to the instance what the type of the resource include is, |
314 e.g. 'ICON' or 'HTML'. Case must be correct, i.e. if the type is all-caps | 382 e.g. 'ICON' or 'HTML'. Case must be correct, i.e. if the type is all-caps |
315 the parameter should be all-caps. | 383 the parameter should be all-caps. |
316 | 384 |
317 Args: | 385 Args: |
318 type: 'ICON' | 386 type: 'ICON' |
319 ''' | 387 ''' |
320 self.type_ = type | 388 self.type_ = type |
321 self.filenameWithoutPath = filenameWithoutPath | 389 self.filenameWithoutPath = filenameWithoutPath |
322 self.relative_path_ = relative_path | 390 self.relative_path_ = relative_path |
323 self.flatten_html = flatten_html | 391 self.flatten_html = flatten_html |
324 | 392 |
325 def Format(self, item, lang='en', begin_item=True, output_dir='.'): | 393 def Format(self, item, lang='en', output_dir='.'): |
326 if not begin_item: | |
327 return '' | |
328 | |
329 assert isinstance(lang, types.StringTypes) | 394 assert isinstance(lang, types.StringTypes) |
330 from grit.node import structure | 395 from grit.node import structure |
331 from grit.node import include | 396 from grit.node import include |
332 assert isinstance(item, (structure.StructureNode, include.IncludeNode)) | 397 assert isinstance(item, (structure.StructureNode, include.IncludeNode)) |
333 assert (isinstance(item, include.IncludeNode) or | 398 assert (isinstance(item, include.IncludeNode) or |
334 item.attrs['type'] in ['tr_html', 'admin_template', 'txt', 'muppet']
) | 399 item.attrs['type'] in ['tr_html', 'admin_template', 'txt', 'muppet']
) |
335 | 400 |
336 # By default, we use relative pathnames to included resources so that | 401 # By default, we use relative pathnames to included resources so that |
337 # sharing the resulting .rc files is possible. | 402 # sharing the resulting .rc files is possible. |
338 # | 403 # |
339 # The FileForLanguage() Function has the side effect of generating the file | 404 # The FileForLanguage() Function has the side effect of generating the file |
340 # if needed (e.g. if it is an HTML file include). | 405 # if needed (e.g. if it is an HTML file include). |
341 filename = os.path.abspath(item.FileForLanguage(lang, output_dir)) | 406 filename = os.path.abspath(item.FileForLanguage(lang, output_dir)) |
342 if self.flatten_html: | 407 if self.flatten_html: |
343 filename = item.Flatten(output_dir) | 408 filename = item.Flatten(output_dir) |
344 elif self.filenameWithoutPath: | 409 elif self.filenameWithoutPath: |
345 filename = os.path.basename(filename) | 410 filename = os.path.basename(filename) |
346 elif self.relative_path_: | 411 elif self.relative_path_: |
347 filename = util.MakeRelativePath(output_dir, filename) | 412 filename = util.MakeRelativePath(output_dir, filename) |
348 | 413 |
349 filename = filename.replace('\\', '\\\\') # escape for the RC format | 414 filename = filename.replace('\\', '\\\\') # escape for the RC format |
350 | 415 |
351 if isinstance(item, structure.StructureNode) and item.IsExcludedFromRc(): | 416 if isinstance(item, structure.StructureNode) and item.IsExcludedFromRc(): |
352 return '' | 417 return '' |
353 else: | 418 else: |
354 return '%-18s %-18s "%s"\n' % (item.attrs['name'], self.type_, filename) | 419 return '%-18s %-18s "%s"\n' % (item.attrs['name'], self.type_, filename) |
355 | 420 |
OLD | NEW |