Commits

Anonymous committed 6d47d5f

add option to check Accept-Language if no locale in URL; fixes #1

Comments (0)

Files changed (4)

docs/installation.rst

   (``settings.LANGUAGE_CODE``). For example, if ``LANGUAGE_CODE == 'en'`` then
   the path ``/about/`` will be passed to the URL resolver unchanged and
   ``/en/about/`` will be redirected to ``/about/``.
+
+``LOCALEURL_USE_ACCEPT_LANGUAGE`` (default: ``False``)
+  Whether to check the ``Accept-Language`` header from the browser as
+  an intermediate fallback in case no locale is specified in the
+  URL. (The default behavior, preserved for backwards compatibility,
+  is to fallback directly to ``settings.LANGUAGE_CODE``).

localeurl/__init__.py

         'LOCALE_INDEPENDENT_MEDIA_URL', True)
 
 PREFIX_DEFAULT_LOCALE = getattr(settings, 'PREFIX_DEFAULT_LOCALE', True)
+
+USE_ACCEPT_LANGUAGE = getattr(settings, 'LOCALEURL_USE_ACCEPT_LANGUAGE', False)

localeurl/middleware.py

 import django.core.exceptions
 from django.http import HttpResponsePermanentRedirect
 from django.utils import translation
+# TODO importing undocumented function
+from django.utils.translation.trans_real import parse_accept_lang_header
 import localeurl
 from localeurl import utils
 
 
     def process_request(self, request):
         locale, path = utils.strip_path(request.path_info)
+        if localeurl.USE_ACCEPT_LANGUAGE and not locale:
+            accept_langs = filter(lambda x: x, [utils.supported_language(lang[0])
+                                                for lang in
+                                                parse_accept_lang_header(
+                        request.META.get('HTTP_ACCEPT_LANGUAGE', ''))])
+            if accept_langs:
+                locale = accept_langs[0]
         locale_path = utils.locale_path(path, locale)
         if locale_path != request.path_info:
             if request.META.get("QUERY_STRING", ""):
         self.assertEqual(301, r2.status_code)
         self.assertEqual('/test/independent/?foo=bar', r2['Location'])
 
+    def test_check_accept_lang(self):
+        self.settings_manager.set(LOCALEURL_USE_ACCEPT_LANGUAGE=True)
+        reload(localeurl)
+
+        r1 = self.request_factory.get('/test/', HTTP_ACCEPT_LANGUAGE='fr, de;q=0.8')
+        r2 = self.middleware.process_request(r1)
+        self.assertEqual(301, r2.status_code)
+        self.assertEqual('/fr/test/', r2['Location'])
+
 class DefaultPrefixMiddlewareTestCase(MiddlewareTestCase):
     def setUp(self):
         super(DefaultPrefixMiddlewareTestCase, self).setUp()
 class NoDefaultPrefixMiddlewareTestCase(MiddlewareTestCase):
     def setUp(self):
         super(NoDefaultPrefixMiddlewareTestCase, self).setUp()
-        self.request_factory = test_utils.RequestFactory()
-        self.middleware = middleware.LocaleURLMiddleware()
         self.settings_manager.set(PREFIX_DEFAULT_LOCALE=False)
         reload(localeurl)
         
         self.assertEqual('/test/foo/', r1.path_info)
 
 
-
-
 class TagsTestCase(LocaleurlTestCase):
     def render_template(self, text):
         t = test_utils.TestTemplate(text, libraries=[localeurl_tags.register])