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 '''The <structure> element. | 6 '''The <structure> element. |
7 ''' | 7 ''' |
8 | 8 |
9 import os | 9 import os |
10 | 10 |
11 from grit.node import base | 11 from grit.node import base |
12 from grit.node import variant | 12 from grit.node import variant |
13 | 13 |
14 from grit import constants | 14 from grit import constants |
15 from grit import exception | 15 from grit import exception |
16 from grit import util | 16 from grit import util |
17 | 17 |
18 import grit.gather.admin_template | 18 import grit.gather.admin_template |
19 import grit.gather.igoogle_strings | 19 import grit.gather.igoogle_strings |
20 import grit.gather.muppet_strings | 20 import grit.gather.muppet_strings |
21 import grit.gather.policy_json | 21 import grit.gather.policy_json |
22 import grit.gather.rc | 22 import grit.gather.rc |
23 import grit.gather.tr_html | 23 import grit.gather.tr_html |
24 import grit.gather.txt | 24 import grit.gather.txt |
25 | 25 |
26 import grit.format.rc | 26 import grit.format.rc |
27 import grit.format.rc_header | 27 import grit.format.rc_header |
28 | 28 |
29 # RTL languages | |
30 # TODO(jennyz): remove this fixed set of RTL language array | |
31 # when generic expand_variable code is added by grit team. | |
32 _RTL_LANGS = [ | |
33 'ar', | |
34 'iw', | |
35 'ur', | |
36 ] | |
37 | |
38 # Type of the gatherer to use for each type attribute | 29 # Type of the gatherer to use for each type attribute |
39 _GATHERERS = { | 30 _GATHERERS = { |
40 'accelerators' : grit.gather.rc.Accelerators, | 31 'accelerators' : grit.gather.rc.Accelerators, |
41 'admin_template' : grit.gather.admin_template.AdmGatherer, | 32 'admin_template' : grit.gather.admin_template.AdmGatherer, |
42 'dialog' : grit.gather.rc.Dialog, | 33 'dialog' : grit.gather.rc.Dialog, |
43 'igoogle' : grit.gather.igoogle_strings.IgoogleStrings, | 34 'igoogle' : grit.gather.igoogle_strings.IgoogleStrings, |
44 'menu' : grit.gather.rc.Menu, | 35 'menu' : grit.gather.rc.Menu, |
45 'muppet' : grit.gather.muppet_strings.MuppetStrings, | 36 'muppet' : grit.gather.muppet_strings.MuppetStrings, |
46 'rcdata' : grit.gather.rc.RCData, | 37 'rcdata' : grit.gather.rc.RCData, |
47 'tr_html' : grit.gather.tr_html.TrHtml, | 38 'tr_html' : grit.gather.tr_html.TrHtml, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 return self.skeletons[expr] | 169 return self.skeletons[expr] |
179 return None | 170 return None |
180 | 171 |
181 def GetFilePath(self): | 172 def GetFilePath(self): |
182 return self.ToRealPath(self.attrs['file']) | 173 return self.ToRealPath(self.attrs['file']) |
183 | 174 |
184 def HasFileForLanguage(self): | 175 def HasFileForLanguage(self): |
185 return self.attrs['type'] in [ | 176 return self.attrs['type'] in [ |
186 'tr_html', 'admin_template', 'txt', 'muppet', 'igoogle'] | 177 'tr_html', 'admin_template', 'txt', 'muppet', 'igoogle'] |
187 | 178 |
| 179 def ExpandVariables(self): |
| 180 '''Variable expansion on structures is controlled by an XML attribute. |
| 181 |
| 182 However, old files assume that expansion is always on for Rc files. |
| 183 |
| 184 Returns: |
| 185 A boolean. |
| 186 ''' |
| 187 attrs = self.GetRoot().attrs |
| 188 if 'grit_version' in attrs and attrs['grit_version'] > 1: |
| 189 return self.attrs['expand_variables'] == 'true' |
| 190 else: |
| 191 return (self.attrs['expand_variables'] == 'true' or |
| 192 self.attrs['file'].lower().endswith('.rc')) |
| 193 |
188 def FileForLanguage(self, lang, output_dir, create_file=True, | 194 def FileForLanguage(self, lang, output_dir, create_file=True, |
189 return_if_not_generated=True): | 195 return_if_not_generated=True): |
190 '''Returns the filename of the file associated with this structure, | 196 '''Returns the filename of the file associated with this structure, |
191 for the specified language. | 197 for the specified language. |
192 | 198 |
193 Args: | 199 Args: |
194 lang: 'fr' | 200 lang: 'fr' |
195 output_dir: 'c:\temp' | 201 output_dir: 'c:\temp' |
196 create_file: True | 202 create_file: True |
197 ''' | 203 ''' |
(...skipping 20 matching lines...) Expand all Loading... |
218 if create_file: | 224 if create_file: |
219 text = self.gatherer.Translate( | 225 text = self.gatherer.Translate( |
220 lang, | 226 lang, |
221 pseudo_if_not_available=self.PseudoIsAllowed(), | 227 pseudo_if_not_available=self.PseudoIsAllowed(), |
222 fallback_to_english=self.ShouldFallbackToEnglish(), | 228 fallback_to_english=self.ShouldFallbackToEnglish(), |
223 skeleton_gatherer=self.GetSkeletonGatherer()) | 229 skeleton_gatherer=self.GetSkeletonGatherer()) |
224 | 230 |
225 file_object = util.WrapOutputStream(file(filename, 'wb'), | 231 file_object = util.WrapOutputStream(file(filename, 'wb'), |
226 self._GetOutputEncoding()) | 232 self._GetOutputEncoding()) |
227 file_contents = util.FixLineEnd(text, self.GetLineEnd()) | 233 file_contents = util.FixLineEnd(text, self.GetLineEnd()) |
228 if self.attrs['expand_variables'] == 'true': | 234 if self.ExpandVariables(): |
229 file_contents = file_contents.replace('[GRITLANGCODE]', lang) | 235 # Note that we reapply substitution a second time here. |
230 # TODO(jennyz): remove this hard coded logic for expanding | 236 # This is because a) we need to look inside placeholders |
231 # [GRITDIR] variable for RTL languages when the generic | 237 # b) the substitution values are language-dependent |
232 # expand_variable code is added by grit team. | 238 file_contents = self.GetRoot().substituter.Substitute(file_contents) |
233 if lang in _RTL_LANGS : | |
234 file_contents = file_contents.replace('[GRITDIR]', 'dir="RTL"') | |
235 else : | |
236 file_contents = file_contents.replace('[GRITDIR]', 'dir="LTR"') | |
237 if self._ShouldAddBom(): | 239 if self._ShouldAddBom(): |
238 file_object.write(constants.BOM) | 240 file_object.write(constants.BOM) |
239 file_object.write(file_contents) | 241 file_object.write(file_contents) |
240 file_object.close() | 242 file_object.close() |
241 | 243 |
242 if self.attrs['run_command']: | 244 if self.attrs['run_command']: |
243 # Run arbitrary commands after translation is complete so that it | 245 # Run arbitrary commands after translation is complete so that it |
244 # doesn't interfere with what's in translation console. | 246 # doesn't interfere with what's in translation console. |
245 command = self.attrs['run_command'] % {'filename': filename} | 247 command = self.attrs['run_command'] % {'filename': filename} |
246 result = os.system(command) | 248 result = os.system(command) |
(...skipping 10 matching lines...) Expand all Loading... |
257 if enc.endswith('-sig'): | 259 if enc.endswith('-sig'): |
258 return enc[0:len(enc) - len('-sig')] | 260 return enc[0:len(enc) - len('-sig')] |
259 else: | 261 else: |
260 return enc | 262 return enc |
261 | 263 |
262 def _ShouldAddBom(self): | 264 def _ShouldAddBom(self): |
263 '''Returns true if output files should have the Unicode BOM prepended. | 265 '''Returns true if output files should have the Unicode BOM prepended. |
264 ''' | 266 ''' |
265 return self.attrs['output_encoding'].endswith('-sig') | 267 return self.attrs['output_encoding'].endswith('-sig') |
266 | 268 |
267 # static method | 269 @staticmethod |
268 def Construct(parent, name, type, file, encoding='cp1252'): | 270 def Construct(parent, name, type, file, encoding='cp1252'): |
269 '''Creates a new node which is a child of 'parent', with attributes set | 271 '''Creates a new node which is a child of 'parent', with attributes set |
270 by parameters of the same name. | 272 by parameters of the same name. |
271 ''' | 273 ''' |
272 node = StructureNode() | 274 node = StructureNode() |
273 node.StartParsing('structure', parent) | 275 node.StartParsing('structure', parent) |
274 node.HandleAttribute('name', name) | 276 node.HandleAttribute('name', name) |
275 node.HandleAttribute('type', type) | 277 node.HandleAttribute('type', type) |
276 node.HandleAttribute('file', file) | 278 node.HandleAttribute('file', file) |
277 node.HandleAttribute('encoding', encoding) | 279 node.HandleAttribute('encoding', encoding) |
278 node.EndParsing() | 280 node.EndParsing() |
279 return node | 281 return node |
280 Construct = staticmethod(Construct) | |
281 | 282 |
| 283 def SubstituteMessages(self, substituter): |
| 284 '''Propagates substitution to gatherer. |
| 285 |
| 286 Args: |
| 287 substituter: a grit.util.Substituter object. |
| 288 ''' |
| 289 assert self.gatherer |
| 290 if self.ExpandVariables(): |
| 291 self.gatherer.SubstituteMessages(substituter) |
| 292 |
OLD | NEW |