Django

Code

Changeset 760

Show
Ignore:
Timestamp:
10/01/05 11:24:21 (3 years ago)
Author:
hugo
Message:

i18n: changed resolve_variable and resolve_variable_with_filters to allways
accept string constants and to accept i18n string constants with _(), too.
That way the i18n tag isn't needed in simple cases.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/i18n/django/core/template.py

    r644 r760  
    255255        self.current_filter_name = None 
    256256        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() 
    259263        if not self.var: 
    260264            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('_('))
    262266            raise TemplateSyntaxError, "Variables and attributes may not begin with underscores: '%s'" % self.var 
    263267        # Have we reached the end? 
     
    269273        self.read_filters() 
    270274 
     275    def peek_char(self): 
     276        try: 
     277            return self.s[self.i+1] 
     278        except IndexError: 
     279            return None 
     280 
    271281    def next_char(self): 
    272282        self.i = self.i + 1 
     
    275285        except IndexError: 
    276286            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 
    277320 
    278321    def read_alphanumeric_token(self): 
     
    373416    (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.') 
    374417    """ 
    375     if path[0] in ('"', "'") and path[0] == path[-1]: 
     418    if path[0] in ('"', "'") and path[0] == path[-1]: 
    376419        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]) 
    377422    else: 
    378423        current = context 
  • django/branches/i18n/docs/translation.txt

    r756 r760  
    102102To translate a variable value, you can just do {% i18n _(variable) %}. This 
    103103can even include filters like {% i18n _(variable|lower} %}. 
     104 
     105There is additional support for i18n string constants for other situations 
     106as well. All template tags that do variable resolving (with or without filters) 
     107will accept string constants, too. Those string constants can now be i18n 
     108strings like this:: 
     109 
     110   <html> 
     111   <title>{{ _('This is the title') }}</title> 
     112   <body> 
     113   <p>{{ _('Hello World!') }}</p> 
     114   </body> 
     115   </html> 
     116 
     117This is much shorter, but won't allow you to use gettext_noop or ngettext. 
    104118 
    105119How the Language is Discovered 
  • django/branches/i18n/tests/othertests/templates.py

    r757 r760  
    242242    # simple non-translation (only marking) of a string to german 
    243243    '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"), 
    244247} 
    245248