Django

Code

Changeset 1066

Show
Ignore:
Timestamp:
11/03/05 11:10:16 (3 years ago)
Author:
hugo
Message:

i18n: fixed documentation and message scanner for constant string translations

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/i18n/django/utils/translation.py

    r1061 r1066  
    365365endblock_re = re.compile(r"""^\s*endblocktrans$""") 
    366366plural_re = re.compile(r"""^\s*plural$""") 
     367constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?'))\)""") 
    367368def templateize(src): 
    368369    from django.core.template import tokenize, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK 
     
    415416                imatch = inline_re.match(t.contents) 
    416417                bmatch = block_re.match(t.contents) 
     418                cmatches = constant_re.findall(t.contents) 
    417419                if imatch: 
    418420                    g = imatch.group(1) 
     
    425427                    singular = [] 
    426428                    plural = [] 
     429                elif cmatches: 
     430                    for cmatch in cmatches: 
     431                        out.write(' _(%s) ' % cmatch) 
    427432                else: 
    428433                    out.write(blankout(t.contents, 'B')) 
    429434            elif t.token_type == TOKEN_VAR: 
    430435                parts = t.contents.split('|') 
    431                 for p in parts: 
     436                cmatch = constant_re.match(parts[0]) 
     437                if cmatch: 
     438                    out.write(' _(%s) ' % cmatch.group(1)) 
     439                for p in parts[1:]: 
    432440                    if p.find(':_(') >= 0: 
    433441                        out.write(' %s ' % p.split(':',1)[1]) 
  • django/branches/i18n/docs/translation.txt

    r1063 r1066  
    172172All tags live in the ``i18n`` tag library, so you need to specify 
    173173``{% load i18n %}`` in the head of your template to make use of them. 
     174 
     175There are some places where you will encounter constant strings in your template code. 
     176One is filter arguments, the other are normal string constants for tags. If you need to 
     177translate those, you can use the ``_("....")`` syntax:: 
     178 
     179    {% some_special_tag _("Page not found") value|yesno:_("yes,no") %} 
     180 
     181In this case both the filter and the tag will see the already translated string, so they 
     182don't need to be aware of translations. And both strings will be pulled out of the templates 
     183for translation and stored in the .po files. 
    174184 
    175185The ``setlang`` redirect view