Changeset 755
- Timestamp:
- 10/01/05 07:47:22 (3 years ago)
- Files:
-
- django/branches/i18n/django/core/defaulttags.py (modified) (2 diffs)
- django/branches/i18n/django/utils/translation.py (modified) (1 diff)
- django/branches/i18n/tests/othertests/templates.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/i18n/django/core/defaulttags.py
r739 r755 292 292 self.cmd = cmd 293 293 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 294 306 295 307 def render(self, context): 296 308 m = self.i18n_re.match(self.cmd) 297 309 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) 307 311 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) %}") 310 319 311 320 def do_comment(parser, token): … … 779 788 780 789 {% i18n _('test') %} 790 {% i18n ngettext('singular', 'plural', counter) %} 781 791 782 792 """ django/branches/i18n/django/utils/translation.py
r738 r755 149 149 """ 150 150 return message 151 152 def 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) 151 159 152 160 def get_language_from_request(request): django/branches/i18n/tests/othertests/templates.py
r715 r755 1 1 from django.core import template, template_loader 2 from django.utils.translation import activate, deactivate 2 3 3 4 # Helper objects for template tests … … 211 212 # Raise exception for custom tags used in child with {% load %} tag in parent, not in child 212 213 '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"), 213 241 } 214 242 … … 226 254 tests.sort() 227 255 for name, vals in tests: 256 if 'LANGUAGE_CODE' in vals[1]: 257 activate('*', vals[1]['LANGUAGE_CODE']) 228 258 try: 229 259 output = template_loader.get_template(name).render(template.Context(vals[1])) … … 237 267 failed_tests.append(name) 238 268 continue 269 if 'LANGUAGE_CODE' in vals[1]: 270 deactivate() 239 271 if output == vals[2]: 240 272 if verbosity:
