Commits

Hector Garcia committed 90d68b6

Modified middleware and change locale view to use django_language cookie and FORCE_LOCALEURL_LANG settings.

Comments (0)

Files changed (3)

 ^django_localeurl.egg-info/
 ^HGREV$
 ^\.tox/
+
+syntax: glob
+*.pyc
+*.swp

localeurl/middleware.py

 
     def process_request(self, request):
         locale, path = utils.strip_path(request.path_info)
-        if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale:
+        # Make localeurl work with django_language cookie
+        lang_cookie = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME, False)
+        locale = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME, locale)
+        # Force certain urls to a given language
+        forced_lang_dict = getattr(settings, 'FORCE_LOCALEURL_LANG', dict())
+        patterns = filter(lambda x: x in forced_lang_dict, request.path.split('/'))
+        if patterns:
+            locale = forced_lang_dict[patterns[0]]
+        if localeurl_settings.USE_ACCEPT_LANGUAGE and not lang_cookie:
             accept_langs = filter(lambda x: x, [utils.supported_language(lang[0])
                                                 for lang in
                                                 parse_accept_lang_header(

localeurl/views.py

 from urlparse import urlsplit
 from django import http
 from django.utils.translation import check_for_language
+from django.conf import settings
 from localeurl import utils
 
 def change_locale(request):
             path = utils.locale_path(path, locale)
 
     response = http.HttpResponseRedirect(path)
+    if request.method == 'POST':
+        response.set_cookie(settings.LANGUAGE_COOKIE_NAME, locale)
     return response