Ticket #3970: 118n-tags_3970.patch
File 118n-tags_3970.patch, 3.2 KB (added by , 18 years ago) |
---|
-
templatetags/i18n.py
31 31 return '' 32 32 33 33 class TranslateNode(Node): 34 def __init__(self, value, noop): 35 self.value = value 36 self.noop = noop 34 def __init__(self, value, noop, as_var): 35 self.value = value 36 self.noop = noop 37 self.as_var = as_var 37 38 38 39 def render(self, context): 39 40 value = resolve_variable(self.value, context) 40 if self.noop: 41 if not self.noop: 42 value = translation.gettext(value) 43 44 if self.as_var != False: 45 context[self.as_var] = value 46 return '' 47 else: 41 48 return value 42 else:43 return translation.gettext(value)44 49 50 45 51 class BlockTranslateNode(Node): 46 52 def __init__(self, extra_context, singular, plural=None, countervar=None, counter=None): 47 53 self.extra_context = extra_context … … 158 164 This will just try to translate the contents of 159 165 the variable ``variable``. Make sure that the string 160 166 in there is something that is in the .po file. 167 168 Additionally, one can use the 'as' keyword to specify 169 whether or not something should go into a context variable 170 instead. For example you can do:: 171 172 {% trans "this is a string" as translated_text %} 173 {{ translated_text|truncatewords:4 }} 174 175 Or, if you wish to still use noop:: 176 177 {% trans "this is a string" as translated_text noop %} 178 {{ translated_text|truncatewords:4 }} 179 161 180 """ 162 181 class TranslateParser(TokenParser): 163 182 def top(self): 164 183 value = self.value() 165 184 if self.more(): 166 if self.tag() == 'noop': 167 noop = True 185 next_tag = self.tag() 186 187 if next_tag.lower() not in ('noop','as'): 188 raise TemplateSyntaxError, "Invalid parameters for 'trans'" 189 190 if next_tag.lower() == 'as': 191 # we're using the 'as' syntax 192 if not self.more(): 193 raise TemplateSyntaxError, "Invalid parameters for 'trans'" 194 as_var = self.tag() 195 196 if self.more(): 197 if self.tag().lower() == 'noop': 198 noop = True 199 else: 200 raise TemplateSyntaxError, "Invalid parameters for 'trans'" 201 168 202 else: 169 raise TemplateSyntaxError, "only option for 'trans' is 'noop'" 203 # no as keyword, thus we must be using 'noop' 204 as_var = False 205 noop = True 206 170 207 else: 171 noop = False 172 return (value, noop) 173 value, noop = TranslateParser(token.contents).top() 174 return TranslateNode(value, noop) 208 as_var = False 209 noop = False 210 return (value, noop, as_var) 211 value, noop, as_var = TranslateParser(token.contents).top() 212 return TranslateNode(value, noop, as_var) 175 213 176 214 def do_block_translate(parser, token): 177 215 """