Django

Code

Changeset 755

Show
Ignore:
Timestamp:
10/01/05 07:47:22 (3 years ago)
Author:
hugo
Message:

i18n now has support for ngettext and has unittests

Files:

Legend:

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

    r739 r755  
    292292        self.cmd = cmd 
    293293        self.i18n_re = re.compile(r'^\s*_\((.*)\)\s*$') 
     294        self.ngettext_re = re.compile(r'''^\s*ngettext\(((?:".+")|(?:'.+')|(?:""".+"""))\s*,\s*((?:".+")|(?:'.+')|(?:""".+"""))\s*,\s*(.*)\)\s*$''') 
     295 
     296    def _resolve_var(self, s, context): 
     297        if s.startswith("'") and s.endswith("'"): 
     298            s = s[1:-1] 
     299        elif s.startswith('"""') and s.endswith('"""'): 
     300            s = s[3:-3] 
     301        elif s.startswith('"') and s.endswith('"'): 
     302            s = s[1:-1] 
     303        else: 
     304            s = template.resolve_variable_with_filters(s, context) 
     305        return s 
    294306 
    295307    def render(self, context): 
    296308        m = self.i18n_re.match(self.cmd) 
    297309        if m: 
    298             s = m.group(1) 
    299             if s.startswith("'") and s.endswith("'"): 
    300                 s = s[1:-1] 
    301             elif s.startswith('"""') and s.endswith('"""'): 
    302                 s = s[3:-3] 
    303             elif s.startswith('"') and s.endswith('"'): 
    304                 s = s[1:-1] 
    305             else: 
    306                 s = template.resolve_variable_with_filters(s, context) 
     310            s = self._resolve_var(m.group(1), context) 
    307311            return translation.gettext(s) % context 
    308         else: 
    309             raise template.TemplateSyntaxError("i18n must be called as {% i18n _('some message') %}") 
     312        m = self.ngettext_re.match(self.cmd) 
     313        if m: 
     314            singular = self._resolve_var(m.group(1), context) 
     315            plural = self._resolve_var(m.group(2), context) 
     316            var = template.resolve_variable_with_filters(m.group(3), context) 
     317            return translation.ngettext(singular, plural, var) % context 
     318        raise template.TemplateSyntaxError("i18n must be called as {% i18n _('some message') %} or {% i18n ngettext('singular', 'plural', var) %}") 
    310319 
    311320def do_comment(parser, token): 
     
    779788 
    780789        {% i18n _('test') %} 
     790        {% i18n ngettext('singular', 'plural', counter) %} 
    781791 
    782792    """ 
  • django/branches/i18n/django/utils/translation.py

    r738 r755  
    149149    """ 
    150150    return message 
     151 
     152def ngettext(singular, plural, number): 
     153    """ 
     154    This function returns the translation of either the singular 
     155    or plural, based on the number. 
     156    """ 
     157    if number == 1: return gettext(singular) 
     158    else: return gettext(plural) 
    151159 
    152160def get_language_from_request(request): 
  • django/branches/i18n/tests/othertests/templates.py

    r715 r755  
    11from django.core import template, template_loader 
     2from django.utils.translation import activate, deactivate 
    23 
    34# Helper objects for template tests 
     
    211212    # Raise exception for custom tags used in child with {% load %} tag in parent, not in child 
    212213    'exception04': ("{% extends 'inheritance17' %}{% block first %}{% echo 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError), 
     214 
     215    # simple translation of a string delimited by ' 
     216    'i18n01': ("{% i18n _('xxxyyyxxx') %}", {}, "xxxyyyxxx"), 
     217 
     218    # simple translation of a string delimited by " 
     219    'i18n02': ('{% i18n _("xxxyyyxxx") %}', {}, "xxxyyyxxx"), 
     220 
     221    # simple translation of a string delimited by """ 
     222    'i18n03': ('{% i18n _("""xxxyyyxxx""") %}', {}, "xxxyyyxxx"), 
     223 
     224    # simple translation of a variable 
     225    'i18n04': ('{% i18n _(anton) %}', {'anton': 'xxxyyyxxx'}, "xxxyyyxxx"), 
     226 
     227    # simple translation of a variable 
     228    'i18n05': ('{% i18n _(anton|lower) %}', {'anton': 'XXXYYYXXX'}, "xxxyyyxxx"), 
     229 
     230    # simple translation of a string with interpolation 
     231    'i18n05': ('{% i18n _("xxx%(anton)sxxx") %}', {'anton': 'yyy'}, "xxxyyyxxx"), 
     232 
     233    # simple translation of a string to german 
     234    'i18n07': ('{% i18n _("Page not found") %}', {'LANGUAGE_CODE': 'de'}, "Seite nicht gefunden"), 
     235 
     236    # translation of singular form 
     237    'i18n08': ('{% i18n ngettext("singular", "plural", count) %}', {'count': 1}, "singular"), 
     238 
     239    # translation of plural form 
     240    'i18n09': ('{% i18n ngettext("singular", "plural", count) %}', {'count': 2}, "plural"), 
    213241} 
    214242 
     
    226254    tests.sort() 
    227255    for name, vals in tests: 
     256        if 'LANGUAGE_CODE' in vals[1]: 
     257            activate('*', vals[1]['LANGUAGE_CODE']) 
    228258        try: 
    229259            output = template_loader.get_template(name).render(template.Context(vals[1])) 
     
    237267                failed_tests.append(name) 
    238268            continue 
     269        if 'LANGUAGE_CODE' in vals[1]: 
     270            deactivate() 
    239271        if output == vals[2]: 
    240272            if verbosity: