Fixed #28755 -- Made check_for_language() include apps' locale directories.

This commit is contained in:
Chillar Anand 2018-02-18 21:05:06 +05:30 committed by Tim Graham
parent 9716860596
commit e8e0cfa9e5
2 changed files with 23 additions and 1 deletions

View File

@ -377,7 +377,12 @@ def all_locale_paths():
""" """
globalpath = os.path.join( globalpath = os.path.join(
os.path.dirname(sys.modules[settings.__module__].__file__), 'locale') os.path.dirname(sys.modules[settings.__module__].__file__), 'locale')
return [globalpath] + list(settings.LOCALE_PATHS) app_paths = []
for app_config in apps.get_app_configs():
locale_path = os.path.join(app_config.path, 'locale')
if os.path.exists(locale_path):
app_paths.append(locale_path)
return [globalpath] + list(settings.LOCALE_PATHS) + app_paths
@functools.lru_cache(maxsize=1000) @functools.lru_cache(maxsize=1000)

View File

@ -4,11 +4,14 @@ import gettext as gettext_module
import os import os
import pickle import pickle
import re import re
import tempfile
from contextlib import contextmanager from contextlib import contextmanager
from importlib import import_module from importlib import import_module
from threading import local from threading import local
from unittest import mock
from django import forms from django import forms
from django.apps import AppConfig
from django.conf import settings from django.conf import settings
from django.conf.locale import LANG_INFO from django.conf.locale import LANG_INFO
from django.conf.urls.i18n import i18n_patterns from django.conf.urls.i18n import i18n_patterns
@ -40,6 +43,11 @@ extended_locale_paths = settings.LOCALE_PATHS + [
] ]
class AppModuleStub:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@contextmanager @contextmanager
def patch_formats(lang, **settings): def patch_formats(lang, **settings):
from django.utils.formats import _format_cache from django.utils.formats import _format_cache
@ -1645,6 +1653,15 @@ class NonDjangoLanguageTests(SimpleTestCase):
self.assertEqual(get_language(), 'xxx') self.assertEqual(get_language(), 'xxx')
self.assertEqual(gettext("year"), "reay") self.assertEqual(gettext("year"), "reay")
@override_settings(USE_I18N=True)
def test_check_for_langauge(self):
with tempfile.TemporaryDirectory() as app_dir:
os.makedirs(os.path.join(app_dir, 'locale', 'dummy_Lang', 'LC_MESSAGES'))
open(os.path.join(app_dir, 'locale', 'dummy_Lang', 'LC_MESSAGES', 'django.mo'), 'w').close()
app_config = AppConfig('dummy_app', AppModuleStub(__path__=[app_dir]))
with mock.patch('django.apps.apps.get_app_configs', return_value=[app_config]):
self.assertIs(check_for_language('dummy-lang'), True)
@override_settings( @override_settings(
USE_I18N=True, USE_I18N=True,
LANGUAGES=[ LANGUAGES=[