From abf514053b5097d3071a8a282cadbb4bbb73fe87 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 16 May 2007 12:50:27 +0000 Subject: [PATCH] unicode: Renamed no_trans() to the more descriptive deactivate_all(). Added a verbose_name_raw attribute to the model Options class so that we can more conveniently access the untranslated model name when we need to. git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5255 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/contenttypes/management.py | 6 +----- django/contrib/contenttypes/models.py | 7 ++----- django/db/models/options.py | 10 ++++++++++ django/utils/translation/__init__.py | 6 +++--- django/utils/translation/trans_null.py | 2 +- django/utils/translation/trans_real.py | 2 +- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/django/contrib/contenttypes/management.py b/django/contrib/contenttypes/management.py index 9314ff7393..cb52e0805e 100644 --- a/django/contrib/contenttypes/management.py +++ b/django/contrib/contenttypes/management.py @@ -4,7 +4,6 @@ Creates content types for all installed models. from django.dispatch import dispatcher from django.db.models import get_apps, get_models, signals -from django.utils.translation import activate, no_trans, get_language from django.utils.encoding import smart_unicode def create_contenttypes(app, created_models, verbosity=2): @@ -19,12 +18,9 @@ def create_contenttypes(app, created_models, verbosity=2): ContentType.objects.get(app_label=opts.app_label, model=opts.object_name.lower()) except ContentType.DoesNotExist: - lang = get_language() - no_trans() - ct = ContentType(name=smart_unicode(opts.verbose_name), + ct = ContentType(name=smart_unicode(opts.verbose_name_raw), app_label=opts.app_label, model=opts.object_name.lower()) ct.save() - activate(lang) if verbosity >= 2: print "Adding content type '%s | %s'" % (ct.app_label, ct.model) diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 4b23a2e008..7ac0ce4266 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -1,5 +1,5 @@ from django.db import models -from django.utils.translation import ugettext_lazy as _, no_trans, activate, get_language +from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import smart_unicode CONTENT_TYPE_CACHE = {} @@ -16,12 +16,9 @@ class ContentTypeManager(models.Manager): except KeyError: # The unicode() is needed around opts.verbose_name because it might # be a django.utils.functional.__proxy__ object. - lang = get_language() - no_trans() ct, created = self.model._default_manager.get_or_create(app_label=key[0], - model=key[1], defaults={'name': smart_unicode(opts.verbose_name)}) + model=key[1], defaults={'name': smart_unicode(opts.verbose_name_raw)}) CONTENT_TYPE_CACHE[key] = ct - activate(lang) return ct def clear_cache(self): diff --git a/django/db/models/options.py b/django/db/models/options.py index dd6c586ddd..04d4e665d7 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -5,6 +5,7 @@ from django.db.models.fields import AutoField, FieldDoesNotExist from django.db.models.loading import get_models from django.db.models.query import orderlist2sql from django.db.models import Manager +from django.utils.translation import activate, deactivate_all, get_language from bisect import bisect import re @@ -41,6 +42,15 @@ class Options(object): self.object_name = cls.__name__ self.module_name = self.object_name.lower() self.verbose_name = get_verbose_name(self.object_name) + + # There are a few places where the untranslated verbose name is needed + # (so that we get the same value regardless of currently active + # locale). + lang = get_language() + deactivate_all() + self.verbose_name_raw = unicode(self.verbose_name) + activate(lang) + # Next, apply any overridden values from 'class Meta'. if self.meta: meta_attrs = self.meta.__dict__ diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 563fa92f50..84fa76c57a 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -8,7 +8,7 @@ __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', 'get_language', 'get_language_bidi', 'get_date_formats', 'get_partial_date_formats', 'check_for_language', 'to_locale', 'get_language_from_request', 'install', 'templatize', 'ugettext', - 'ungettext', 'no_trans'] + 'ungettext', 'deactivate_all'] # Here be dragons, so a short explanation of the logic won't hurt: # We are trying to solve two problems: (1) access settings, in particular @@ -105,6 +105,6 @@ def install(): def templatize(src): return real_templatize(src) -def no_trans(): - return real_no_trans() +def deactivate_all(): + return real_deactivate_all() diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py index 433af3897c..a09f5ce8df 100644 --- a/django/utils/translation/trans_null.py +++ b/django/utils/translation/trans_null.py @@ -15,7 +15,7 @@ def ungettext(singular, plural, number): string_concat = lambda *strings: ''.join([str(el) for el in strings]) activate = lambda x: None -deactivate = no_trans = install = lambda: None +deactivate = deactivate_all = install = lambda: None get_language = lambda: settings.LANGUAGE_CODE get_language_bidi = lambda: settings.LANGUAGE_CODE in settings.LANGUAGES_BIDI get_date_formats = lambda: (settings.DATE_FORMAT, settings.DATETIME_FORMAT, settings.TIME_FORMAT) diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index e57408b0fe..b8411366f9 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -203,7 +203,7 @@ def deactivate(): if currentThread() in _active: del _active[currentThread()] -def no_trans(): +def deactivate_all(): """ Makes the active translation object a NullTranslations() instance. This is useful when we want delayed translations to appear as the original string