Carl Meyer avatar Carl Meyer committed a424bb4

Made language-code matching case insensitive.

Comments (0)

Files changed (5)

 ---------
 
 Tip: (unreleased)
+  * Made language-code matching case-insensitive, per RFC. Thanks torgeilo for
+    the report.
   * Dropped support for Django 1.0 and Python 2.4.
   * Fixed issue with non-ASCII characters in URLs. Thanks Chris Adams.
 

localeurl/middleware.py

             if accept_langs:
                 locale = accept_langs[0]
         locale_path = utils.locale_path(path, locale)
-        if locale_path != request.path_info:
+        if locale_path != request.path_info.lower():
             locale_url = utils.add_script_prefix(locale_path)
 
             qs = request.META.get("QUERY_STRING", "")

localeurl/settings.py

 import re
 from django.conf import settings
 
-SUPPORTED_LOCALES = dict(settings.LANGUAGES)
+SUPPORTED_LOCALES = dict(
+    (code.lower(), name) for code, name in settings.LANGUAGES)
 # Issue #15. Sort locale codes to avoid matching e.g. 'pt' before 'pt-br'
 LOCALES_RE = '|'.join(
     sorted(SUPPORTED_LOCALES.keys(), key=lambda i: len(i), reverse=True))
-PATH_RE = re.compile(r'^/(?P<locale>%s)(?P<path>.*)$' % LOCALES_RE)
+PATH_RE = re.compile(r'^/(?P<locale>%s)(?P<path>.*)$' % LOCALES_RE, re.I)
 
 LOCALE_INDEPENDENT_PATHS = [re.compile(p) for p in
                             getattr(settings, 'LOCALE_INDEPENDENT_PATHS', [])]

localeurl/tests/tests.py

         self.assertEqual('/test/bla/bla/', r1.path_info)
 
 
+    def test_case_insensitive_locale(self):
+        r1 = self.request_factory.get('/nl-NL/test/bla/bla/')
+        r2 = self.middleware.process_request(r1)
+        self.assertEqual(None, r2)
+        self.assertEqual('nl-nl', r1.LANGUAGE_CODE)
+        self.assertEqual('/test/bla/bla/', r1.path_info)
+
+
     def test_locale_independent_url(self):
         r1 = self.request_factory.get('/test/independent/bla/bla/')
         r2 = self.middleware.process_request(r1)

localeurl/utils.py

     """
     Returns the supported language (from settings.LANGUAGES) for the locale.
     """
+    locale = locale.lower()
     if locale in localeurl_settings.SUPPORTED_LOCALES:
         return locale
     elif locale[:2] in localeurl_settings.SUPPORTED_LOCALES:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.