diff options
| author | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-01 12:47:22 +0000 |
|---|---|---|
| committer | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-01 12:47:22 +0000 |
| commit | 4eb73a35848bb31e84ee6ae002224ba5023968b4 (patch) | |
| tree | 2ba69c1a2d6e7425af3ebc8f603f0092b9387c91 /django | |
| parent | 3049adf4859319a19e6a0f72eb172a13b71aaf06 (diff) | |
i18n now has support for ngettext and has unittests
git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@755 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/defaulttags.py | 32 | ||||
| -rw-r--r-- | django/utils/translation.py | 8 |
2 files changed, 29 insertions, 11 deletions
diff --git a/django/core/defaulttags.py b/django/core/defaulttags.py index e351a70f7e..4e1b4e551c 100644 --- a/django/core/defaulttags.py +++ b/django/core/defaulttags.py @@ -291,22 +291,31 @@ class I18NNode(template.Node): def __init__(self, cmd): self.cmd = cmd self.i18n_re = re.compile(r'^\s*_\((.*)\)\s*$') + self.ngettext_re = re.compile(r'''^\s*ngettext\(((?:".+")|(?:'.+')|(?:""".+"""))\s*,\s*((?:".+")|(?:'.+')|(?:""".+"""))\s*,\s*(.*)\)\s*$''') + + def _resolve_var(self, s, context): + if s.startswith("'") and s.endswith("'"): + s = s[1:-1] + elif s.startswith('"""') and s.endswith('"""'): + s = s[3:-3] + elif s.startswith('"') and s.endswith('"'): + s = s[1:-1] + else: + s = template.resolve_variable_with_filters(s, context) + return s def render(self, context): m = self.i18n_re.match(self.cmd) if m: - s = m.group(1) - if s.startswith("'") and s.endswith("'"): - s = s[1:-1] - elif s.startswith('"""') and s.endswith('"""'): - s = s[3:-3] - elif s.startswith('"') and s.endswith('"'): - s = s[1:-1] - else: - s = template.resolve_variable_with_filters(s, context) + s = self._resolve_var(m.group(1), context) return translation.gettext(s) % context - else: - raise template.TemplateSyntaxError("i18n must be called as {% i18n _('some message') %}") + m = self.ngettext_re.match(self.cmd) + if m: + singular = self._resolve_var(m.group(1), context) + plural = self._resolve_var(m.group(2), context) + var = template.resolve_variable_with_filters(m.group(3), context) + return translation.ngettext(singular, plural, var) % context + raise template.TemplateSyntaxError("i18n must be called as {% i18n _('some message') %} or {% i18n ngettext('singular', 'plural', var) %}") def do_comment(parser, token): """ @@ -778,6 +787,7 @@ def do_i18n(parser, token): For example:: {% i18n _('test') %} + {% i18n ngettext('singular', 'plural', counter) %} """ args = token.contents.split(' ', 1) diff --git a/django/utils/translation.py b/django/utils/translation.py index 6385c7b26b..7cbaa27217 100644 --- a/django/utils/translation.py +++ b/django/utils/translation.py @@ -149,6 +149,14 @@ def gettext_noop(message): """ return message +def ngettext(singular, plural, number): + """ + This function returns the translation of either the singular + or plural, based on the number. + """ + if number == 1: return gettext(singular) + else: return gettext(plural) + def get_language_from_request(request): """ analyze the request to find what language the user |
