Make LazyUGettext a Promise so it deconstructs nicely.
[fnpdjango.git] / fnpdjango / utils / urls.py
index bf9747d..4e9916f 100644 (file)
@@ -2,39 +2,50 @@
 Utilities for urlconfs.
 """
 
-import re
-from django.conf import settings
-from django.conf.urls import patterns
-from django.core.urlresolvers import LocaleRegexURLResolver
-from django.utils.translation import get_language, string_concat
+import django
+if django.VERSION >= (1, 10):
+    import warnings
+    from django.conf.urls.i18n import i18n_patterns as django_i18n_patterns
 
+    def i18n_patterns(*args):
+        warnings.warn(
+            "As of Django 1.10, fnpdjango.utils.urls.i18n_patterns "
+            "is deprecated in favor of directly using "
+            "django.urls.i18n_patterns(prefix_default_language=False).",
+            DeprecationWarning)
+        return django_i18n_patterns(*args, prefix_default_language=False)
 
-class MyLocaleRegexURLResolver(LocaleRegexURLResolver):
-    """
-    A URL resolver that always matches the active language code as URL prefix.
+else:
+    # Django <= 1.9
+    import re
+    from django.conf import settings
+    from django.core.urlresolvers import LocaleRegexURLResolver
+    from django.utils.translation import get_language
 
-    Rather than taking a regex argument, we just override the ``regex``
-    function to always return the active language-code as regex.
-    """
-    @property
-    def regex(self):
-        language_code = get_language()
-        if language_code == settings.LANGUAGE_CODE:
-            return re.compile('')
-        if language_code not in self._regex_dict:
-            regex_compiled = re.compile('^%s/' % language_code, re.UNICODE)
-            self._regex_dict[language_code] = regex_compiled
-        return self._regex_dict[language_code]
+    class MyLocaleRegexURLResolver(LocaleRegexURLResolver):
+        """
+        A URL resolver that always matches the active language code as URL prefix.
 
+        Rather than taking a regex argument, we just override the ``regex``
+        function to always return the active language-code as regex.
+        """
+        @property
+        def regex(self):
+            language_code = get_language()
+            if language_code == settings.LANGUAGE_CODE:
+                return re.compile('')
+            if language_code not in self._regex_dict:
+                regex_compiled = re.compile('^%s/' % language_code, re.UNICODE)
+                self._regex_dict[language_code] = regex_compiled
+            return self._regex_dict[language_code]
 
-def i18n_patterns(prefix, *args):
-    """
-    Adds the language code prefix to every URL pattern within this
-    function. This may only be used in the root URLconf, not in an included
-    URLconf.
-
-    """
-    pattern_list = patterns(prefix, *args)
-    if not settings.USE_I18N:
-        return pattern_list
-    return pattern_list + [MyLocaleRegexURLResolver(pattern_list)]
+    def i18n_patterns(*args):
+        """
+        Adds the language code prefix to every URL pattern within this
+        function. This may only be used in the root URLconf, not in an included
+        URLconf.
+        """
+        pattern_list = list(args)
+        if not settings.USE_I18N:
+            return pattern_list
+        return pattern_list + [MyLocaleRegexURLResolver(pattern_list)]