From 12d40a9fd63d1b92d7410f8fec8adc69980daee0 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Tue, 16 Feb 2010 12:17:17 +0000 Subject: [PATCH] Fixed #11384 - Make sure translations in the project directory really override translations in apps as specified in the docs. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12447 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/translation/trans_real.py | 6 +- .../other/locale/de/LC_MESSAGES/django.mo | Bin 0 -> 469 bytes .../other/locale/de/LC_MESSAGES/django.po | 22 +++++ .../i18n/resolution/__init__.py | 0 .../locale/de/LC_MESSAGES/django.mo | Bin 0 -> 460 bytes .../locale/de/LC_MESSAGES/django.po | 22 +++++ .../regressiontests/i18n/resolution/models.py | 1 + tests/regressiontests/i18n/tests.py | 78 ++++++++++++++++++ .../locale/de/LC_MESSAGES/django.mo | Bin 0 -> 464 bytes .../locale/de/LC_MESSAGES/django.po | 22 +++++ tests/regressiontests/views/tests/i18n.py | 1 - 11 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo create mode 100644 tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po create mode 100644 tests/regressiontests/i18n/resolution/__init__.py create mode 100644 tests/regressiontests/i18n/resolution/locale/de/LC_MESSAGES/django.mo create mode 100644 tests/regressiontests/i18n/resolution/locale/de/LC_MESSAGES/django.po create mode 100644 tests/regressiontests/i18n/resolution/models.py create mode 100644 tests/regressiontests/locale/de/LC_MESSAGES/django.mo create mode 100644 tests/regressiontests/locale/de/LC_MESSAGES/django.po diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index e6e0a61201..7a66177f30 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -155,9 +155,6 @@ def translation(language): if os.path.isdir(localepath): res = _merge(localepath) - if projectpath and os.path.isdir(projectpath): - res = _merge(projectpath) - for appname in settings.INSTALLED_APPS: app = import_module(appname) apppath = os.path.join(os.path.dirname(app.__file__), 'locale') @@ -165,6 +162,9 @@ def translation(language): if os.path.isdir(apppath): res = _merge(apppath) + if projectpath and os.path.isdir(projectpath): + res = _merge(projectpath) + if res is None: if fallback is not None: res = fallback diff --git a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.mo new file mode 100644 index 0000000000000000000000000000000000000000..2bc9343aa31850042669f03865cb890cc73e35d3 GIT binary patch literal 469 zcmYL_QE$^Q5XS==59E=zNqacnHjP`84vEQYrBAh0q=_JO(WLgu9QoC|o1RsP? z!*}7ca2b<&@=tds{qOv@|Gql-9Z@_dj)*tJ3t~j{Q4%kSkHo{XDEdtKkoZG2oyR#! z`^$7{F(mzbU{7Lso@Cx$+ryOnzmS?B;#azTv zbpdOyY}cybJcHR%N|^Ipz)0jglh=GYt;Aw+MN-@BRHL!hYQsSC`OUJoJd*OdHD}H@ zS8<-@V*jZ0EQu%125sq%Ut2O8^v^KZw#wK`SbbBz(_y@`I?$G~nM+ipC(g#YFE*uv{Eq$hDi|FYW*?zIVUEQ<-3;-TdA?cyZ* E2Y+CF82|tP literal 0 HcmV?d00001 diff --git a/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po new file mode 100644 index 0000000000..2fdcee5015 --- /dev/null +++ b/tests/regressiontests/i18n/other/locale/de/LC_MESSAGES/django.po @@ -0,0 +1,22 @@ +# 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: 2010-02-14 17:33+0100\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: 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:3 +msgid "Date/time" +msgstr "Datum/Zeit (LOCALE_PATHS)" diff --git a/tests/regressiontests/i18n/resolution/__init__.py b/tests/regressiontests/i18n/resolution/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/i18n/resolution/locale/de/LC_MESSAGES/django.mo b/tests/regressiontests/i18n/resolution/locale/de/LC_MESSAGES/django.mo new file mode 100644 index 0000000000000000000000000000000000000000..1c37ac5c87e9392a742ea3db6149b902c9667bd9 GIT binary patch literal 460 zcmYL_QE$^Q5XZv=59Dd$nTO+T)3`S2CT;TCsit*HMAC#boy2%Rru8yUsa-kl1RsP? z!*}7ca2ZHF`KP;+{&)V{f8MI z4l$Kp^}XJzpm6q4tvx0iyj8B%)-WjePdIY|LeX8USgKxl*7lIWy#rHz7Sz5 zTaT5KrfsCRj=^k|rm*0dfT75El8*UwT8icJhNPz6%UWTjk{sIy1dSU, YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2010-02-14 17:33+0100\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: 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:3 +msgid "Date/time" +msgstr "Datum/Zeit (APP)" diff --git a/tests/regressiontests/i18n/resolution/models.py b/tests/regressiontests/i18n/resolution/models.py new file mode 100644 index 0000000000..4287ca8617 --- /dev/null +++ b/tests/regressiontests/i18n/resolution/models.py @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index 588a7b607c..a4d0d90400 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +import os import sys import decimal import datetime @@ -497,3 +498,80 @@ class MiscTests(TestCase): r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: 'zh-cn'} r.META = {'HTTP_ACCEPT_LANGUAGE': 'de'} self.assertEqual(g(r), 'zh-cn') + +class ResolutionOrderI18NTests(TestCase): + + def setUp(self): + from django.utils.translation import trans_real + # Okay, this is brutal, but we have no other choice to fully reset + # the translation framework + trans_real._active = {} + trans_real._translations = {} + activate('de') + + def tearDown(self): + deactivate() + + def assertUgettext(self, msgid, msgstr): + result = ugettext(msgid) + self.assert_(msgstr in result, ("The string '%s' isn't in the " + "translation of '%s'; the actual result is '%s'." % (msgstr, msgid, result))) + +class AppResolutionOrderI18NTests(ResolutionOrderI18NTests): + + def setUp(self): + self.old_installed_apps = settings.INSTALLED_APPS + settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) + ['regressiontests.i18n.resolution'] + super(AppResolutionOrderI18NTests, self).setUp() + + def tearDown(self): + settings.INSTALLED_APPS = self.old_installed_apps + super(AppResolutionOrderI18NTests, self).tearDown() + + def test_app_translation(self): + self.assertUgettext('Date/time', 'APP') + +class LocalePathsResolutionOrderI18NTests(ResolutionOrderI18NTests): + + def setUp(self): + self.old_locale_paths = settings.LOCALE_PATHS + settings.LOCALE_PATHS += (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),) + super(LocalePathsResolutionOrderI18NTests, self).setUp() + + def tearDown(self): + settings.LOCALE_PATHS = self.old_locale_paths + super(LocalePathsResolutionOrderI18NTests, self).tearDown() + + def test_locale_paths_translation(self): + self.assertUgettext('Date/time', 'LOCALE_PATHS') + +class ProjectResolutionOrderI18NTests(ResolutionOrderI18NTests): + + def setUp(self): + self.old_settings_module = settings.SETTINGS_MODULE + settings.SETTINGS_MODULE = 'regressiontests' + super(ProjectResolutionOrderI18NTests, self).setUp() + + def tearDown(self): + settings.SETTINGS_MODULE = self.old_settings_module + super(ProjectResolutionOrderI18NTests, self).tearDown() + + def test_project_translation(self): + self.assertUgettext('Date/time', 'PROJECT') + + def test_project_override_app_translation(self): + old_installed_apps = settings.INSTALLED_APPS + settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) + ['regressiontests.i18n.resolution'] + self.assertUgettext('Date/time', 'PROJECT') + settings.INSTALLED_APPS = old_installed_apps + + def test_project_override_locale_paths_translation(self): + old_locale_paths = settings.LOCALE_PATHS + settings.LOCALE_PATHS += (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),) + self.assertUgettext('Date/time', 'PROJECT') + settings.LOCALE_PATHS = old_locale_paths + +class DjangoFallbackResolutionOrderI18NTests(ResolutionOrderI18NTests): + + def test_django_fallback(self): + self.assertUgettext('Date/time', 'Datum/Zeit') diff --git a/tests/regressiontests/locale/de/LC_MESSAGES/django.mo b/tests/regressiontests/locale/de/LC_MESSAGES/django.mo new file mode 100644 index 0000000000000000000000000000000000000000..2ee860a80b5e75cd9f5fe727c02f74936918dc9c GIT binary patch literal 464 zcmYL_!A{#i5Qbeu5A_lC*u!uG(IIxI1X>b=Nj9me9lMT$RC=hEWN{ZcyU}`s%7gT2 zco&|9aX`vwKh2Ev&+NbZvbX!+qu3$75MPOpM2~1ACq5BBi9a7a?>FVo#52`&HglA= zF%(XAoLWjRx8_049mWg1k+xArGsvUCk7&%{h8NQ~oA`xX8S5}@Dz(6?b=6>L82EWs z;=oGbXb9Vl47x!WV9>?z8-%Cq, YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2010-02-14 17:33+0100\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: 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:3 +msgid "Date/time" +msgstr "Datum/Zeit (PROJECT)" diff --git a/tests/regressiontests/views/tests/i18n.py b/tests/regressiontests/views/tests/i18n.py index a6b0b89389..7debb213f6 100644 --- a/tests/regressiontests/views/tests/i18n.py +++ b/tests/regressiontests/views/tests/i18n.py @@ -1,4 +1,3 @@ -from os import path import gettext from django.conf import settings