Commits

Carl Meyer committed 41b305e

Sorted language codes longest-first to avoid matching e.g. 'nl' before 'nl-be'. Fixes #15

Comments (0)

Files changed (3)

 ---------
 
 Tip: (unreleased)
+  * Sorted language codes longest-first to avoid matching e.g. 'nl' before
+    'nl-be'. Fixes #15. Thanks Roman Barczyński for report and draft patch.
   * Added ``LOCALE_INDEPENDENT_STATIC_URL`` setting to mirror
     ``LOCALE_INDEPENDENT_MEDIA_URL``. This setting defaults to True, so if you
     want URLs under STATIC_URL to be locale-dependent, you will need to set it

localeurl/settings.py

 from django.conf import settings
 
 SUPPORTED_LOCALES = dict(settings.LANGUAGES)
-LOCALES_RE = '|'.join(SUPPORTED_LOCALES)
+# Sort locale codes longest-first to avoid matching e.g. 'nl' before 'nl-be'
+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)
 
 LOCALE_INDEPENDENT_PATHS = getattr(settings, 'LOCALE_INDEPENDENT_PATHS', ())

localeurl/tests/__init__.py

         USE_I18N = True,
         LANGUAGES = (
             ('en', 'English'),
+            ('nl', 'Dutch'),
             ('nl-nl', 'Dutch'),
             ('nl-be', 'Flemish'),
             ('fr', 'French'),
 
     def test_locale_url_tag(self):
         self.assertRaises(ValueError, self.render_template,
-                '{% locale_url "nl" dummy0 %}')
+                '{% locale_url "pt" dummy0 %}')
 
         self.assertEqual('/en/dummy/', self.render_template(
                 '{% locale_url "en-us" dummy0 %}'))
 
         def test_locale_url_tag(self):
             self.assertRaises(ValueError, self.render_template,
-                    '{% locale_url "nl" "dummy0" %}')
+                    '{% locale_url "pt" "dummy0" %}')
 
             self.assertEqual('/en/dummy/', self.render_template(
                     '{% locale_url "en-us" "dummy0" %}'))