diff options
| author | Georg Bauer <gb@hugo.westfalen.de> | 2005-09-29 17:01:05 +0000 |
|---|---|---|
| committer | Georg Bauer <gb@hugo.westfalen.de> | 2005-09-29 17:01:05 +0000 |
| commit | 98d74306099d73a4e6765bf677207b240f82dd16 (patch) | |
| tree | 062de72e915bce4475f5bf7470970cfb9cbee74e /django/core | |
| parent | a60b2050c643314996434117892136fba8adb487 (diff) | |
i18n patch - references #65
git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@726 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core')
| -rw-r--r-- | django/core/defaulttags.py | 43 | ||||
| -rw-r--r-- | django/core/validators.py | 2 |
2 files changed, 44 insertions, 1 deletions
diff --git a/django/core/defaulttags.py b/django/core/defaulttags.py index 103eb4c9f4..29eaf4cf70 100644 --- a/django/core/defaulttags.py +++ b/django/core/defaulttags.py @@ -1,8 +1,11 @@ "Default tags used by the template system, available to all templates." +import re import sys import template +from django.utils import translation + class CommentNode(template.Node): def render(self, context): return '' @@ -283,6 +286,28 @@ class WidthRatioNode(template.Node): return '' return str(int(round(ratio))) +class I18NNode(template.Node): + + def __init__(self, cmd): + self.cmd = cmd + self.i18n_re = re.compile(r'^\s*_\((.*)\)\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: + raise template.TemplateSyntaxError("i18n must be called as {% i18n _('some message') %}") + return translation.gettext(s) % context + else: + raise template.TemplateSyntaxError("i18n must be called as {% i18n _('some message') %}") + def do_comment(parser, token): """ Ignore everything between ``{% comment %}`` and ``{% endcomment %}`` @@ -745,6 +770,22 @@ def do_widthratio(parser, token): raise template.TemplateSyntaxError("widthratio final argument must be an integer") return WidthRatioNode(this_value_var, max_value_var, max_width) +def do_i18n(parser, token): + """ + translate a given string with the current + translation object. + + For example:: + + {% i18n _('test') %} + + """ + args = token.contents.split(' ', 1) + if len(args) != 2: + raise template.TemplateSyntaxError("'i18n' requires one argument (got %r)" % args) + + return I18NNode(args[1].strip()) + template.register_tag('comment', do_comment) template.register_tag('cycle', do_cycle) template.register_tag('debug', do_debug) @@ -761,3 +802,5 @@ template.register_tag('load', do_load) template.register_tag('now', do_now) template.register_tag('templatetag', do_templatetag) template.register_tag('widthratio', do_widthratio) +template.register_tag('i18n', do_i18n) + diff --git a/django/core/validators.py b/django/core/validators.py index 697c2efe81..bc70a330e0 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -53,7 +53,7 @@ class CriticalValidationError(Exception): def isAlphaNumeric(field_data, all_data): if not alnum_re.search(field_data): - raise ValidationError, "This value must contain only letters, numbers and underscores." + raise ValidationError, _("This value must contain only letters, numbers and underscores.") def isAlphaNumericURL(field_data, all_data): if not alnumurl_re.search(field_data): |
