From f9dad46d3665214ac80af06371fae10c55605086 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Fri, 16 Sep 2011 11:57:03 +0000 Subject: [PATCH] Fixed #16803 -- Use model verbose_name directly as ContentType unicode representation so it can be translated. Thanks to bronger for the report and Ivan Sagalaev for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16839 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/contenttypes/models.py | 15 +++++++- .../i18n/contenttypes/__init__.py | 0 .../locale/en/LC_MESSAGES/django.mo | Bin 0 -> 418 bytes .../locale/en/LC_MESSAGES/django.po | 26 +++++++++++++ .../locale/fr/LC_MESSAGES/django.mo | Bin 0 -> 461 bytes .../locale/fr/LC_MESSAGES/django.po | 27 ++++++++++++++ .../i18n/contenttypes/tests.py | 35 ++++++++++++++++++ tests/regressiontests/i18n/models.py | 3 ++ tests/regressiontests/i18n/tests.py | 1 + 9 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tests/regressiontests/i18n/contenttypes/__init__.py create mode 100644 tests/regressiontests/i18n/contenttypes/locale/en/LC_MESSAGES/django.mo create mode 100644 tests/regressiontests/i18n/contenttypes/locale/en/LC_MESSAGES/django.po create mode 100644 tests/regressiontests/i18n/contenttypes/locale/fr/LC_MESSAGES/django.mo create mode 100644 tests/regressiontests/i18n/contenttypes/locale/fr/LC_MESSAGES/django.po create mode 100644 tests/regressiontests/i18n/contenttypes/tests.py diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 1f2a3dbbd2..2f20797e95 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -1,6 +1,6 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_unicode, force_unicode class ContentTypeManager(models.Manager): @@ -85,7 +85,18 @@ class ContentType(models.Model): unique_together = (('app_label', 'model'),) def __unicode__(self): - return self.name + # self.name is deprecated in favor of using model's verbose_name, which + # can be translated. Formal deprecation is delayed until we have DB + # migration to be able to remove the field from the database along with + # the attribute. + # + # We return self.name only when users have changed its value from the + # initial verbose_name_raw and might rely on it. + meta = self.model_class()._meta + if self.name != meta.verbose_name_raw: + return self.name + else: + return force_unicode(meta.verbose_name) def model_class(self): "Returns the Python model class for this type of content." diff --git a/tests/regressiontests/i18n/contenttypes/__init__.py b/tests/regressiontests/i18n/contenttypes/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/i18n/contenttypes/locale/en/LC_MESSAGES/django.mo b/tests/regressiontests/i18n/contenttypes/locale/en/LC_MESSAGES/django.mo new file mode 100644 index 0000000000000000000000000000000000000000..673033ab3055fa5ac0a9ee78359aacb416151ac3 GIT binary patch literal 418 zcmY+A-%7(U6vnG4LNC4cBKLY`D-LI!ve;&`*nf82VBWELg ze&h!y@a3HEtmpczejl-Dj_H#Rq z?JH@7#BrM%a~tt#&g&(waoq@WJ1K_H+xgm9uR;TpbG4{s>5<+`W$eF?`~`=( BZASnA literal 0 HcmV?d00001 diff --git a/tests/regressiontests/i18n/contenttypes/locale/en/LC_MESSAGES/django.po b/tests/regressiontests/i18n/contenttypes/locale/en/LC_MESSAGES/django.po new file mode 100644 index 0000000000..2529ce6dfb --- /dev/null +++ b/tests/regressiontests/i18n/contenttypes/locale/en/LC_MESSAGES/django.po @@ -0,0 +1,26 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2011-09-15 15:41-0700\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: models.py:6 +msgid "Anything" +msgstr "" + +#: models.py:15 +msgid "Company" +msgstr "Company" \ No newline at end of file diff --git a/tests/regressiontests/i18n/contenttypes/locale/fr/LC_MESSAGES/django.mo b/tests/regressiontests/i18n/contenttypes/locale/fr/LC_MESSAGES/django.mo new file mode 100644 index 0000000000000000000000000000000000000000..c3083c90b5fc1ae2ed62ac6eb9c1e633c9d95bc0 GIT binary patch literal 461 zcmY+9(MrQG7=^1SVlTb+BA1H(+KNMsn^?DO7TdLUouGGdi#5_ECCx-1#E0-X_8vZq ze^nSB@{tob`E&BMwf5nlR*`LFA6ZA*NTU?lKyDGa;yCxXZz2oyxc;e`;dJ$^;_BT= zjeZdm3*i**gsG)gl%zrTCKxbs$FfnF4Bbpr+E|F|LQdgoUeu6lO58Nbp=$)UcnNyk z3QCSV-v{pu{1)+B^cek#=V1^s@hY3)f4O@WWDqCN3&?ds<8bdG88SEGwT0YpRhQgq zLrH%eMPwMnjI>!CgwbWt>t$>-y1=L^=DZM)3qGSH3WkGmQ@I^Qm!&+=rdVdMY8=LF z`9J@_bGuquq42r)3VU1eW)G{9OLa~r&)n3)cE))hPM5i+){`*6)I?9EDkwR9l-5nl mx#1=BwVBnFsOslfcaF}, YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2011-09-15 15:41-0700\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1)\n" + +#: models.py:6 +msgid "Anything" +msgstr "" + +#: models.py:15 +msgid "Company" +msgstr "Société" \ No newline at end of file diff --git a/tests/regressiontests/i18n/contenttypes/tests.py b/tests/regressiontests/i18n/contenttypes/tests.py new file mode 100644 index 0000000000..eb5c1718fc --- /dev/null +++ b/tests/regressiontests/i18n/contenttypes/tests.py @@ -0,0 +1,35 @@ +# coding: utf-8 +from __future__ import with_statement + +import os + +from django.test import TestCase +from django.test.utils import override_settings +from django.utils import translation +from django.contrib.contenttypes.models import ContentType + + +class ContentTypeTests(TestCase): + def test_verbose_name(self): + company_type = ContentType.objects.get(app_label='i18n', model='company') + with translation.override('en'): + self.assertEqual(unicode(company_type), u'Company') + with translation.override('fr'): + self.assertEqual(unicode(company_type), u'Société') + + def test_field_override(self): + company_type = ContentType.objects.get(app_label='i18n', model='company') + company_type.name = 'Other' + self.assertEqual(unicode(company_type), 'Other') + +ContentTypeTests = override_settings( + USE_I18N=True, + LOCALE_PATHS=( + os.path.join(os.path.dirname(__file__), 'locale'), + ), + LANGUAGE_CODE='en', + LANGUAGES=( + ('en', 'English'), + ('fr', 'French'), + ), +)(ContentTypeTests) diff --git a/tests/regressiontests/i18n/models.py b/tests/regressiontests/i18n/models.py index 75cd996f83..a302769fef 100644 --- a/tests/regressiontests/i18n/models.py +++ b/tests/regressiontests/i18n/models.py @@ -10,3 +10,6 @@ class Company(models.Model): date_added = models.DateTimeField(default=datetime(1799,1,31,23,59,59,0)) cents_payed = models.DecimalField(max_digits=4, decimal_places=2) products_delivered = models.IntegerField() + + class Meta: + verbose_name = _('Company') \ No newline at end of file diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index e6799ae28e..3a75201372 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -26,6 +26,7 @@ from models import Company, TestModel from commands.tests import * from patterns.tests import * +from contenttypes.tests import * from test_warnings import DeprecationWarningTests here = os.path.dirname(os.path.abspath(__file__))