Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: grit/node/message.py

Issue 9965022: Allow substitution of messages as variables in other messages. (Closed) Base URL: https://grit-i18n.googlecode.com/svn/trunk
Patch Set: Fix unit tests for policy writers. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « grit/node/io_unittest.py ('k') | grit/node/misc.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 '''Handling of the <message> element. 6 '''Handling of the <message> element.
7 ''' 7 '''
8 8
9 import re 9 import re
10 import types 10 import types
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 # A list of "shortcut groups" this message is in. We check to make sure 49 # A list of "shortcut groups" this message is in. We check to make sure
50 # that shortcut keys (e.g. &J) within each shortcut group are unique. 50 # that shortcut keys (e.g. &J) within each shortcut group are unique.
51 self.shortcut_groups_ = [] 51 self.shortcut_groups_ = []
52 52
53 def _IsValidChild(self, child): 53 def _IsValidChild(self, child):
54 return isinstance(child, (PhNode)) 54 return isinstance(child, (PhNode))
55 55
56 def _IsValidAttribute(self, name, value): 56 def _IsValidAttribute(self, name, value):
57 if name not in ['name', 'offset', 'translateable', 'desc', 'meaning', 57 if name not in ['name', 'offset', 'translateable', 'desc', 'meaning',
58 'internal_comment', 'shortcut_groups', 'custom_type', 58 'internal_comment', 'shortcut_groups', 'custom_type',
59 'validation_expr', 'use_name_for_id']: 59 'validation_expr', 'use_name_for_id', 'sub_variable']:
60 return False 60 return False
61 if name == 'translateable' and value not in ['true', 'false']: 61 if (name in ('translateable', 'sub_variable') and
62 value not in ['true', 'false']):
62 return False 63 return False
63 return True 64 return True
64 65
65 def MandatoryAttributes(self): 66 def MandatoryAttributes(self):
66 return ['name|offset'] 67 return ['name|offset']
67 68
68 def DefaultAttributes(self): 69 def DefaultAttributes(self):
69 return { 70 return {
71 'custom_type' : '',
72 'desc' : '',
73 'internal_comment' : '',
74 'meaning' : '',
75 'shortcut_groups' : '',
76 'sub_variable' : 'false',
70 'translateable' : 'true', 77 'translateable' : 'true',
71 'desc' : '', 78 'use_name_for_id' : 'false',
72 'meaning' : '',
73 'internal_comment' : '',
74 'shortcut_groups' : '',
75 'custom_type' : '',
76 'validation_expr' : '', 79 'validation_expr' : '',
77 'use_name_for_id' : 'false',
78 } 80 }
79 81
80 def GetTextualIds(self): 82 def GetTextualIds(self):
81 ''' 83 '''
82 Returns the concatenation of the parent's node first_id and 84 Returns the concatenation of the parent's node first_id and
83 this node's offset if it has one, otherwise just call the 85 this node's offset if it has one, otherwise just call the
84 superclass' implementation 86 superclass' implementation
85 ''' 87 '''
86 if 'offset' in self.attrs: 88 if 'offset' in self.attrs:
87 # we search for the first grouping node in the parents' list 89 # we search for the first grouping node in the parents' list
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 description_or_id = 'ID: %s' % self.attrs['name'] 153 description_or_id = 'ID: %s' % self.attrs['name']
152 154
153 assigned_id = None 155 assigned_id = None
154 if (self.attrs['use_name_for_id'] == 'true' and 156 if (self.attrs['use_name_for_id'] == 'true' and
155 self.SatisfiesOutputCondition()): 157 self.SatisfiesOutputCondition()):
156 assigned_id = self.attrs['name'] 158 assigned_id = self.attrs['name']
157 message = tclib.Message(text=text, placeholders=placeholders, 159 message = tclib.Message(text=text, placeholders=placeholders,
158 description=description_or_id, 160 description=description_or_id,
159 meaning=self.attrs['meaning'], 161 meaning=self.attrs['meaning'],
160 assigned_id=assigned_id) 162 assigned_id=assigned_id)
163 self.InstallMessage(message)
164
165 def InstallMessage(self, message):
166 '''Sets this node's clique from a tclib.Message instance.
167
168 Args:
169 message: A tclib.Message.
170 '''
161 self.clique = self.UberClique().MakeClique(message, self.IsTranslateable()) 171 self.clique = self.UberClique().MakeClique(message, self.IsTranslateable())
162 for group in self.shortcut_groups_: 172 for group in self.shortcut_groups_:
163 self.clique.AddToShortcutGroup(group) 173 self.clique.AddToShortcutGroup(group)
164 if self.attrs['custom_type'] != '': 174 if self.attrs['custom_type'] != '':
165 self.clique.SetCustomType(util.NewClassInstance(self.attrs['custom_type'], 175 self.clique.SetCustomType(util.NewClassInstance(self.attrs['custom_type'],
166 clique.CustomType)) 176 clique.CustomType))
167 elif self.attrs['validation_expr'] != '': 177 elif self.attrs['validation_expr'] != '':
168 self.clique.SetCustomType( 178 self.clique.SetCustomType(
169 clique.OneOffCustomType(self.attrs['validation_expr'])) 179 clique.OneOffCustomType(self.attrs['validation_expr']))
170 180
181 def SubstituteMessages(self, substituter):
182 '''Applies substitution to this message.
183
184 Args:
185 substituter: a grit.util.Substituter object.
186 '''
187 message = substituter.SubstituteMessage(self.clique.GetMessage())
188 if message is not self.clique.GetMessage():
189 self.InstallMessage(message)
190
171 def GetCliques(self): 191 def GetCliques(self):
172 if self.clique: 192 if self.clique:
173 return [self.clique] 193 return [self.clique]
174 else: 194 else:
175 return [] 195 return []
176 196
177 def Translate(self, lang): 197 def Translate(self, lang):
178 '''Returns a translated version of this message. 198 '''Returns a translated version of this message.
179 ''' 199 '''
180 assert self.clique 200 assert self.clique
181 msg = self.clique.MessageForLanguage(lang, 201 msg = self.clique.MessageForLanguage(lang,
182 self.PseudoIsAllowed(), 202 self.PseudoIsAllowed(),
183 self.ShouldFallbackToEnglish() 203 self.ShouldFallbackToEnglish()
184 ).GetRealContent() 204 ).GetRealContent()
185 return msg.replace('[GRITLANGCODE]', lang) 205 return msg.replace('[GRITLANGCODE]', lang)
186 206
187 def NameOrOffset(self): 207 def NameOrOffset(self):
188 if 'name' in self.attrs: 208 if 'name' in self.attrs:
189 return self.attrs['name'] 209 return self.attrs['name']
190 else: 210 else:
191 return self.attrs['offset'] 211 return self.attrs['offset']
192 212
213 def ExpandVariables(self):
214 '''We always expand variables on Messages.'''
215 return True
216
193 def GetDataPackPair(self, lang, encoding): 217 def GetDataPackPair(self, lang, encoding):
194 '''Returns a (id, string) pair that represents the string id and the string 218 '''Returns a (id, string) pair that represents the string id and the string
195 in utf8. This is used to generate the data pack data file. 219 in utf8. This is used to generate the data pack data file.
196 ''' 220 '''
197 from grit.format import rc_header 221 from grit.format import rc_header
198 id_map = rc_header.Item.tids_ 222 id_map = rc_header.Item.tids_
199 id = id_map[self.GetTextualIds()[0]] 223 id = id_map[self.GetTextualIds()[0]]
200 224
201 message = self.ws_at_start + self.Translate(lang) + self.ws_at_end 225 message = self.ws_at_start + self.Translate(lang) + self.ws_at_end
202 226
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 def EndParsing(self): 297 def EndParsing(self):
274 super(type(self), self).EndParsing() 298 super(type(self), self).EndParsing()
275 # We only allow a single example for each placeholder 299 # We only allow a single example for each placeholder
276 if len(self.children) > 1: 300 if len(self.children) > 1:
277 raise exception.TooManyExamples() 301 raise exception.TooManyExamples()
278 302
279 303
280 class ExNode(base.ContentNode): 304 class ExNode(base.ContentNode):
281 '''An <ex> element.''' 305 '''An <ex> element.'''
282 pass 306 pass
OLDNEW
« no previous file with comments | « grit/node/io_unittest.py ('k') | grit/node/misc.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698