Changeset 760
- Timestamp:
- 10/01/05 11:24:21 (3 years ago)
- Files:
-
- django/branches/i18n/django/core/template.py (modified) (4 diffs)
- django/branches/i18n/docs/translation.txt (modified) (1 diff)
- django/branches/i18n/tests/othertests/templates.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/i18n/django/core/template.py
r644 r760 255 255 self.current_filter_name = None 256 256 self.current_filter_arg = None 257 # First read the variable part 258 self.var = self.read_alphanumeric_token() 257 # First read the variable part - decide on wether we need 258 # to parse a string or a variable by peeking into the stream 259 if self.peek_char() in ('_', '"', "'"): 260 self.var = self.read_constant_string_token() 261 else: 262 self.var = self.read_alphanumeric_token() 259 263 if not self.var: 260 264 raise TemplateSyntaxError, "Could not read variable name: '%s'" % self.s 261 if self.var.find(VARIABLE_ATTRIBUTE_SEPARATOR + '_') > -1 or self.var[0] == '_':265 if self.var.find(VARIABLE_ATTRIBUTE_SEPARATOR + '_') > -1 or (self.var[0] == '_' and not self.var.startswith('_(')): 262 266 raise TemplateSyntaxError, "Variables and attributes may not begin with underscores: '%s'" % self.var 263 267 # Have we reached the end? … … 269 273 self.read_filters() 270 274 275 def peek_char(self): 276 try: 277 return self.s[self.i+1] 278 except IndexError: 279 return None 280 271 281 def next_char(self): 272 282 self.i = self.i + 1 … … 275 285 except IndexError: 276 286 self.current = None 287 288 def read_constant_string_token(self): 289 """Read a constant string that must be delimited by either " 290 or ' characters. The string is returned with it's delimiters 291 and a possible i18n tag.""" 292 val = '' 293 i18n = False 294 qchar = None 295 self.next_char() 296 if self.current == '_': 297 self.next_char() 298 if self.current != '(': 299 raise TemplateSyntaxError, "Bad character (expecting '(') '%s'" % self.current 300 i18n = True 301 val = '_(' 302 self.next_char() 303 if not self.current in ('"', "'"): 304 raise TemplateSyntaxError, "Bad character (expecting '\"' or ''') '%s'" % self.current 305 qchar = self.current 306 val += qchar 307 while 1: 308 self.next_char() 309 if self.current == qchar: 310 break 311 val += self.current 312 val += self.current 313 if i18n: 314 self.next_char() 315 if self.current != ')': 316 raise TemplateSyntaxError, "Bad character (expecting ')') '%s'" % self.current 317 val += self.current 318 self.next_char() 319 return val 277 320 278 321 def read_alphanumeric_token(self): … … 373 416 (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.') 374 417 """ 375 if path[0] in ('"', "'") and path[0] == path[-1]:418 if path[0] in ('"', "'") and path[0] == path[-1]: 376 419 current = path[1:-1] 420 elif path.startswith('_(') and path.endswith(')') and path[2] in ("'", '"') and path[-2] == path[2]: 421 current = _(path[3:-2]) 377 422 else: 378 423 current = context django/branches/i18n/docs/translation.txt
r756 r760 102 102 To translate a variable value, you can just do {% i18n _(variable) %}. This 103 103 can even include filters like {% i18n _(variable|lower} %}. 104 105 There is additional support for i18n string constants for other situations 106 as well. All template tags that do variable resolving (with or without filters) 107 will accept string constants, too. Those string constants can now be i18n 108 strings like this:: 109 110 <html> 111 <title>{{ _('This is the title') }}</title> 112 <body> 113 <p>{{ _('Hello World!') }}</p> 114 </body> 115 </html> 116 117 This is much shorter, but won't allow you to use gettext_noop or ngettext. 104 118 105 119 How the Language is Discovered django/branches/i18n/tests/othertests/templates.py
r757 r760 242 242 # simple non-translation (only marking) of a string to german 243 243 'i18n10': ('{% i18n gettext_noop("Page not found") %}', {'LANGUAGE_CODE': 'de'}, "Page not found"), 244 245 # translation of string without i18n tag 246 'i18n11': ('{{ _("blah") }}', {}, "blah"), 244 247 } 245 248
